Skip to content

Commit 2324f6b

Browse files
committed
fix
1 parent e0bbe4a commit 2324f6b

File tree

10 files changed

+280
-75
lines changed

10 files changed

+280
-75
lines changed

backend/apis/exam/exam.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func ListExams(ctx *fiber.Ctx) (err error) {
3333
}
3434

3535
// GetExam @GetExam
36-
// @Router /api/exams [get]
36+
// @Router /api/exams/{id} [get]
3737
// @Summary Get exam by ID
3838
// @Description Get exam by ID
3939
// @Tags Exam
@@ -90,8 +90,8 @@ func AddExam(ctx *fiber.Ctx) (err error) {
9090
UserID: tmpUser.ID,
9191
Title: addExamRequest.Title,
9292
Description: addExamRequest.Description,
93-
StartTime: addExamRequest.StartTime,
94-
EndTime: addExamRequest.EndTime,
93+
StartTime: &addExamRequest.StartTime,
94+
EndTime: &addExamRequest.EndTime,
9595
Duration: addExamRequest.EndTime.Time.Sub(addExamRequest.StartTime.Time),
9696
Score: addExamRequest.Score,
9797
}
@@ -114,12 +114,19 @@ func StartExam(ctx *fiber.Ctx) (err error) {
114114
if err != nil {
115115
return
116116
}
117-
return DB.Create(&Exam{
117+
exam := Exam{
118118
UserID: tmpUser.ID,
119-
StartTime: MyTime{
119+
StartTime: &MyTime{
120120
Time: time.Now(),
121121
},
122-
}).Error
122+
}
123+
err = DB.Omit("EndTime").Create(&exam).Error
124+
if err != nil {
125+
return
126+
}
127+
return ctx.JSON(StartExamResponse{
128+
ID: exam.ID,
129+
})
123130
}
124131

125132
// EndExam @EndExam
@@ -129,6 +136,7 @@ func StartExam(ctx *fiber.Ctx) (err error) {
129136
// @Tags Exam
130137
// @Accept json
131138
// @Produce json
139+
// @Param json body EndExamRequest true "json"
132140
// @Success 200 {object} EndExamResponse
133141
// @Failure 400 {object} common.HttpError
134142
// @Failure 401 {object} common.HttpError
@@ -150,7 +158,10 @@ func EndExam(ctx *fiber.Ctx) (err error) {
150158
if exam.UserID != tmpUser.ID {
151159
return common.Forbidden("You are not the owner of this exam")
152160
}
153-
exam.EndTime = MyTime{Time: time.Now()}
161+
if exam.IsFinished() {
162+
return common.Forbidden("This exam has already ended")
163+
}
164+
exam.EndTime = &MyTime{Time: time.Now()}
154165
exam.Duration = exam.EndTime.Time.Sub(exam.StartTime.Time)
155166
err = DB.Transaction(func(tx *gorm.DB) (err error) {
156167
err = DB.Model(&exam).Select("EndTime", "Duration").UpdateColumns(&exam).Error
@@ -165,7 +176,7 @@ func EndExam(ctx *fiber.Ctx) (err error) {
165176
if err != nil {
166177
return
167178
}
168-
if !user.IsPassed {
179+
if user.IsPassed {
169180
return
170181
}
171182
user.IsPassed = true

backend/apis/exam/punishment.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func AddPunishment(c *fiber.Ctx) (err error) {
4040
if exam.UserID != tmpUser.ID {
4141
return common.Forbidden("You are not the owner of this exam")
4242
}
43+
if exam.IsFinished() {
44+
return common.Forbidden("The exam has been finished")
45+
}
4346
var punishment = Punishment{
4447
Reason: addPunishmentRequest.Reason,
4548
Score: addPunishmentRequest.Score,
@@ -96,7 +99,7 @@ func ListPunishments(c *fiber.Ctx) (err error) {
9699
}
97100
var punishments Punishments
98101
err = DB.Where("exam_id = ? and type = ?", exam.ID, EXAM).Find(&punishments).Error
99-
return c.JSON(exam.Punishments)
102+
return c.JSON(punishments)
100103
}
101104

102105
// GetPunishment @GetPunishment

backend/apis/exam/route.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func RegisterRoutes(routes fiber.Router) {
1414
routes.Delete("/exams/:id", DeleteExam)
1515

1616
// exam punishment
17-
routes.Get("/exams/punishments/:punishment_id", GetPunishment)
17+
routes.Get("/exams/punishments/:id", GetPunishment)
1818
routes.Post("/exams/:id/punishments/", AddPunishment)
1919
routes.Get("/exams/:id<int>/punishments/", ListPunishments)
2020
//routes.Get("/exams/:id/questions", ListQuestions)

backend/docs/docs.go

+88-24
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const docTemplate = `{
192192
},
193193
"/api/exams": {
194194
"get": {
195-
"description": "Get exam by ID",
195+
"description": "list my exams",
196196
"consumes": [
197197
"application/json"
198198
],
@@ -202,15 +202,8 @@ const docTemplate = `{
202202
"tags": [
203203
"Exam"
204204
],
205-
"summary": "Get exam by ID",
205+
"summary": "list my exams",
206206
"parameters": [
207-
{
208-
"type": "integer",
209-
"description": "Exam ID",
210-
"name": "id",
211-
"in": "path",
212-
"required": true
213-
},
214207
{
215208
"type": "string",
216209
"description": "Bearer和token空格拼接",
@@ -223,7 +216,10 @@ const docTemplate = `{
223216
"200": {
224217
"description": "OK",
225218
"schema": {
226-
"$ref": "#/definitions/models.Exam"
219+
"type": "array",
220+
"items": {
221+
"$ref": "#/definitions/models.Exam"
222+
}
227223
}
228224
},
229225
"400": {
@@ -237,12 +233,6 @@ const docTemplate = `{
237233
"schema": {
238234
"$ref": "#/definitions/common.HttpError"
239235
}
240-
},
241-
"403": {
242-
"description": "Forbidden",
243-
"schema": {
244-
"$ref": "#/definitions/common.HttpError"
245-
}
246236
}
247237
}
248238
}
@@ -314,6 +304,15 @@ const docTemplate = `{
314304
],
315305
"summary": "End exam",
316306
"parameters": [
307+
{
308+
"description": "json",
309+
"name": "json",
310+
"in": "body",
311+
"required": true,
312+
"schema": {
313+
"$ref": "#/definitions/exam.EndExamRequest"
314+
}
315+
},
317316
{
318317
"type": "string",
319318
"description": "Bearer和token空格拼接",
@@ -446,6 +445,61 @@ const docTemplate = `{
446445
}
447446
},
448447
"/api/exams/{id}": {
448+
"get": {
449+
"description": "Get exam by ID",
450+
"consumes": [
451+
"application/json"
452+
],
453+
"produces": [
454+
"application/json"
455+
],
456+
"tags": [
457+
"Exam"
458+
],
459+
"summary": "Get exam by ID",
460+
"parameters": [
461+
{
462+
"type": "integer",
463+
"description": "Exam ID",
464+
"name": "id",
465+
"in": "path",
466+
"required": true
467+
},
468+
{
469+
"type": "string",
470+
"description": "Bearer和token空格拼接",
471+
"name": "Authorization",
472+
"in": "header",
473+
"required": true
474+
}
475+
],
476+
"responses": {
477+
"200": {
478+
"description": "OK",
479+
"schema": {
480+
"$ref": "#/definitions/models.Exam"
481+
}
482+
},
483+
"400": {
484+
"description": "Bad Request",
485+
"schema": {
486+
"$ref": "#/definitions/common.HttpError"
487+
}
488+
},
489+
"401": {
490+
"description": "Unauthorized",
491+
"schema": {
492+
"$ref": "#/definitions/common.HttpError"
493+
}
494+
},
495+
"403": {
496+
"description": "Forbidden",
497+
"schema": {
498+
"$ref": "#/definitions/common.HttpError"
499+
}
500+
}
501+
}
502+
},
449503
"put": {
450504
"description": "Modify exam by ID",
451505
"consumes": [
@@ -1415,6 +1469,14 @@ const docTemplate = `{
14151469
}
14161470
}
14171471
},
1472+
"exam.EndExamRequest": {
1473+
"type": "object",
1474+
"properties": {
1475+
"id": {
1476+
"type": "integer"
1477+
}
1478+
}
1479+
},
14181480
"exam.EndExamResponse": {
14191481
"type": "object",
14201482
"properties": {
@@ -1514,12 +1576,6 @@ const docTemplate = `{
15141576
"description": "IsPublic bool ` + "`" + `json:\"is_public\"` + "`" + `",
15151577
"type": "integer"
15161578
},
1517-
"punishments": {
1518-
"type": "array",
1519-
"items": {
1520-
"$ref": "#/definitions/models.Punishment"
1521-
}
1522-
},
15231579
"score": {
15241580
"type": "integer"
15251581
},
@@ -1649,10 +1705,14 @@ const docTemplate = `{
16491705
1000000000,
16501706
60000000000,
16511707
3600000000000,
1708+
-9223372036854775808,
1709+
9223372036854775807,
16521710
1,
16531711
1000,
16541712
1000000,
1655-
1000000000
1713+
1000000000,
1714+
60000000000,
1715+
3600000000000
16561716
],
16571717
"x-enum-varnames": [
16581718
"minDuration",
@@ -1663,10 +1723,14 @@ const docTemplate = `{
16631723
"Second",
16641724
"Minute",
16651725
"Hour",
1726+
"minDuration",
1727+
"maxDuration",
16661728
"Nanosecond",
16671729
"Microsecond",
16681730
"Millisecond",
1669-
"Second"
1731+
"Second",
1732+
"Minute",
1733+
"Hour"
16701734
]
16711735
},
16721736
"user.ChangePasswordRequest": {

0 commit comments

Comments
 (0)