diff --git a/internal/models/result.go b/internal/models/result.go index 75b4cff..59f6e50 100644 --- a/internal/models/result.go +++ b/internal/models/result.go @@ -5,13 +5,13 @@ import "time" type Result int32 const ( - NotTested Result = 0 - Testing Result = 1 - Accepted Result = 2 + Testing Result = 0 + NotTested Result = 1 + SystemFailDuringTesting Result = 2 TimeLimitExceeded Result = 3 MemoryLimitExceeded Result = 4 CompilationError Result = 5 - SystemFailDuringTesting Result = 6 + Accepted Result = 6 ) func (result Result) Valid() error { diff --git a/internal/storage/solution.go b/internal/storage/solution.go index 2f03558..b995d6a 100644 --- a/internal/storage/solution.go +++ b/internal/storage/solution.go @@ -24,9 +24,18 @@ func NewSolutionStorage(db *sqlx.DB, logger *zap.Logger) *SolutionStorage { } } -//func updateResult(ctx context.Context, participant_id int32, task_id int32) error { -// -//} +func updateResult(ctx context.Context, participantId int32, taskId int32) error { + tx,err := storage.db.Begin() + if err != nil { + return storage.HandlePgErr(err) + } + tx.Exec("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)",participantId,taskId) + tx.Exec("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 = ?",participantId,taskId,participantId,taskId) + err = tx.Commit() + if err != nil { + return storage.HandlePgErr(err) + } +} func (storage *SolutionStorage) CreateSolution(ctx context.Context, solution *models.Solution) (int32, error) { query := storage.db.Rebind(` diff --git a/migrations/20240727123308_initial.sql b/migrations/20240727123308_initial.sql index 3ed82cc..bd75bba 100644 --- a/migrations/20240727123308_initial.sql +++ b/migrations/20240727123308_initial.sql @@ -153,19 +153,21 @@ CREATE INDEX ON tests USING BTREE (testgroup_id); -CREATE TABLE IF NOT EXISTS testgroupruns +CREATE TABLE IF NOT EXISTS subtaskruns ( id serial NOT NULL, - testgroup_id INT REFERENCES testgroups ON DELETE CASCADE, + subtask_id INT REFERENCES subtasks ON DELETE CASCADE, solution_id INT REFERENCES solutions ON DELETE CASCADE, result INT NOT NULL, + score INT NOT NULL, + UNIQUE (subtask_id,solution_id), PRIMARY KEY (id) ); -CREATE INDEX ON testgroupruns USING BTREE (id); -CREATE INDEX ON testgroupruns USING BTREE (result); -CREATE INDEX ON testgroupruns USING BTREE (solution_id); +CREATE INDEX ON subtaskruns USING BTREE (id); +CREATE INDEX ON subtaskruns USING BTREE (result); +CREATE INDEX ON subtaskruns USING BTREE (solution_id); @@ -175,7 +177,7 @@ CREATE TABLE IF NOT EXISTS testruns id serial NOT NULL, test_id INT REFERENCES tests ON DELETE CASCADE, --solution_id INT REFERENCES solutions ON DELETE CASCADE, - testgrouprun_id INT REFERENCES testgroupruns ON DELETE CASCADE, + subtaskrun_id INT REFERENCES subtaskruns ON DELETE CASCADE, result INT NOT NULL, PRIMARY KEY (id) @@ -183,7 +185,7 @@ CREATE TABLE IF NOT EXISTS testruns CREATE INDEX ON testruns USING BTREE (id); CREATE INDEX ON testruns USING BTREE (result); -CREATE INDEX ON testruns USING BTREE (testgrouprun_id); +CREATE INDEX ON testruns USING BTREE (subtaskrun_id); @@ -192,7 +194,7 @@ CREATE TABLE IF NOT EXISTS participant_subtask ( participant_id INT REFERENCES participants ON DELETE CASCADE, subtask_id INT REFERENCES subtasks ON DELETE CASCADE, - result INT NOT NULL, + --result INT NOT NULL, best_score INT NOT NULL ); @@ -203,7 +205,7 @@ CREATE TABLE IF NOT EXISTS participant_task ( participant_id INT REFERENCES participants ON DELETE CASCADE, task_id INT REFERENCES tasks ON DELETE CASCADE, - result INT NOT NULL, + --result INT NOT NULL, best_score INT NOT NULL, penalty INT NOT NULL ); @@ -211,8 +213,8 @@ CREATE TABLE IF NOT EXISTS participant_task CREATE FUNCTION on_new_participant() RETURNS TRIGGER AS $$ BEGIN --RAISE NOTICE 'NEW.ID:%, NEW.contest_id:%', NEW.id,NEW.contest_id; - INSERT INTO participant_task (participant_id,task_id,result,best_score,penalty) SELECT NEW.id,id,0,0,0 FROM tasks WHERE contest_id=NEW.contest_id; - INSERT INTO participant_subtask (participant_id,subtask_id,result,best_score) SELECT NEW.id,id,0,0 FROM subtasks WHERE contest_id=NEW.contest_id; + INSERT INTO participant_task (participant_id,task_id,best_score,penalty) SELECT NEW.id,id,0,0 FROM tasks WHERE contest_id=NEW.contest_id; + INSERT INTO participant_subtask (participant_id,subtask_id,best_score) SELECT NEW.id,id,0 FROM subtasks WHERE contest_id=NEW.contest_id; RETURN NEW; END; $$ LANGUAGE plpgsql; @@ -220,7 +222,7 @@ CREATE TRIGGER new_participant_trg AFTER INSERT ON participants FOR EACH ROW EXE CREATE FUNCTION on_new_task() RETURNS TRIGGER AS $$ BEGIN - INSERT INTO participant_task (participant_id,task_id,result,best_score,penalty) SELECT id,NEW.id,0,0,0 FROM participants WHERE contest_id=NEW.contest_id; + INSERT INTO participant_task (participant_id,task_id,best_score,penalty) SELECT id,NEW.id,0,0 FROM participants WHERE contest_id=NEW.contest_id; INSERT INTO subtasks(contest_id,testgroup_id,task_id) SELECT NEW.contest_id,id,NEW.id FROM testgroups WHERE problem_id = NEW.problem_id; RETURN NEW; END; $$ LANGUAGE plpgsql; @@ -229,7 +231,7 @@ CREATE TRIGGER new_task_trg AFTER INSERT ON tasks FOR EACH ROW EXECUTE FUNCTION CREATE FUNCTION on_new_subtask() RETURNS TRIGGER AS $$ BEGIN - INSERT INTO participant_subtask (participant_id,subtask_id,result,best_score) SELECT id,NEW.id,0,0 FROM participants WHERE contest_id=NEW.contest_id; + INSERT INTO participant_subtask (participant_id,subtask_id,best_score) SELECT id,NEW.id,0 FROM participants WHERE contest_id=NEW.contest_id; RETURN NEW; END; $$ LANGUAGE plpgsql; @@ -263,7 +265,7 @@ DROP TABLE IF EXISTS solutions CASCADE; DROP TABLE IF EXISTS languages CASCADE; DROP TABLE IF EXISTS testgroups CASCADE; DROP TABLE IF EXISTS testruns CASCADE; -DROP TABLE IF EXISTS testgroupruns CASCADE; +DROP TABLE IF EXISTS subtaskruns CASCADE; DROP TABLE IF EXISTS problems CASCADE; DROP TABLE IF EXISTS contests CASCADE; DROP TABLE IF EXISTS tasks CASCADE;