working commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package maindb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"mstore/app/descr"
|
||||
)
|
||||
|
||||
func (db *Database) InsertGrant(ctx context.Context, grant *descr.Grant) error {
|
||||
var err error
|
||||
request := `INSERT INTO grants(id, account_id, operation, created_at)
|
||||
VALUES ($1, $2, $3, $4)`
|
||||
_, err = db.db.Exec(request, grant.ID, grant.AccountID, grant.Operation, grant.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) ListGrantsByAccountID(ctx context.Context, accountID int64) ([]descr.Grant, error) {
|
||||
var err error
|
||||
request := `SELECT * FROM grants WHERE account_id = $1`
|
||||
res := make([]descr.Grant, 0)
|
||||
err = db.db.Select(&res, request, accountID)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (db *Database) ListGrants(ctx context.Context) ([]descr.Grant, error) {
|
||||
var err error
|
||||
request := `SELECT * FROM grants`
|
||||
res := make([]descr.Grant, 0)
|
||||
err = db.db.Select(&res, request)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (db *Database) GetGrant(ctx context.Context, accountID int64, operation string) (bool, *descr.Grant, error) {
|
||||
var err error
|
||||
res := &descr.Grant{}
|
||||
request := `SELECT * FROM grants WHERE account_id = $1 AND operation = $2 LIMIT 1`
|
||||
dbRes := make([]descr.Grant, 0)
|
||||
err = db.db.Select(&dbRes, request, accountID, operation)
|
||||
if err != nil {
|
||||
return false, res, err
|
||||
}
|
||||
if len(dbRes) == 0 {
|
||||
return false, res, err
|
||||
|
||||
}
|
||||
res = &dbRes[0]
|
||||
return true, res, err
|
||||
}
|
||||
|
||||
func (db *Database) DeleteGrantByAccountID(ctx context.Context, grantID int64, operation string) error {
|
||||
var err error
|
||||
request := `DELETE FROM grants WHERE account_id = $1 AND operation = $2`
|
||||
_, err = db.db.Exec(request, grantID, operation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *Database) DeleteAllGrantsForAccountID(ctx context.Context, grantID int64) error {
|
||||
var err error
|
||||
request := `DELETE FROM grants WHERE account_id = $1`
|
||||
_, err = db.db.Exec(request, grantID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -22,7 +22,7 @@ const schema = `
|
||||
created_by VARCHAR(255) NOT NULL,
|
||||
updated_by VARCHAR(255) NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS file_index
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS files_index
|
||||
ON files(collection, name);
|
||||
|
||||
--- DROP TABLE IF EXISTS manifests;
|
||||
@@ -38,7 +38,7 @@ const schema = `
|
||||
created_by VARCHAR(255) NOT NULL,
|
||||
updated_by VARCHAR(255) NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS manifest_index
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS manifests_index
|
||||
ON manifests(name, reference);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS blobs (
|
||||
@@ -57,7 +57,7 @@ const schema = `
|
||||
ON blobs(name, reference, digest);
|
||||
|
||||
--- DROP TABLE IF EXISTS accounts;
|
||||
CREATE TABLE IF NOT EXISTS account (
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
id INT NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
passhash TEXT NOT NULL,
|
||||
@@ -65,22 +65,21 @@ const schema = `
|
||||
updated_at TEXT NOT NULL,
|
||||
disabled BOOL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS account_index01
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS accounts_index01
|
||||
ON accounts(id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS account_index02
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS accounts_index02
|
||||
ON accounts(username);
|
||||
|
||||
|
||||
--- DROP TABLE IF EXISTS grants;
|
||||
CREATE TABLE IF NOT EXISTS grant (
|
||||
CREATE TABLE IF NOT EXISTS grants (
|
||||
id INT NOT NULL,
|
||||
account_id INT NOT NULL,
|
||||
operation TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS grant_index01
|
||||
CREATE INDEX IF NOT EXISTS grants_index01
|
||||
ON grants(account_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS grant_index02
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS grants_index02
|
||||
ON grants(account_id, operation);
|
||||
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user