Skip to content

Commit 1622998

Browse files
committed
feat: more info when end exam
1 parent 13a660a commit 1622998

File tree

8 files changed

+83
-8
lines changed

8 files changed

+83
-8
lines changed

backend/apis/exam/driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ func AddDriverPunishment(c *fiber.Ctx) (err error) {
7474
if err != nil {
7575
return
7676
}
77-
if !user.IsPassed {
78-
return common.BadRequest("You don't have the driver's license")
77+
if !user.ValidateDriver() {
78+
return common.Forbidden("You don't have the driver's license")
7979
}
8080
if user.Point <= addPunishmentRequest.Score {
8181
user.Point = 0

backend/apis/exam/exam.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package exam
22

33
import (
4+
"fmt"
5+
. "src/models"
6+
"time"
7+
48
"github.com/gofiber/fiber/v2"
59
"github.com/opentreehole/go-common"
610
"gorm.io/gorm"
7-
. "src/models"
8-
"time"
911
)
1012

1113
// ListExams @ListExams
@@ -146,6 +148,8 @@ func EndExam(ctx *fiber.Ctx) (err error) {
146148
if err != nil {
147149
return
148150
}
151+
isDriver := false
152+
info := "Oops, something went wrong"
149153
var endExamRequest EndExamRequest
150154
if err = ctx.BodyParser(&endExamRequest); err != nil {
151155
return common.BadRequest("Invalid request body")
@@ -169,6 +173,7 @@ func EndExam(ctx *fiber.Ctx) (err error) {
169173
return
170174
}
171175
if exam.Score != 100 {
176+
info = "Sorry, you didn't pass the exam"
172177
return
173178
}
174179
var user User
@@ -177,14 +182,27 @@ func EndExam(ctx *fiber.Ctx) (err error) {
177182
return
178183
}
179184
if user.IsPassed {
185+
info = "Congratulations! You have passed the exam"
180186
return
181187
}
188+
if user.Age < 18 {
189+
info = "Congratulations! You have passed the exam. However, you are not eligible for a driver's license because you are " + fmt.Sprintf("%d", user.Age) + " years old"
190+
return
191+
}
192+
info = "Congratulations! You have passed the exam and obtained the driver's license"
193+
isDriver = true
182194
user.IsPassed = true
183195
user.Point = 12
184196
return DB.Model(&user).Select("IsPassed", "Point").UpdateColumns(&user).Error
185197
})
198+
if err != nil {
199+
return
200+
}
186201
var endExamResponse = EndExamResponse{
187-
Score: exam.Score,
202+
Score: exam.Score,
203+
IsPassed: exam.Score == 100,
204+
IsDriver: isDriver,
205+
Info: info,
188206
}
189207
return ctx.JSON(endExamResponse)
190208
}

backend/apis/exam/schemas.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ type EndExamRequest struct {
2424
}
2525

2626
type EndExamResponse struct {
27-
Score int `json:"score"`
27+
Score int `json:"score"`
28+
IsPassed bool `json:"is_passed"`
29+
IsDriver bool `json:"is_driver"`
30+
Info string `json:"info"`
2831
}
2932

3033
type AddExamRequest struct {

backend/docs/docs.go

+9
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,15 @@ const docTemplate = `{
16021602
"exam.EndExamResponse": {
16031603
"type": "object",
16041604
"properties": {
1605+
"info": {
1606+
"type": "string"
1607+
},
1608+
"is_driver": {
1609+
"type": "boolean"
1610+
},
1611+
"is_passed": {
1612+
"type": "boolean"
1613+
},
16051614
"score": {
16061615
"type": "integer"
16071616
}

backend/docs/swagger.json

+9
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,15 @@
15941594
"exam.EndExamResponse": {
15951595
"type": "object",
15961596
"properties": {
1597+
"info": {
1598+
"type": "string"
1599+
},
1600+
"is_driver": {
1601+
"type": "boolean"
1602+
},
1603+
"is_passed": {
1604+
"type": "boolean"
1605+
},
15971606
"score": {
15981607
"type": "integer"
15991608
}

backend/docs/swagger.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ definitions:
120120
type: object
121121
exam.EndExamResponse:
122122
properties:
123+
info:
124+
type: string
125+
is_driver:
126+
type: boolean
127+
is_passed:
128+
type: boolean
123129
score:
124130
type: integer
125131
type: object

backend/models/exam.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
type Exam struct {
99
ID int `json:"id" gorm:"primaryKey"`
10-
Title string `json:"title" gorm:"default:'exam'"`
11-
Description string `json:"description" gorm:"default:'description'"`
10+
Title string `json:"title" gorm:"default:'default exam title'"`
11+
Description string `json:"description" gorm:"default:'default exam description'"`
1212
StartTime *MyTime `json:"start_time"`
1313
EndTime *MyTime `json:"end_time"`
1414
Duration time.Duration `json:"duration"`

backend/models/user.go

+30
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,33 @@ func DeleteUserByID(id int) error {
100100
return nil
101101
})
102102
}
103+
104+
func ValidateDriver(id int) (bool, error) {
105+
var user User
106+
err := DB.Take(&user, id).Error
107+
if err != nil {
108+
return false, err
109+
}
110+
return user.Age >= 18 && user.IsPassed, nil
111+
}
112+
113+
func (user *User) AddPunishment(score int) error {
114+
return DB.Transaction(func(tx *gorm.DB) error {
115+
var user User
116+
err := tx.Take(&user, user.ID).Error
117+
if err != nil {
118+
return err
119+
}
120+
if user.Point <= score {
121+
user.Point = 0
122+
user.IsPassed = false
123+
} else {
124+
user.Point -= score
125+
}
126+
return tx.Model(&user).Select("Point", "IsPassed").Updates(&user).Error
127+
})
128+
}
129+
130+
func (user *User) ValidateDriver() bool {
131+
return user.Age >= 18 && user.IsPassed
132+
}

0 commit comments

Comments
 (0)