skeleton added
This commit is contained in:
42
gates/storage/admin.go
Normal file
42
gates/storage/admin.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"tgVideoCall/domain/admin/models"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
)
|
||||
|
||||
// Пытается получить админа по userID
|
||||
func (p *DB) GetAdmin(ctx context.Context, userID int) (models.Admin, error) {
|
||||
const op = "gates.postgres.GetAdmin"
|
||||
p.log.Debug(op, "Starting retrieving admin, for user: ", userID)
|
||||
|
||||
query := p.sq.Select("user_id", "role").
|
||||
From("admins").
|
||||
Where(sq.Eq{"user_id": userID})
|
||||
|
||||
qry, args, err := query.ToSql()
|
||||
p.log.Debug(op, "Building query: ", qry, "args: ", args)
|
||||
if err != nil {
|
||||
p.log.Error(op, "Error building query: ", err)
|
||||
return models.Admin{}, err
|
||||
}
|
||||
|
||||
var result models.Admin
|
||||
err = p.db.QueryRowxContext(ctx, qry, args...).StructScan(&result)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
p.log.Debug(op, "Admin does not exist, for user: ", userID)
|
||||
return models.Admin{}, models.ErrNotAdmin
|
||||
}
|
||||
if err != nil {
|
||||
p.log.Error(op, "Error retrieving admin: ", err)
|
||||
return models.Admin{}, err
|
||||
}
|
||||
|
||||
p.log.Debug(op, "Successfully retrieved admin for user: ", userID)
|
||||
return result, nil
|
||||
}
|
||||
20
gates/storage/migrations/1_admins.sql
Executable file
20
gates/storage/migrations/1_admins.sql
Executable file
@@ -0,0 +1,20 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE IF NOT EXISTS admins(
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INT NOT NULL UNIQUE,
|
||||
role VARCHAR(50) NOT NULL
|
||||
);
|
||||
|
||||
-- Добавляем первого админа
|
||||
INSERT INTO admins (user_id, role)
|
||||
SELECT '104186268', 'creator'
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM admins WHERE user_id = '104186268'
|
||||
);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS admins;
|
||||
-- +goose StatementEnd
|
||||
20
gates/storage/migrations/1_init.sql
Executable file
20
gates/storage/migrations/1_init.sql
Executable file
@@ -0,0 +1,20 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE IF NOT EXISTS admins(
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INT NOT NULL UNIQUE,
|
||||
role VARCHAR(50) NOT NULL
|
||||
);
|
||||
|
||||
-- Добавляем первого админа
|
||||
INSERT INTO admins (user_id, role)
|
||||
SELECT '104186268', 'creator'
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM admins WHERE user_id = '104186268'
|
||||
);
|
||||
-- +goose StatementEnd1_admins
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS admins;
|
||||
-- +goose StatementEnd
|
||||
30
gates/storage/storage.go
Normal file
30
gates/storage/storage.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/bool64/sqluct"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
db *sqlx.DB
|
||||
sq sq.StatementBuilderType
|
||||
sm sqluct.Mapper
|
||||
log slog.Logger
|
||||
}
|
||||
|
||||
func NewPostgresDB(db *sqlx.DB, log slog.Logger) *DB {
|
||||
dab := DB{
|
||||
db: db,
|
||||
sm: sqluct.Mapper{Dialect: sqluct.DialectPostgres},
|
||||
sq: sq.StatementBuilder.PlaceholderFormat(sq.Dollar),
|
||||
log: log,
|
||||
}
|
||||
_, err := dab.db.Exec("SET timezone TO 'UTC'")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &dab
|
||||
}
|
||||
Reference in New Issue
Block a user