2024-10-09 18:55:16 +00:00
|
|
|
package repository
|
2024-08-18 13:51:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-05 16:05:29 +00:00
|
|
|
"git.sch9.ru/new_gate/models"
|
2024-08-18 13:51:28 +00:00
|
|
|
"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,
|
2024-08-20 22:54:46 +00:00
|
|
|
task.PositionName,
|
2024-08-18 13:51:28 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return 0, handlePgErr(err)
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
var id int32
|
|
|
|
err = rows.StructScan(&id)
|
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return 0, handlePgErr(err)
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-08-20 22:54:46 +00:00
|
|
|
return nil, handlePgErr(err)
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
|
|
|
return &task, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 22:54:46 +00:00
|
|
|
func (storage *TaskStorage) UpdateTask(ctx context.Context, id int32, task models.Task) error {
|
2024-08-18 13:51:28 +00:00
|
|
|
query := storage.db.Rebind("UPDATE tasks SET position=?,position_name=?")
|
2024-08-20 22:54:46 +00:00
|
|
|
_, err := storage.db.ExecContext(ctx, query, task.Position, task.PositionName)
|
2024-08-18 13:51:28 +00:00
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return handlePgErr(err)
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
2024-08-20 22:54:46 +00:00
|
|
|
return nil
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-08-20 22:54:46 +00:00
|
|
|
return handlePgErr(err)
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|