From e63e831374a6264553fd3a1e19f3c3691dbb4bf7 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 29 Jul 2022 13:27:00 -0400 Subject: [PATCH] store updates (#10) --- controller/store/account.go | 8 ++++ controller/store/service.go | 64 +++++++++++++++++++++++++++++++ controller/store/sql/000_base.sql | 7 ++-- 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 controller/store/service.go diff --git a/controller/store/account.go b/controller/store/account.go index bc322703..ebb76085 100644 --- a/controller/store/account.go +++ b/controller/store/account.go @@ -28,6 +28,14 @@ func (self *Store) CreateAccount(a *Account, tx *sqlx.Tx) (int, error) { return int(id), nil } +func (self *Store) GetAccount(id int, tx *sqlx.Tx) (*Account, error) { + a := &Account{} + if err := tx.QueryRowx("select * from accounts where id = ?", id).StructScan(a); err != nil { + return nil, errors.Wrap(err, "error selecting account by id") + } + return a, nil +} + func (self *Store) FindAccountWithToken(token string, tx *sqlx.Tx) (*Account, error) { a := &Account{} if err := tx.QueryRowx("select * from accounts where token = ?", token).StructScan(a); err != nil { diff --git a/controller/store/service.go b/controller/store/service.go new file mode 100644 index 00000000..8212f73e --- /dev/null +++ b/controller/store/service.go @@ -0,0 +1,64 @@ +package store + +import ( + "github.com/jmoiron/sqlx" + "github.com/pkg/errors" +) + +type Service struct { + Model + AccountId int + ZitiId string +} + +func (self *Store) CreateService(accountId int, svc *Service, tx *sqlx.Tx) (int, error) { + stmt, err := tx.Prepare("insert into services (account_id, ziti_id) values (?, ?)") + if err != nil { + return 0, errors.Wrap(err, "error preparing services insert statement") + } + res, err := stmt.Exec(accountId, svc.ZitiId) + if err != nil { + return 0, errors.Wrap(err, "error executing services insert statement") + } + id, err := res.LastInsertId() + if err != nil { + return 0, errors.Wrap(err, "error retrieving last services insert id") + } + return int(id), nil +} + +func (self *Store) GetService(id int, tx *sqlx.Tx) (*Service, error) { + svc := &Service{} + if err := tx.QueryRowx("select * from services where id = ?", id).StructScan(svc); err != nil { + return nil, errors.Wrap(err, "error selecting service by id") + } + return svc, nil +} + +func (self *Store) FindServicesForAccount(accountId int, tx *sqlx.Tx) ([]*Service, error) { + rows, err := tx.Queryx("select services.* from services where account_id = ?", accountId) + if err != nil { + return nil, errors.Wrap(err, "error selecting services by account id") + } + var svcs []*Service + for rows.Next() { + svc := &Service{} + if err := rows.StructScan(svc); err != nil { + return nil, errors.Wrap(err, "error scanning service") + } + svcs = append(svcs, svc) + } + return svcs, nil +} + +func (self *Store) DeleteService(id int, tx *sqlx.Tx) error { + stmt, err := tx.Prepare("delete from services where id = ?") + if err != nil { + return errors.Wrap(err, "error preparing services delete statement") + } + _, err = stmt.Exec(id) + if err != nil { + return errors.Wrap(err, "error executing services delete statement") + } + return nil +} diff --git a/controller/store/sql/000_base.sql b/controller/store/sql/000_base.sql index 9aa7569e..fc2c6622 100644 --- a/controller/store/sql/000_base.sql +++ b/controller/store/sql/000_base.sql @@ -21,9 +21,10 @@ create table accounts ( -- create table services ( id integer primary key, - name string not null unique, + account_id integer constraint fk_accounts_service references accounts on delete cascade, + ziti_id string not null unique, created_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')), updated_at datetime not null default(strftime('%Y-%m-%d %H:%M:%f', 'now')), - - constraint chk_name check (name <> '') + + constraint chk_name check (ziti_id <> '') ); \ No newline at end of file