From 2c4a5d43d28cb2c5eb2a05633363fa067f758b34 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 17 Mar 2023 12:02:18 -0400 Subject: [PATCH] [account|environment|share] limit journals (#273) --- controller/store/accountLimitJournal.go | 32 +++++++++++++++++++++ controller/store/environmentLimitJournal.go | 32 +++++++++++++++++++++ controller/store/shareLimitJournal.go | 32 +++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 controller/store/accountLimitJournal.go create mode 100644 controller/store/environmentLimitJournal.go create mode 100644 controller/store/shareLimitJournal.go diff --git a/controller/store/accountLimitJournal.go b/controller/store/accountLimitJournal.go new file mode 100644 index 00000000..3760dc03 --- /dev/null +++ b/controller/store/accountLimitJournal.go @@ -0,0 +1,32 @@ +package store + +import ( + "github.com/jmoiron/sqlx" + "github.com/pkg/errors" +) + +type AccountLimitJournal struct { + Model + AccountId int + Action string +} + +func (self *Store) CreateAccountLimitJournal(j *AccountLimitJournal, tx *sqlx.Tx) (int, error) { + stmt, err := tx.Prepare("insert into account_limit_journal (account_id, action) values ($1, $2) returning id") + if err != nil { + return 0, errors.Wrap(err, "error preparing account_limit_journal insert statement") + } + var id int + if err := stmt.QueryRow(j.AccountId, j.AccountId).Scan(&id); err != nil { + return 0, errors.Wrap(err, "error executing account_limit_journal insert statement") + } + return id, nil +} + +func (self *Store) FindLatestAccountJournal(acctId int, tx *sqlx.Tx) (*AccountLimitJournal, error) { + j := &AccountLimitJournal{} + if err := tx.QueryRowx("select * from account_limit_journal where account_id = $1", acctId).StructScan(j); err != nil { + return nil, errors.Wrap(err, "error finding account_limit_journal by account_id") + } + return j, nil +} diff --git a/controller/store/environmentLimitJournal.go b/controller/store/environmentLimitJournal.go new file mode 100644 index 00000000..c721022c --- /dev/null +++ b/controller/store/environmentLimitJournal.go @@ -0,0 +1,32 @@ +package store + +import ( + "github.com/jmoiron/sqlx" + "github.com/pkg/errors" +) + +type EnvironmentLimitJournal struct { + Model + EnvironmentId int + Action string +} + +func (self *Store) CreateEnvironmentLimitJournal(j *EnvironmentLimitJournal, tx *sqlx.Tx) (int, error) { + stmt, err := tx.Prepare("insert into environment_limit_journal (environment_id, action) values ($1, $2) returning id") + if err != nil { + return 0, errors.Wrap(err, "error preparing environment_limit_journal insert statement") + } + var id int + if err := stmt.QueryRow(j.EnvironmentId, j.Action).Scan(&id); err != nil { + return 0, errors.Wrap(err, "error executing environment_limit_journal insert statement") + } + return id, nil +} + +func (self *Store) FindLatestEnvironmentLimitJournal(envId int, tx *sqlx.Tx) (*EnvironmentLimitJournal, error) { + j := &EnvironmentLimitJournal{} + if err := tx.QueryRowx("select * from environment_limit_journal where environment_id = $1", envId).StructScan(j); err != nil { + return nil, errors.Wrap(err, "error finding environment_limit_journal by environment_id") + } + return j, nil +} diff --git a/controller/store/shareLimitJournal.go b/controller/store/shareLimitJournal.go new file mode 100644 index 00000000..f8ac92ea --- /dev/null +++ b/controller/store/shareLimitJournal.go @@ -0,0 +1,32 @@ +package store + +import ( + "github.com/jmoiron/sqlx" + "github.com/pkg/errors" +) + +type ShareLimitJournal struct { + Model + ShareId int + Action string +} + +func (self *Store) CreateShareLimitJournal(j *ShareLimitJournal, tx *sqlx.Tx) (int, error) { + stmt, err := tx.Prepare("insert into share_limit_journal (share_id, action) values ($1, $2) returning id") + if err != nil { + return 0, errors.Wrap(err, "error preparing share_limit_journal insert statement") + } + var id int + if err := stmt.QueryRow(j.ShareId, j.Action).Scan(&id); err != nil { + return 0, errors.Wrap(err, "error executing share_limit_journal insert statement") + } + return id, nil +} + +func (self *Store) FindLatestShareLimitJournal(shrId int, tx *sqlx.Tx) (*ShareLimitJournal, error) { + j := &ShareLimitJournal{} + if err := tx.QueryRowx("select * from share_limit_journal where share_id = $1", shrId).StructScan(j); err != nil { + return nil, errors.Wrap(err, "error finding share_limit_journal by share_id") + } + return j, nil +}