81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"git.sch9.ru/new_gate/models"
|
|
"github.com/jmoiron/sqlx"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
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, handlePgErr(err)
|
|
}
|
|
|
|
defer rows.Close()
|
|
var id int32
|
|
err = rows.StructScan(&id)
|
|
if err != nil {
|
|
return 0, 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, 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 handlePgErr(err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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 handlePgErr(err)
|
|
}
|
|
|
|
return nil
|
|
}
|