ms-tester/internal/storage/solution.go

115 lines
3.5 KiB
Go
Raw Normal View History

2024-08-17 12:23:35 +00:00
package storage
import (
"context"
2024-08-20 22:54:46 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/models"
2024-08-17 12:23:35 +00:00
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)
type SolutionStorage struct {
db *sqlx.DB
logger *zap.Logger
}
func NewSolutionStorage(db *sqlx.DB, logger *zap.Logger) *SolutionStorage {
return &SolutionStorage{
db: db,
logger: logger,
}
}
2024-08-18 12:29:52 +00:00
// TODO: testing graph
2024-08-20 22:54:46 +00:00
func (storage *SolutionStorage) updateResult(ctx context.Context, participantId int32, taskId int32) error {
tx, err := storage.db.Beginx()
if err != nil {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
}
2024-08-20 22:54:46 +00:00
query := tx.Rebind("UPDATE participant_subtask AS ps SET best_score = (SELECT COALESCE(max(score),0) FROM subtaskruns WHERE subtask_id = ps.subtask_id AND solution_id IN (SELECT id FROM solutions WHERE participant_id=ps.participant_id)) WHERE participant_id = 2 AND subtask_id IN (SELECT id FROM subtasks WHERE task_id = 2)")
tx.QueryxContext(ctx, query, participantId, taskId)
query = tx.Rebind("UPDATE participant_task SET best_score = (select max(best_score) from participant_subtask WHERE participant_id = ? AND subtask_id IN (SELECT id FROM subtask WHERE task_id = ?)) WHERE participant_id = ? AND task_id = ?")
tx.QueryxContext(ctx, query, participantId, taskId, participantId, taskId)
err = tx.Commit()
if err != nil {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
}
2024-08-20 22:54:46 +00:00
return nil
}
2024-08-17 12:23:35 +00:00
func (storage *SolutionStorage) CreateSolution(ctx context.Context, solution *models.Solution) (int32, error) {
query := storage.db.Rebind(`
INSERT INTO solutions
(participant_id,task_id,language_id,solution_hash,result)
VALUES (?, ?, ?, ?, ?)
RETURNING id
`)
rows, err := storage.db.QueryxContext(
ctx,
query,
solution.ParticipantId,
solution.TaskId,
solution.LanguageId,
2024-08-20 22:54:46 +00:00
"", //FIXME
2024-08-17 12:23:35 +00:00
models.NotTested,
)
2024-08-20 22:54:46 +00:00
//TODO: add testing tree
2024-08-17 12:23:35 +00:00
if err != nil {
2024-08-20 22:54:46 +00:00
return 0, handlePgErr(err)
2024-08-17 12:23:35 +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-17 12:23:35 +00:00
}
return id, nil
}
func (storage *SolutionStorage) ReadSolutionById(ctx context.Context, id int32) (*models.Solution, error) {
var solution models.Solution
query := storage.db.Rebind("SELECT * from solutions WHERE id=? LIMIT 1")
err := storage.db.GetContext(ctx, &solution, query, id)
if err != nil {
2024-08-20 22:54:46 +00:00
return nil, handlePgErr(err)
2024-08-17 12:23:35 +00:00
}
return &solution, nil
}
func (storage *SolutionStorage) RejudgeSolution(ctx context.Context, id int32) error {
2024-08-20 22:54:46 +00:00
tx, err := storage.db.Beginx()
if err != nil {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
}
2024-08-20 22:54:46 +00:00
query := tx.Rebind("UPDATE solutions SET result = ? WHERE id = ?")
2024-08-22 22:56:03 +00:00
tx.QueryxContext(ctx, query, models.NotTested, id) // FIXME
2024-08-20 22:54:46 +00:00
query = tx.Rebind("UPDATE subtaskruns SET result = ?,score = 0 WHERE solution_id = ?")
2024-08-22 22:56:03 +00:00
tx.QueryxContext(ctx, query, models.NotTested, id) // FIXME
2024-08-20 22:54:46 +00:00
query = tx.Rebind("UPDATE testruns SET result = ?, score = 0 WHERE testgrouprun_id IN (SELECT id FROM tesgrouprun WHERE solution_id = ?)")
2024-08-22 22:56:03 +00:00
tx.QueryxContext(ctx, query, models.NotTested, id) // FIXME
2024-08-20 22:54:46 +00:00
err = tx.Commit()
2024-08-18 12:29:52 +00:00
var solution models.Solution
2024-08-20 22:54:46 +00:00
query = storage.db.Rebind("SELECT * from solutions WHERE id=? LIMIT 1")
err = storage.db.GetContext(ctx, &solution, query, id)
2024-08-17 13:41:37 +00:00
if err != nil {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
2024-08-17 13:41:37 +00:00
}
2024-08-22 22:56:03 +00:00
storage.updateResult(ctx, *solution.ParticipantId, *solution.TaskId) // FIXME
return nil
}
2024-08-17 12:23:35 +00:00
func (storage *SolutionStorage) DeleteSolution(ctx context.Context, id int32) error {
2024-08-17 12:40:33 +00:00
query := storage.db.Rebind("DELETE FROM solutions WHERE id=?")
2024-08-17 12:23:35 +00:00
_, err := storage.db.ExecContext(ctx, query, id)
if err != nil {
2024-08-20 22:54:46 +00:00
return handlePgErr(err)
2024-08-17 12:23:35 +00:00
}
return nil
}