Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhinav-055 authored Sep 27, 2024
2 parents e7a1bf2 + 6ebe8aa commit 8d6ef7c
Show file tree
Hide file tree
Showing 28 changed files with 1,206 additions and 240 deletions.
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Simple Makefile for a Go project

include .env
# Build the application
all: build

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

migrate:
@echo "Generating migration diff..."
atlas migrate diff cookoff_backend \
atlas migrate diff $(name) \
--dir "$(MIGRATIONS_DIR)" \
--to "$(SCHEMA_FILE)" \
--dev-url "$(DEV_URL)"


.PHONY: all build run test clean watch generate
.PHONY: all build run test clean watch generate
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Create "questions" table
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"));
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"));
-- Create "testcases" table
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);
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);
-- Create "users" table
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"));
-- Create "submissions" table
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);
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);
4 changes: 4 additions & 0 deletions database/migrations/20240926192436.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Modify "submissions" table
ALTER TABLE "public"."submissions" DROP COLUMN "testcase_id", ALTER COLUMN "memory" TYPE numeric;
-- Create "submission_results" table
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);
5 changes: 3 additions & 2 deletions database/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
h1:sfG3u9dWuQiUXQ0Fia9dOLA6w4mncdKU2PNaX0NqGCk=
20240923132455_cookoff_backend.sql h1:31jiDjFKi7AywbVApUvBQeL5Fd8+cXxLzIUypIYB2dw=
h1:SfrlwGx8g4V5VkQK9l6EfwFchAwCMGWIUQS/ZHvWXS8=
20240926184603.sql h1:wPT86iP7DBa4YxmbZDqwieI8GAecgGAw2JyG9S82ngI=
20240926192436.sql h1:b5lQ7n1SlrGlBZV7y+HeCOZKbjkj3A8Q79iP+s73t74=
11 changes: 5 additions & 6 deletions database/queries/questions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ SELECT * FROM questions
WHERE id = $1 LIMIT 1;

-- name: GetQuestions :many
SELECT id, description, title, input_format, points, round, constraints, output_format
FROM questions;
SELECT * FROM questions;

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

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

-- name: UpdateQuestion :exec
UPDATE questions
SET description = $1, title = $2, input_format = $3, points = $4, round = $5, constraints = $6, output_format = $7
WHERE id = $8;
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
WHERE id = $11;

-- name: GetQuestionByRound :many
SELECT * FROM questions
Expand Down
55 changes: 52 additions & 3 deletions database/queries/submission.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@ WHERE question_id = $1

-- name: UpdateSubmission :exec
UPDATE submissions
SET testcases_passed = $1, testcases_failed = $2, runtime = $3, memory = $4
WHERE id = $5;
SET
runtime = $1,
memory = $2,
status = $3,
testcases_passed = $4,
testcases_failed = $5
WHERE id = $6;

-- name: UpdateSubmissionStatus :exec
UPDATE submissions
SET status = $1
WHERE id = $2;

-- name: UpdateDescriptionStatus :exec
UPDATE submissions
SET description = $1
WHERE id = $2;

-- name: GetSubmission :one
SELECT
Expand All @@ -26,4 +41,38 @@ WHERE
SELECT q.round, q.title, q.description, s.*
FROM submissions s
INNER JOIN questions q ON s.question_id = q.id
WHERE s.user_id = $1;
WHERE s.user_id = $1;

-- name: GetSubmissionByID :one
SELECT
id,
question_id,
testcases_passed,
testcases_failed,
runtime,
memory,
submission_time,
description,
user_id
FROM submissions
WHERE id = $1;

-- name: GetSubmissionStatusByID :one
SELECT
status
FROM submissions
WHERE id = $1;

-- name: GetSubmissionResultsBySubmissionID :many
SELECT
id,
testcase_id,
submission_id,
runtime,
memory,
status,
description
FROM
submission_results
WHERE
submission_id = $1;
11 changes: 11 additions & 0 deletions database/queries/submission_status.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- name: CreateSubmissionStatus :exec
INSERT INTO submission_results (id, submission_id, testcase_id ,status ,runtime, memory, description)
VALUES ($1, $2, $3, $4, $5, $6, $7);

-- name: GetStatsForFinalSubEntry :many
SELECT
runtime,
memory,
status
FROM submission_results
WHERE submission_id = $1;
6 changes: 5 additions & 1 deletion database/queries/testcases.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ SELECT
question_id,
runtime
FROM testcases
WHERE question_id = $1;
WHERE question_id = $1;

