Skip to content

Commit 8d6ef7c

Browse files
authored
Merge branch 'master' into master
2 parents e7a1bf2 + 6ebe8aa commit 8d6ef7c

28 files changed

Lines changed: 1206 additions & 240 deletions

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# Simple Makefile for a Go project
2-
1+
include .env
32
# Build the application
43
all: build
54

6-
DB_URL=postgres://username:password@localhost:5432/database_name?sslmode=disable
5+
DB_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/${POSTGRES_DB}?sslmode=disable
76
DEV_URL=docker://postgres/15/dev
87
SCHEMA_FILE=file://database/schema.sql
98
MIGRATIONS_DIR=file://database/migrations
@@ -63,10 +62,10 @@ apply-schema:
6362

6463
migrate:
6564
@echo "Generating migration diff..."
66-
atlas migrate diff cookoff_backend \
65+
atlas migrate diff $(name) \
6766
--dir "$(MIGRATIONS_DIR)" \
6867
--to "$(SCHEMA_FILE)" \
6968
--dev-url "$(DEV_URL)"
7069

7170

72-
.PHONY: all build run test clean watch generate
71+
.PHONY: all build run test clean watch generate
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- Create "questions" table
2-
CREATE TABLE "public"."questions" ("id" uuid NOT NULL, "description" text NULL, "title" text NULL, "input_format" text NULL, "points" integer NULL, "round" integer NOT NULL, "constraints" text NULL, "output_format" text NULL, PRIMARY KEY ("id"));
2+
CREATE TABLE "public"."questions" ("id" uuid NOT NULL, "description" text NOT NULL, "title" text NOT NULL, "input_format" text[] NULL, "points" integer NOT NULL, "round" integer NOT NULL, "constraints" text[] NOT NULL, "output_format" text[] NOT NULL, "sample_test_input" text[] NULL, "sample_test_output" text[] NULL, "explanation" text[] NULL, PRIMARY KEY ("id"));
33
-- Create "testcases" table
4-
CREATE TABLE "public"."testcases" ("id" uuid NOT NULL, "expected_output" text NULL, "memory" text NULL, "input" text NULL, "hidden" boolean NULL, "runtime" numeric NULL, "question_id" uuid NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "testcases_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."questions" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION);
4+
CREATE TABLE "public"."testcases" ("id" uuid NOT NULL, "expected_output" text NOT NULL, "memory" numeric NOT NULL, "input" text NOT NULL, "hidden" boolean NOT NULL, "runtime" numeric NOT NULL, "question_id" uuid NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "testcases_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."questions" ("id") ON UPDATE NO ACTION ON DELETE CASCADE);
55
-- Create "users" table
66
CREATE TABLE "public"."users" ("id" uuid NOT NULL, "email" text NOT NULL, "reg_no" text NOT NULL, "password" text NOT NULL, "role" text NOT NULL, "round_qualified" integer NOT NULL DEFAULT 0, "score" integer NULL DEFAULT 0, "name" text NOT NULL, PRIMARY KEY ("id"), CONSTRAINT "users_email_key" UNIQUE ("email"), CONSTRAINT "users_reg_no_key" UNIQUE ("reg_no"));
77
-- Create "submissions" table
8-
CREATE TABLE "public"."submissions" ("id" uuid NOT NULL, "question_id" uuid NOT NULL, "testcases_passed" integer NULL DEFAULT 0, "testcases_failed" integer NULL DEFAULT 0, "runtime" numeric NULL, "submission_time" timestamp NULL DEFAULT CURRENT_TIMESTAMP, "testcase_id" uuid NULL, "language_id" integer NOT NULL, "description" text NULL, "memory" integer NULL, "user_id" uuid NULL, "status" text NULL, PRIMARY KEY ("id"), CONSTRAINT "submissions_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."questions" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "submissions_testcase_id_fkey" FOREIGN KEY ("testcase_id") REFERENCES "public"."testcases" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT "submissions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION);
8+
CREATE TABLE "public"."submissions" ("id" uuid NOT NULL, "question_id" uuid NOT NULL, "testcases_passed" integer NULL DEFAULT 0, "testcases_failed" integer NULL DEFAULT 0, "runtime" numeric NULL, "submission_time" timestamp NULL DEFAULT CURRENT_TIMESTAMP, "testcase_id" uuid NULL, "language_id" integer NOT NULL, "description" text NULL, "memory" integer NULL, "user_id" uuid NULL, "status" text NULL, PRIMARY KEY ("id"), CONSTRAINT "submissions_question_id_fkey" FOREIGN KEY ("question_id") REFERENCES "public"."questions" ("id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "submissions_testcase_id_fkey" FOREIGN KEY ("testcase_id") REFERENCES "public"."testcases" ("id") ON UPDATE NO ACTION ON DELETE CASCADE, CONSTRAINT "submissions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "public"."users" ("id") ON UPDATE NO ACTION ON DELETE CASCADE);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Modify "submissions" table
2+
ALTER TABLE "public"."submissions" DROP COLUMN "testcase_id", ALTER COLUMN "memory" TYPE numeric;
3+
-- Create "submission_results" table
4+
CREATE TABLE "public"."submission_results" ("id" uuid NOT NULL, "submission_id" uuid NOT NULL, "runtime" numeric NOT NULL, "memory" numeric NOT NULL, "description" text NULL, PRIMARY KEY ("id"), CONSTRAINT "submission_results_submission_id_fkey" FOREIGN KEY ("submission_id") REFERENCES "public"."submissions" ("id") ON UPDATE NO ACTION ON DELETE CASCADE);

database/migrations/atlas.sum

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
h1:sfG3u9dWuQiUXQ0Fia9dOLA6w4mncdKU2PNaX0NqGCk=
2-
20240923132455_cookoff_backend.sql h1:31jiDjFKi7AywbVApUvBQeL5Fd8+cXxLzIUypIYB2dw=
1+
h1:SfrlwGx8g4V5VkQK9l6EfwFchAwCMGWIUQS/ZHvWXS8=
2+
20240926184603.sql h1:wPT86iP7DBa4YxmbZDqwieI8GAecgGAw2JyG9S82ngI=
3+
20240926192436.sql h1:b5lQ7n1SlrGlBZV7y+HeCOZKbjkj3A8Q79iP+s73t74=

database/queries/questions.sql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ SELECT * FROM questions
33
WHERE id = $1 LIMIT 1;
44

55
-- name: GetQuestions :many
6-
SELECT id, description, title, input_format, points, round, constraints, output_format
7-
FROM questions;
6+
SELECT * FROM questions;
87

98
-- name: CreateQuestion :one
10-
INSERT INTO questions (id, description, title, "input_format", points, round, constraints, output_format)
11-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
9+
INSERT INTO questions (id, description, title, "input_format", points, round, constraints, output_format, sample_test_input, sample_test_output, explanation)
10+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
1211
RETURNING *;
1312

1413
-- name: DeleteQuestion :exec
@@ -17,8 +16,8 @@ WHERE id = $1;
1716

1817
-- name: UpdateQuestion :exec
1918
UPDATE questions
20-
SET description = $1, title = $2, input_format = $3, points = $4, round = $5, constraints = $6, output_format = $7
21-
WHERE id = $8;
19+
SET description = $1, title = $2, input_format = $3, points = $4, round = $5, constraints = $6, output_format = $7, sample_test_input = $8, sample_test_output = $9, explanation = $10
20+
WHERE id = $11;
2221

2322
-- name: GetQuestionByRound :many
2423
SELECT * FROM questions

database/queries/submission.sql

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,23 @@ WHERE question_id = $1
1010

1111
-- name: UpdateSubmission :exec
1212
UPDATE submissions
13-
SET testcases_passed = $1, testcases_failed = $2, runtime = $3, memory = $4
14-
WHERE id = $5;
13+
SET
14+
runtime = $1,
15+
memory = $2,
16+
status = $3,
17+
testcases_passed = $4,
18+
testcases_failed = $5
19+
WHERE id = $6;
20+
21+
-- name: UpdateSubmissionStatus :exec
22+
UPDATE submissions
23+
SET status = $1
24+
WHERE id = $2;
25+
26+
-- name: UpdateDescriptionStatus :exec
27+
UPDATE submissions
28+
SET description = $1
29+
WHERE id = $2;
1530

1631
-- name: GetSubmission :one
1732
SELECT
@@ -26,4 +41,38 @@ WHERE
2641
SELECT q.round, q.title, q.description, s.*
2742
FROM submissions s
2843
INNER JOIN questions q ON s.question_id = q.id
29-
WHERE s.user_id = $1;
44+
WHERE s.user_id = $1;
45+
46+
-- name: GetSubmissionByID :one
47+
SELECT
48+
id,
49+
question_id,
50+
testcases_passed,
51+
testcases_failed,
52+
runtime,
53+
memory,
54+
submission_time,
55+
description,
56+
user_id
57+
FROM submissions
58+
WHERE id = $1;
59+
60+
-- name: GetSubmissionStatusByID :one
61+
SELECT
62+
status
63+
FROM submissions
64+
WHERE id = $1;
65+
66+
-- name: GetSubmissionResultsBySubmissionID :many
67+
SELECT
68+
id,
69+
testcase_id,
70+
submission_id,
71+
runtime,
72+
memory,
73+
status,
74+
description
75+
FROM
76+
submission_results
77+
WHERE
78+
submission_id = $1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- name: CreateSubmissionStatus :exec
2+
INSERT INTO submission_results (id, submission_id, testcase_id ,status ,runtime, memory, description)
3+
VALUES ($1, $2, $3, $4, $5, $6, $7);
4+
5+
-- name: GetStatsForFinalSubEntry :many
6+
SELECT
7+
runtime,
8+
memory,
9+
status
10+
FROM submission_results
11+
WHERE submission_id = $1;

database/queries/testcases.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ SELECT
5353
question_id,
5454
runtime
5555
FROM testcases
56-
WHERE question_id = $1;
56+
WHERE question_id = $1;
57+
58+
-- name: GetPublicTestCasesByQuestion :many
59+
SELECT * FROM testcases
60+
WHERE question_id = $1 AND hidden = false;

database/schema.sql

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ CREATE TABLE users (
1313

1414
CREATE TABLE questions (
1515
id UUID NOT NULL UNIQUE,
16-
description TEXT,
17-
title TEXT,
18-
input_format TEXT,
19-
points INTEGER,
16+
description TEXT NOT NULL,
17+
title TEXT NOT NULL,
18+
input_format TEXT[],
19+
points INTEGER NOT NULL,
2020
round INTEGER NOT NULL,
21-
constraints TEXT,
22-
output_format TEXT,
21+
constraints TEXT[] NOT NULL,
22+
output_format TEXT[] NOT NULL,
23+
sample_test_input TEXT[],
24+
sample_test_output TEXT[],
25+
explanation TEXT[],
2326
PRIMARY KEY(id)
2427
);
2528

@@ -30,39 +33,50 @@ CREATE TABLE submissions (
3033
testcases_failed INTEGER DEFAULT 0,
3134
runtime DECIMAL,
3235
submission_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
33-
testcase_id UUID,
3436
language_id INTEGER NOT NULL,
3537
description TEXT,
36-
memory INTEGER,
38+
memory NUMERIC,
3739
user_id UUID,
3840
status TEXT,
3941
PRIMARY KEY(id)
4042
);
4143

44+
45+
CREATE TABLE submission_results (
46+
id UUID NOT NULL UNIQUE,
47+
testcase_id UUID,
48+
submission_id UUID NOT NULL,
49+
runtime DECIMAL NOT NULL,
50+
memory NUMERIC NOT NULL,
51+
status TEXT NOT NULL,
52+
description TEXT,
53+
PRIMARY KEY(id),
54+
FOREIGN KEY(submission_id) REFERENCES submissions(id)
55+
ON UPDATE NO ACTION ON DELETE CASCADE
56+
);
57+
4258
CREATE TABLE testcases (
4359
id UUID NOT NULL UNIQUE,
44-
expected_output TEXT NOT NULL,
45-
memory TEXT NOT NULL,
46-
input TEXT NOT NULL,
47-
hidden BOOLEAN NOT NULL,
48-
runtime DECIMAL NOT NULL,
60+
expected_output TEXT NOT NULL ,
61+
memory NUMERIC NOT NULL ,
62+
input TEXT NOT NULL ,
63+
hidden BOOLEAN NOT NULL ,
64+
runtime DECIMAL NOT NULL ,
4965
question_id UUID NOT NULL,
5066
PRIMARY KEY(id)
5167
);
5268

69+
70+
5371
-- Foreign keys
5472
ALTER TABLE submissions
5573
ADD FOREIGN KEY(question_id) REFERENCES questions(id)
56-
ON UPDATE NO ACTION ON DELETE NO ACTION;
74+
ON UPDATE NO ACTION ON DELETE CASCADE;
5775

5876
ALTER TABLE testcases
5977
ADD FOREIGN KEY(question_id) REFERENCES questions(id)
60-
ON UPDATE NO ACTION ON DELETE NO ACTION;
61-
62-
ALTER TABLE submissions
63-
ADD FOREIGN KEY(testcase_id) REFERENCES testcases(id)
64-
ON UPDATE NO ACTION ON DELETE NO ACTION;
78+
ON UPDATE NO ACTION ON DELETE CASCADE;
6579

6680
ALTER TABLE submissions
6781
ADD FOREIGN KEY(user_id) REFERENCES users(id)
68-
ON UPDATE NO ACTION ON DELETE NO ACTION;
82+
ON UPDATE NO ACTION ON DELETE CASCADE;

internal/controllers/auth.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,31 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
153153
"data": data,
154154
})
155155
}
156+
157+
func Logout(w http.ResponseWriter, r *http.Request) {
158+
jwt, err := r.Cookie("jwt")
159+
if err != nil && !errors.Is(err, http.ErrNoCookie) {
160+
httphelpers.WriteError(w, http.StatusBadRequest, err.Error())
161+
return
162+
}
163+
164+
refresh, err := r.Cookie("refresh_token")
165+
if err != nil && !errors.Is(err, http.ErrNoCookie) {
166+
httphelpers.WriteError(w, http.StatusBadRequest, err.Error())
167+
return
168+
}
169+
170+
if jwt != nil {
171+
jwt.MaxAge = -1
172+
http.SetCookie(w, jwt)
173+
}
174+
175+
if refresh != nil {
176+
refresh.MaxAge = -1
177+
http.SetCookie(w, refresh)
178+
}
179+
180+
httphelpers.WriteJSON(w, http.StatusOK, map[string]any{
181+
"message": "logged out successfully",
182+
})
183+
}

0 commit comments

Comments
 (0)