86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"git.sch9.ru/new_gate/ms-auth/internal/lib"
|
|
"git.sch9.ru/new_gate/ms-auth/internal/models"
|
|
"github.com/jackc/pgerrcode"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jmoiron/sqlx"
|
|
"go.uber.org/zap"
|
|
"time"
|
|
)
|
|
|
|
type TaskStorage struct {
|
|
db *sqlx.DB
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func NewTaskStorage(db *sqlx.DB, logger *zap.Logger) *TaskStorage {
|
|
return &TaskStorage{
|
|
db: db,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (storage *TaskStorage) CreateTask(ctx context.Context, task *models.Task) (int32, error) {
|
|
query := storage.db.Rebind(`
|
|
INSERT INTO tasks
|
|
(contest_id,problem_id,position,position_name)
|
|
VALUES (?, ?, ?, ?)
|
|
RETURNING id
|
|
`)
|
|
|
|
rows, err := storage.db.QueryxContext(
|
|
ctx,
|
|
query,
|
|
task.ContestId,
|
|
task.ProblemId,
|
|
task.Position,
|
|
task.PositionName
|
|
)
|
|
if err != nil {
|
|
return 0, storage.HandlePgErr(err)
|
|
}
|
|
|
|
defer rows.Close()
|
|
var id int32
|
|
err = rows.StructScan(&id)
|
|
if err != nil {
|
|
return 0, storage.HandlePgErr(err)
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
func (storage *TaskStorage) ReadTaskById(ctx context.Context, id int32) (*models.Task, error) {
|
|
var task models.Task
|
|
query := storage.db.Rebind("SELECT * from tasks WHERE id=? LIMIT 1")
|
|
err := storage.db.GetContext(ctx, &task, query, id)
|
|
if err != nil {
|
|
return nil, storage.HandlePgErr(err)
|
|
}
|
|
return &task, nil
|
|
}
|
|
|
|
func (storage *TaskStorage) UpdateTask(ctx context.Context, id int32,task models.Task) error {
|
|
query := storage.db.Rebind("UPDATE tasks SET position=?,position_name=?")
|
|
_, err := storage.db.ExecContext(ctx, query, task.Position,task.PositionName)
|
|
if err != nil {
|
|
return storage.HandlePgErr(err)
|
|
}
|
|
}
|
|
|
|
func (storage *TaskStorage) DeleteTask(ctx context.Context, id int32) error {
|
|
query := storage.db.Rebind("DELETE FROM tasks WHERE id=?")
|
|
_, err := storage.db.ExecContext(ctx, query, id)
|
|
if err != nil {
|
|
return storage.HandlePgErr(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|