2022-11-17 20:25:35 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Frontend struct {
|
|
|
|
Model
|
2022-12-01 20:56:55 +01:00
|
|
|
EnvironmentId *int
|
2022-11-30 17:52:48 +01:00
|
|
|
Token string
|
2022-11-17 20:25:35 +01:00
|
|
|
ZId string
|
2022-11-28 19:33:59 +01:00
|
|
|
PublicName *string
|
2022-12-01 20:56:55 +01:00
|
|
|
UrlTemplate *string
|
2022-11-29 19:45:23 +01:00
|
|
|
Reserved bool
|
2022-11-17 20:25:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (str *Store) CreateFrontend(envId int, f *Frontend, tx *sqlx.Tx) (int, error) {
|
2022-11-30 17:52:48 +01:00
|
|
|
stmt, err := tx.Prepare("insert into frontends (environment_id, token, z_id, public_name, reserved) values ($1, $2, $3, $4, $5) returning id")
|
2022-11-17 20:25:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing frontends insert statement")
|
|
|
|
}
|
|
|
|
var id int
|
2022-11-30 17:52:48 +01:00
|
|
|
if err := stmt.QueryRow(envId, f.Token, f.ZId, f.PublicName, f.Reserved).Scan(&id); err != nil {
|
2022-11-17 20:25:35 +01:00
|
|
|
return 0, errors.Wrap(err, "error executing frontends insert statement")
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (str *Store) GetFrontend(id int, tx *sqlx.Tx) (*Frontend, error) {
|
|
|
|
i := &Frontend{}
|
|
|
|
if err := tx.QueryRowx("select * from frontends where id = $1", id).StructScan(i); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting frontend by id")
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:38:49 +01:00
|
|
|
func (str *Store) FindFrontendWithToken(token string, tx *sqlx.Tx) (*Frontend, error) {
|
2022-11-17 20:25:35 +01:00
|
|
|
i := &Frontend{}
|
2022-11-30 20:38:49 +01:00
|
|
|
if err := tx.QueryRowx("select frontends.* from frontends where token = $1", token).StructScan(i); err != nil {
|
2022-11-17 20:25:35 +01:00
|
|
|
return nil, errors.Wrap(err, "error selecting frontend by name")
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:55:53 +01:00
|
|
|
func (str *Store) FindFrontendPubliclyNamed(publicName string, tx *sqlx.Tx) (*Frontend, error) {
|
|
|
|
i := &Frontend{}
|
|
|
|
if err := tx.QueryRowx("select frontends.* from frontends where public_name = $1", publicName).StructScan(i); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting frontend by public_name")
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2022-11-17 20:25:35 +01:00
|
|
|
func (str *Store) FindFrontendsForEnvironment(envId int, tx *sqlx.Tx) ([]*Frontend, error) {
|
|
|
|
rows, err := tx.Queryx("select frontends.* from frontends where environment_id = $1", envId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting frontends by environment_id")
|
|
|
|
}
|
|
|
|
var is []*Frontend
|
|
|
|
for rows.Next() {
|
|
|
|
i := &Frontend{}
|
|
|
|
if err := rows.StructScan(i); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error scanning frontend")
|
|
|
|
}
|
|
|
|
is = append(is, i)
|
|
|
|
}
|
|
|
|
return is, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (str *Store) DeleteFrontend(id int, tx *sqlx.Tx) error {
|
|
|
|
stmt, err := tx.Prepare("delete from frontends where id = $1")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error preparing frontends delete statement")
|
|
|
|
}
|
|
|
|
_, err = stmt.Exec(id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error executing frontends delete statement")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|