Skip to content

Commit

Permalink
fix: mentor and student registration endpoints check if the mentor/st…
Browse files Browse the repository at this point in the history
…udent already exists (#100)
  • Loading branch information
harshkhandeparkar committed Jun 14, 2023
1 parent 74c75b5 commit efd1205
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions controllers/mentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ func MentorReg(req map[string]interface{}, r *http.Request) (interface{}, int) {
db := utils.GetDB()
defer db.Close()

mentor := models.Mentor{}
db.
Table("mentors").
Where("username = ?", req["username"].(string)).
First(&mentor)

mentor_exists := mentor.Username == req["username"].(string)

if mentor_exists {
utils.LogWarn(
r,
"Mentor already exists.",
)

return "Error: mentor already exists", 400
}

err := db.Create(&models.Mentor{
Name: req["name"].(string),
Email: req["email"].(string),
Expand Down
17 changes: 17 additions & 0 deletions controllers/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ func StudentReg(req map[string]interface{}, r *http.Request) (interface{}, int)
db := utils.GetDB()
defer db.Close()

student := models.Student{}
db.
Table("students").
Where("username = ?", req["username"].(string)).
First(&student)

student_exists := student.Username == req["username"].(string)

if student_exists {
utils.LogWarn(
r,
"Student already exists.",
)

return "Error: student already exists", 400
}

err := db.Create(&models.Student{
Name: req["name"].(string),
Email: req["email"].(string),
Expand Down

0 comments on commit efd1205

Please sign in to comment.