Skip to content

Commit bc8efde

Browse files
committed
chore: Add submission deadline
1 parent bdf20ad commit bc8efde

4 files changed

Lines changed: 66 additions & 43 deletions

File tree

internal/app/handler/contest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func (h *Handler) GetContestByID(c echo.Context) error {
134134

135135
cdetailed.IsParticipant = true
136136

137+
_, deadline := AllowSubmitAt(contest, entry)
138+
cdetailed.SubmissionDeadline = &deadline
139+
137140
statuses, err := h.repo.Submission.GetProblemStatuses(ctx, entry.ID)
138141
if err != nil {
139142
return fmt.Errorf("%s: can't get submissions: %v", op, err)

internal/app/handler/dto/response/response.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ type User struct {
4343
}
4444

4545
type ContestDetailed struct {
46-
ID int32 `json:"id"`
47-
Creator User `json:"creator"`
48-
Title string `json:"title"`
49-
Description string `json:"description"`
50-
StartTime time.Time `json:"start_time"`
51-
EndTime time.Time `json:"end_time"`
52-
DurationMins int32 `json:"duration_mins"`
53-
MaxEntries int32 `json:"max_entries,omitempty"`
54-
Participants int32 `json:"participants"`
55-
AllowLateJoin bool `json:"allow_late_join"`
56-
IsParticipant bool `json:"is_participant,omitempty"`
57-
Problems []ContestProblemListItem `json:"problems"`
58-
CreatedAt time.Time `json:"created_at"`
46+
ID int32 `json:"id"`
47+
Creator User `json:"creator"`
48+
Title string `json:"title"`
49+
Description string `json:"description"`
50+
StartTime time.Time `json:"start_time"`
51+
EndTime time.Time `json:"end_time"`
52+
DurationMins int32 `json:"duration_mins"`
53+
MaxEntries int32 `json:"max_entries,omitempty"`
54+
Participants int32 `json:"participants"`
55+
AllowLateJoin bool `json:"allow_late_join"`
56+
IsParticipant bool `json:"is_participant,omitempty"`
57+
SubmissionDeadline *time.Time `json:"submission_deadline,omitempty"`
58+
Problems []ContestProblemListItem `json:"problems"`
59+
CreatedAt time.Time `json:"created_at"`
5960
}
6061

6162
type ContestListItem struct {
@@ -97,17 +98,18 @@ type Test struct {
9798
}
9899

99100
type ContestProblemDetailed struct {
100-
ID int32 `json:"id"`
101-
Charcode string `json:"charcode"`
102-
ContestID int32 `json:"contest_id"`
103-
Writer User `json:"writer"`
104-
Title string `json:"title"`
105-
Statement string `json:"statement"`
106-
Examples []TC `json:"examples,omitempty"`
107-
Difficulty string `json:"difficulty"`
108-
Status string `json:"status,omitempty"`
109-
TimeLimitMS int32 `json:"time_limit_ms"`
110-
CreatedAt time.Time `json:"created_at"`
101+
ID int32 `json:"id"`
102+
Charcode string `json:"charcode"`
103+
ContestID int32 `json:"contest_id"`
104+
Writer User `json:"writer"`
105+
Title string `json:"title"`
106+
Statement string `json:"statement"`
107+
Examples []TC `json:"examples,omitempty"`
108+
Difficulty string `json:"difficulty"`
109+
Status string `json:"status,omitempty"`
110+
TimeLimitMS int32 `json:"time_limit_ms"`
111+
SubmissionDeadline *time.Time `json:"submission_deadline,omitempty"`
112+
CreatedAt time.Time `json:"created_at"`
111113
}
112114

113115
type ContestProblemListItem struct {

internal/app/handler/problem.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,25 @@ func (h *Handler) GetContestProblem(c echo.Context) error {
167167
return err
168168
}
169169

170+
contest, err := h.repo.Contest.GetByID(ctx, int32(contestID))
171+
if err != nil {
172+
return err
173+
}
174+
175+
_, deadline := AllowSubmitAt(contest, entry)
176+
170177
pdetailed := response.ContestProblemDetailed{
171-
ID: p.ID,
172-
Charcode: p.Charcode,
173-
ContestID: int32(contestID),
174-
Title: p.Title,
175-
Statement: p.Statement,
176-
Examples: examples,
177-
Difficulty: p.Difficulty,
178-
Status: status,
179-
CreatedAt: p.CreatedAt,
180-
TimeLimitMS: p.TimeLimitMS,
178+
ID: p.ID,
179+
Charcode: p.Charcode,
180+
ContestID: int32(contestID),
181+
Title: p.Title,
182+
Statement: p.Statement,
183+
Examples: examples,
184+
Difficulty: p.Difficulty,
185+
Status: status,
186+
CreatedAt: p.CreatedAt,
187+
TimeLimitMS: p.TimeLimitMS,
188+
SubmissionDeadline: &deadline,
181189
Writer: response.User{
182190
ID: p.WriterID,
183191
Username: p.WriterUsername,

internal/app/handler/submission.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/voidcontests/api/internal/app/handler/dto/request"
1313
"github.com/voidcontests/api/internal/app/handler/dto/response"
1414
"github.com/voidcontests/api/internal/lib/logger/sl"
15+
"github.com/voidcontests/api/internal/storage/models"
1516
"github.com/voidcontests/api/internal/storage/models/status"
1617
"github.com/voidcontests/api/pkg/requestid"
1718
"github.com/voidcontests/api/pkg/validate"
@@ -49,15 +50,6 @@ func (h *Handler) CreateSubmission(c echo.Context) error {
4950
return err
5051
}
5152

52-
if contest.StartTime.After(time.Now()) {
53-
return Error(http.StatusForbidden, "contest is not started yet")
54-
}
55-
56-
// TODO: maybe allow to submit solutions after end time if `contest.keep_as_training` is enabled
57-
if contest.EndTime.Before(time.Now()) {
58-
return Error(http.StatusForbidden, "contest alreay ended")
59-
}
60-
6153
entry, err := h.repo.Entry.Get(ctx, int32(contestID), claims.UserID)
6254
if errors.Is(err, pgx.ErrNoRows) {
6355
log.Debug("trying to create submission without entry")
@@ -68,6 +60,12 @@ func (h *Handler) CreateSubmission(c echo.Context) error {
6860
return err
6961
}
7062

63+
now := time.Now()
64+
earliest, deadline := AllowSubmitAt(contest, entry)
65+
if earliest.After(now) || deadline.Before(now) {
66+
return Error(http.StatusForbidden, "submission window is currently closed")
67+
}
68+
7169
problem, err := h.repo.Problem.Get(ctx, int32(contestID), charcode)
7270
if errors.Is(err, pgx.ErrNoRows) {
7371
return Error(http.StatusNotFound, "problem not found")
@@ -254,3 +252,15 @@ func (h *Handler) GetSubmissions(c echo.Context) error {
254252
Items: items,
255253
})
256254
}
255+
256+
func AllowSubmitAt(contest models.Contest, entry models.Entry) (earliest time.Time, deadline time.Time) {
257+
if contest.DurationMins == 0 {
258+
return contest.StartTime, contest.EndTime
259+
}
260+
261+
deadline = entry.CreatedAt.Add(time.Duration(contest.DurationMins) * time.Minute)
262+
if deadline.Before(contest.EndTime) {
263+
return entry.CreatedAt, deadline
264+
}
265+
return entry.CreatedAt, contest.EndTime
266+
}

0 commit comments

Comments
 (0)