the very basic store implementation for limit_classes, applied_limit_classes (#606)

This commit is contained in:
Michael Quigley 2024-05-14 14:16:20 -04:00
parent 75d58e84e4
commit bd5aaa061a
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package store
import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
type AppliedLimitClass struct {
Model
AccountId int
LimitClassId int
}
func (str *Store) ApplyLimitClass(lc *AppliedLimitClass, trx *sqlx.Tx) (int, error) {
stmt, err := trx.Prepare("insert into applied_limit_classes (account_id, limit_class_id) values ($1, $2) returning id")
if err != nil {
return 0, errors.Wrap(err, "error preparing applied_limit_classes insert statement")
}
var id int
if err := stmt.QueryRow(lc.AccountId, lc.LimitClassId).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing applied_limit_classes insert statement")
}
return id, nil
}
func (str *Store) FindLimitClassesForAccount(acctId int, trx *sqlx.Tx) ([]*LimitClass, error) {
rows, err := trx.Queryx("select limit_classes.* from applied_limit_classes, limit_classes where applied_limit_classes.account_id = $1 and applied_limit_classes.limit_class_id = limit_classes.id", acctId)
if err != nil {
return nil, errors.Wrap(err, "error finding limit classes for account")
}
var lcs []*LimitClass
for rows.Next() {
lc := &LimitClass{}
if err := rows.StructScan(&lc); err != nil {
return nil, errors.Wrap(err, "error scanning limit_classes")
}
lcs = append(lcs, lc)
}
return lcs, nil
}

View File

@ -0,0 +1,31 @@
package store
import (
"github.com/jmoiron/sqlx"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/pkg/errors"
)
type LimitClass struct {
Model
LimitScope LimitScope
LimitAction LimitAction
ShareMode sdk.ShareMode
BackendMode sdk.BackendMode
PeriodMinutes int
RxBytes int64
TxBytes int64
TotalBytes int64
}
func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) {
stmt, err := trx.Prepare("insert into limit_classes (limit_scope, limit_action, share_mode, backend_mode, period_minutes, rx_bytes, tx_bytes, total_bytes) values ($1, $2, $3, $4, $5, $6, $7, $8) returning id")
if err != nil {
return 0, errors.Wrap(err, "error preparing limit_classes insert statement")
}
var id int
if err := stmt.QueryRow(lc.LimitScope, lc.LimitAction, lc.ShareMode, lc.BackendMode, lc.PeriodMinutes).Scan(&id); err != nil {
return 0, errors.Wrap(err, "error executing limit_classes insert statement")
}
return id, nil
}