feat: task storage

This commit is contained in:
dragonmuffin 2024-08-18 18:51:28 +05:00
parent a5e464d4a7
commit 2504e278a3
3 changed files with 96 additions and 0 deletions

View file

@ -0,0 +1,8 @@
package models
type Task struct {
Id *int32 `db:"id"`
ContestId *int32 `db:"contest_id"`
TestgroupId *int32 `db:"testgroup_id"`
TaskId *int32 `db:"task_id"`
}

View file

@ -40,6 +40,9 @@ RETURNING id
problem.TimeLimit, problem.TimeLimit,
problem.MemoryLimit problem.MemoryLimit
) )
if err != nil {
return 0, storage.HandlePgErr(err)
}
for _,tgd := testGroupData { for _,tgd := testGroupData {
query := tx.Rebind(` query := tx.Rebind(`
INSERT INTO testgroups INSERT INTO testgroups

85
internal/storage/task.go Normal file
View file

@ -0,0 +1,85 @@
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
}