2023-03-17 17:02:18 +01:00
package store
import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type EnvironmentLimitJournal struct {
Model
EnvironmentId 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 ) CreateEnvironmentLimitJournal ( j * EnvironmentLimitJournal , trx * sqlx . Tx ) ( int , error ) {
2023-03-22 19:10:07 +01:00
stmt , err := trx . Prepare ( "insert into environment_limit_journal (environment_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 environment_limit_journal insert statement" )
}
var id int
2023-03-17 17:57:26 +01:00
if err := stmt . QueryRow ( j . EnvironmentId , 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 environment_limit_journal insert statement" )
}
return id , nil
}
2023-03-22 20:17:27 +01:00
func ( str * Store ) IsEnvironmentLimitJournalEmpty ( envId int , trx * sqlx . Tx ) ( bool , error ) {
2023-03-22 19:10:07 +01:00
count := 0
if err := trx . QueryRowx ( "select count(0) from environment_limit_journal where environment_id = $1" , envId ) . Scan ( & count ) ; err != nil {
return false , err
}
return count == 0 , nil
}
2023-03-22 20:17:27 +01:00
func ( str * Store ) FindLatestEnvironmentLimitJournal ( envId int , trx * sqlx . Tx ) ( * EnvironmentLimitJournal , error ) {
2023-03-17 17:02:18 +01:00
j := & EnvironmentLimitJournal { }
2023-03-22 19:10:07 +01:00
if err := trx . QueryRowx ( "select * from environment_limit_journal where environment_id = $1 order by created_at desc limit 1" , envId ) . StructScan ( j ) ; err != nil {
2023-03-17 17:02:18 +01:00
return nil , errors . Wrap ( err , "error finding environment_limit_journal by environment_id" )
}
return j , nil
}