mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 16:54:23 +01:00
identities (#10)
This commit is contained in:
parent
7fd1287465
commit
409fee1576
64
controller/store/identity.go
Normal file
64
controller/store/identity.go
Normal file
@ -0,0 +1,64 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Identity struct {
|
||||
Model
|
||||
AccountId int
|
||||
ZitiId string
|
||||
}
|
||||
|
||||
func (self *Store) CreateIdentity(accountId int, svc *Service, tx *sqlx.Tx) (int, error) {
|
||||
stmt, err := tx.Prepare("insert into identities (account_id, ziti_id) values (?, ?)")
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "error preparing identities insert statement")
|
||||
}
|
||||
res, err := stmt.Exec(accountId, svc.ZitiId)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "error executing identities insert statement")
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "error retrieving last identities insert id")
|
||||
}
|
||||
return int(id), nil
|
||||
}
|
||||
|
||||
func (self *Store) GetIdentity(id int, tx *sqlx.Tx) (*Identity, error) {
|
||||
i := &Identity{}
|
||||
if err := tx.QueryRowx("select * from identities where id = ?", id).StructScan(i); err != nil {
|
||||
return nil, errors.Wrap(err, "error selecting identity by id")
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (self *Store) FindIdentitiesForAccount(accountId int, tx *sqlx.Tx) ([]*Identity, error) {
|
||||
rows, err := tx.Queryx("select identities.* from identities where account_id = ?", accountId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error selecting identities by account id")
|
||||
}
|
||||
var is []*Identity
|
||||
for rows.Next() {
|
||||
i := &Identity{}
|
||||
if err := rows.StructScan(i); err != nil {
|
||||
return nil, errors.Wrap(err, "error scanning identity")
|
||||
}
|
||||
is = append(is, i)
|
||||
}
|
||||
return is, nil
|
||||
}
|
||||
|
||||
func (self *Store) DeleteIdentity(id int, tx *sqlx.Tx) error {
|
||||
stmt, err := tx.Prepare("delete from identities where id = ?")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error preparing identities delete statement")
|
||||
}
|
||||
_, err = stmt.Exec(id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error executing identities delete statement")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user