-- name: GetPublicTestCasesByQuestion :many
SELECT * FROM testcases
WHERE question_id = $1 AND hidden = false;
54 changes: 34 additions & 20 deletions database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ CREATE TABLE users (

CREATE TABLE questions (
id UUID NOT NULL UNIQUE,
description TEXT,
title TEXT,
input_format TEXT,
points INTEGER,
description TEXT NOT NULL,
title TEXT NOT NULL,
input_format TEXT[],
points INTEGER NOT NULL,
round INTEGER NOT NULL,
constraints TEXT,
output_format TEXT,
constraints TEXT[] NOT NULL,
output_format TEXT[] NOT NULL,
sample_test_input TEXT[],
sample_test_output TEXT[],
explanation TEXT[],
PRIMARY KEY(id)
);

Expand All @@ -30,39 +33,50 @@ CREATE TABLE submissions (
testcases_failed INTEGER DEFAULT 0,
runtime DECIMAL,
submission_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
testcase_id UUID,
language_id INTEGER NOT NULL,
description TEXT,
memory INTEGER,
memory NUMERIC,
user_id UUID,
status TEXT,
PRIMARY KEY(id)
);


CREATE TABLE submission_results (
id UUID NOT NULL UNIQUE,
testcase_id UUID,
submission_id UUID NOT NULL,
runtime DECIMAL NOT NULL,
memory NUMERIC NOT NULL,
status TEXT NOT NULL,
description TEXT,
PRIMARY KEY(id),
FOREIGN KEY(submission_id) REFERENCES submissions(id)
ON UPDATE NO ACTION ON DELETE CASCADE
);

CREATE TABLE testcases (
id UUID NOT NULL UNIQUE,
expected_output TEXT NOT NULL,
memory TEXT NOT NULL,
input TEXT NOT NULL,
hidden BOOLEAN NOT NULL,
runtime DECIMAL NOT NULL,
expected_output TEXT NOT NULL ,
memory NUMERIC NOT NULL ,
input TEXT NOT NULL ,
hidden BOOLEAN NOT NULL ,
runtime DECIMAL NOT NULL ,
question_id UUID NOT NULL,
PRIMARY KEY(id)
);



-- Foreign keys
ALTER TABLE submissions
ADD FOREIGN KEY(question_id) REFERENCES questions(id)
ON UPDATE NO ACTION ON DELETE NO ACTION;
ON UPDATE NO ACTION ON DELETE CASCADE;

ALTER TABLE testcases
ADD FOREIGN KEY(question_id) REFERENCES questions(id)
ON UPDATE NO ACTION ON DELETE NO ACTION;

ALTER TABLE submissions
ADD FOREIGN KEY(testcase_id) REFERENCES testcases(id)
ON UPDATE NO ACTION ON DELETE NO ACTION;
ON UPDATE NO ACTION ON DELETE CASCADE;

ALTER TABLE submissions
ADD FOREIGN KEY(user_id) REFERENCES users(id)
ON UPDATE NO ACTION ON DELETE NO ACTION;
ON UPDATE NO ACTION ON DELETE CASCADE;
28 changes: 28 additions & 0 deletions internal/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,31 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
"data": data,
})
}

func Logout(w http.ResponseWriter, r *http.Request) {
jwt, err := r.Cookie("jwt")
if err != nil && !errors.Is(err, http.ErrNoCookie) {
httphelpers.WriteError(w, http.StatusBadRequest, err.Error())
return
}

refresh, err := r.Cookie("refresh_token")
if err != nil && !errors.Is(err, http.ErrNoCookie) {
httphelpers.WriteError(w, http.StatusBadRequest, err.Error())
return
}

if jwt != nil {
jwt.MaxAge = -1
http.SetCookie(w, jwt)
}

if refresh != nil {
refresh.MaxAge = -1
http.SetCookie(w, refresh)
}

httphelpers.WriteJSON(w, http.StatusOK, map[string]any{
"message": "logged out successfully",
})
}
2 changes: 1 addition & 1 deletion internal/controllers/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ type Data struct {
const TypeProcessSubmission = "submission:process"

func CallbackUrl(w http.ResponseWriter, r *http.Request, taskClient *asynq.Client) {
log.Println("Callback URL hit")
var data Data
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
log.Println("Error decoding JSON: ", err)
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
log.Println("Callback URL hit")

payload, err := json.Marshal(data)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions internal/controllers/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ func MeHandler(w http.ResponseWriter, r *http.Request) {
return
}

submissions, err := database.Queries.GetSubmissionsWithRoundByUserId(r.Context(), uuid.NullUUID{UUID: id, Valid: true})
submissions, err := database.Queries.GetSubmissionsWithRoundByUserId(
r.Context(),
uuid.NullUUID{UUID: id, Valid: true},
)
if err != nil {
logger.Errof("Failed to get submissions: %v", err)
httphelpers.WriteError(w, http.StatusInternalServerError, "Failed to get submissions")
return
}

var submissionsByRound = make(map[string][]DashboardSubmission)
submissionsByRound := make(map[string][]DashboardSubmission)

for _, submission := range submissions {
round := fmt.Sprint(submission.Round)
submissionsByRound[round] = append(submissionsByRound[round], DashboardSubmission{
Title: *submission.Title,
Description: *submission.Description,
Title: submission.Title,
Description: submission.Description,
Score: 0,
})
}
Expand Down
Loading

0 comments on commit 8d6ef7c

Please sign in to comment.