2023-01-04 19:13:50 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Share struct {
|
|
|
|
Model
|
|
|
|
EnvironmentId int
|
|
|
|
ZId string
|
|
|
|
Token string
|
|
|
|
ShareMode string
|
|
|
|
BackendMode string
|
|
|
|
FrontendSelection *string
|
|
|
|
FrontendEndpoint *string
|
|
|
|
BackendProxyEndpoint *string
|
|
|
|
Reserved bool
|
2024-03-01 17:17:39 +01:00
|
|
|
PermissionMode PermissionMode
|
2023-03-09 21:08:59 +01:00
|
|
|
Deleted bool
|
2023-01-04 19:13:50 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) CreateShare(envId int, shr *Share, tx *sqlx.Tx) (int, error) {
|
2024-03-01 17:17:39 +01:00
|
|
|
stmt, err := tx.Prepare("insert into shares (environment_id, z_id, token, share_mode, backend_mode, frontend_selection, frontend_endpoint, backend_proxy_endpoint, reserved, permission_mode) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) returning id")
|
2023-01-04 19:13:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing shares insert statement")
|
|
|
|
}
|
|
|
|
var id int
|
2024-03-01 17:17:39 +01:00
|
|
|
if err := stmt.QueryRow(envId, shr.ZId, shr.Token, shr.ShareMode, shr.BackendMode, shr.FrontendSelection, shr.FrontendEndpoint, shr.BackendProxyEndpoint, shr.Reserved, shr.PermissionMode).Scan(&id); err != nil {
|
2023-01-04 19:13:50 +01:00
|
|
|
return 0, errors.Wrap(err, "error executing shares insert statement")
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) GetShare(id int, tx *sqlx.Tx) (*Share, error) {
|
2023-01-04 19:13:50 +01:00
|
|
|
shr := &Share{}
|
|
|
|
if err := tx.QueryRowx("select * from shares where id = $1", id).StructScan(shr); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting share by id")
|
|
|
|
}
|
|
|
|
return shr, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindAllShares(tx *sqlx.Tx) ([]*Share, error) {
|
2023-03-10 17:16:37 +01:00
|
|
|
rows, err := tx.Queryx("select * from shares where not deleted order by id")
|
2023-01-04 19:13:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting all shares")
|
|
|
|
}
|
|
|
|
var shrs []*Share
|
|
|
|
for rows.Next() {
|
|
|
|
shr := &Share{}
|
|
|
|
if err := rows.StructScan(shr); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error scanning share")
|
|
|
|
}
|
|
|
|
shrs = append(shrs, shr)
|
|
|
|
}
|
|
|
|
return shrs, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindShareWithToken(shrToken string, tx *sqlx.Tx) (*Share, error) {
|
2023-01-04 19:13:50 +01:00
|
|
|
shr := &Share{}
|
2023-03-10 17:16:37 +01:00
|
|
|
if err := tx.QueryRowx("select * from shares where token = $1 and not deleted", shrToken).StructScan(shr); err != nil {
|
2023-01-04 19:13:50 +01:00
|
|
|
return nil, errors.Wrap(err, "error selecting share by token")
|
|
|
|
}
|
|
|
|
return shr, nil
|
|
|
|
}
|
|
|
|
|
2024-02-15 20:08:28 +01:00
|
|
|
func (str *Store) ShareWithTokenExists(shrToken string, tx *sqlx.Tx) (bool, error) {
|
|
|
|
count := 0
|
|
|
|
if err := tx.QueryRowx("select count(0) from shares where token = $1 and not deleted", shrToken).Scan(&count); err != nil {
|
|
|
|
return true, errors.Wrap(err, "error selecting share count by token")
|
|
|
|
}
|
|
|
|
return count > 0, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindShareWithZIdAndDeleted(zId string, tx *sqlx.Tx) (*Share, error) {
|
2023-03-07 21:27:39 +01:00
|
|
|
shr := &Share{}
|
2023-03-10 17:33:06 +01:00
|
|
|
if err := tx.QueryRowx("select * from shares where z_id = $1", zId).StructScan(shr); err != nil {
|
2023-03-07 21:27:39 +01:00
|
|
|
return nil, errors.Wrap(err, "error selecting share by z_id")
|
|
|
|
}
|
|
|
|
return shr, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) FindSharesForEnvironment(envId int, tx *sqlx.Tx) ([]*Share, error) {
|
2023-03-10 17:16:37 +01:00
|
|
|
rows, err := tx.Queryx("select shares.* from shares where environment_id = $1 and not deleted", envId)
|
2023-01-04 19:13:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting shares by environment id")
|
|
|
|
}
|
|
|
|
var shrs []*Share
|
|
|
|
for rows.Next() {
|
|
|
|
shr := &Share{}
|
|
|
|
if err := rows.StructScan(shr); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error scanning share")
|
|
|
|
}
|
|
|
|
shrs = append(shrs, shr)
|
|
|
|
}
|
|
|
|
return shrs, nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) UpdateShare(shr *Share, tx *sqlx.Tx) error {
|
2024-03-01 17:17:39 +01:00
|
|
|
sql := "update shares set z_id = $1, token = $2, share_mode = $3, backend_mode = $4, frontend_selection = $5, frontend_endpoint = $6, backend_proxy_endpoint = $7, reserved = $8, permission_mode = $9, updated_at = current_timestamp where id = $10"
|
2023-01-04 19:13:50 +01:00
|
|
|
stmt, err := tx.Prepare(sql)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error preparing shares update statement")
|
|
|
|
}
|
2024-03-01 17:17:39 +01:00
|
|
|
_, err = stmt.Exec(shr.ZId, shr.Token, shr.ShareMode, shr.BackendMode, shr.FrontendSelection, shr.FrontendEndpoint, shr.BackendProxyEndpoint, shr.Reserved, shr.PermissionMode, shr.Id)
|
2023-01-04 19:13:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error executing shares update statement")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 20:17:27 +01:00
|
|
|
func (str *Store) DeleteShare(id int, tx *sqlx.Tx) error {
|
2023-03-10 17:16:37 +01:00
|
|
|
stmt, err := tx.Prepare("update shares set updated_at = current_timestamp, deleted = true where id = $1")
|
2023-01-04 19:13:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error preparing shares delete statement")
|
|
|
|
}
|
|
|
|
_, err = stmt.Exec(id)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error executing shares delete statement")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|