2023-03-17 17:02:18 +01:00
package store
import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type AccountLimitJournal struct {
Model
AccountId int
2023-03-17 17:57:26 +01:00
RxBytes int64
TxBytes int64
2023-03-22 19:10:07 +01:00
Action LimitJournalAction
2023-03-17 17:02:18 +01:00
}
2023-03-22 20:17:27 +01:00
func ( str * Store ) CreateAccountLimitJournal ( j * AccountLimitJournal , trx * sqlx . Tx ) ( int , error ) {
2023-03-22 19:10:07 +01:00
stmt , err := trx . Prepare ( "insert into account_limit_journal (account_id, rx_bytes, tx_bytes, action) values ($1, $2, $3, $4) returning id" )
2023-03-17 17:02:18 +01:00
if err != nil {
return 0 , errors . Wrap ( err , "error preparing account_limit_journal insert statement" )
}
var id int
2023-03-22 18:09:21 +01:00
if err := stmt . QueryRow ( j . AccountId , j . RxBytes , j . TxBytes , j . Action ) . Scan ( & id ) ; err != nil {
2023-03-17 17:02:18 +01:00
return 0 , errors . Wrap ( err , "error executing account_limit_journal insert statement" )
}
return id , nil
}
2023-03-22 20:17:27 +01:00
func ( str * Store ) IsAccountLimitJournalEmpty ( acctId int , trx * sqlx . Tx ) ( bool , error ) {
2023-03-22 18:09:21 +01:00
count := 0
2023-03-22 19:10:07 +01:00
if err := trx . QueryRowx ( "select count(0) from account_limit_journal where account_id = $1" , acctId ) . Scan ( & count ) ; err != nil {
2023-03-22 18:09:21 +01:00
return false , err
}
return count == 0 , nil
}
2023-03-22 20:17:27 +01:00
func ( str * Store ) FindLatestAccountLimitJournal ( acctId int , trx * sqlx . Tx ) ( * AccountLimitJournal , error ) {
2023-03-17 17:02:18 +01:00
j := & AccountLimitJournal { }
2023-03-22 19:10:07 +01:00
if err := trx . QueryRowx ( "select * from account_limit_journal where account_id = $1 order by id desc limit 1" , acctId ) . StructScan ( j ) ; err != nil {
2023-03-17 17:02:18 +01:00
return nil , errors . Wrap ( err , "error finding account_limit_journal by account_id" )
}
return j , nil
}
2023-03-22 20:42:47 +01:00
func ( str * Store ) FindAllLatestAccountLimitJournal ( trx * sqlx . Tx ) ( [ ] * AccountLimitJournal , error ) {
rows , err := trx . Queryx ( "select id, account_id, rx_bytes, tx_bytes, action, created_at, updated_at from account_limit_journal where id in (select max(id) as id from account_limit_journal group by account_id)" )
if err != nil {
2023-03-23 20:13:59 +01:00
return nil , errors . Wrap ( err , "error selecting all latest account_limit_journal" )
2023-03-22 20:42:47 +01:00
}
2023-03-23 20:13:59 +01:00
var aljs [ ] * AccountLimitJournal
2023-03-22 20:42:47 +01:00
for rows . Next ( ) {
2023-03-23 20:13:59 +01:00
alj := & AccountLimitJournal { }
if err := rows . StructScan ( alj ) ; err != nil {
2023-03-22 20:42:47 +01:00
return nil , errors . Wrap ( err , "error scanning account_limit_journal" )
}
2023-03-23 20:13:59 +01:00
aljs = append ( aljs , alj )
2023-03-22 20:42:47 +01:00
}
2023-03-23 20:13:59 +01:00
return aljs , nil
}
func ( str * Store ) DeleteAccountLimitJournalForAccount ( acctId int , trx * sqlx . Tx ) error {
if _ , err := trx . Exec ( "delete from account_limit_journal where account_id = $1" , acctId ) ; err != nil {
return errors . Wrapf ( err , "error deleting account_limit journal for '#%d'" , acctId )
}
return nil
2023-03-22 20:42:47 +01:00
}