From 3cd5fdb0d9a0b357881ad19d245eaf151715fcbf Mon Sep 17 00:00:00 2001 From: Harsh Khandeparkar Date: Sat, 18 Nov 2023 15:09:36 +0530 Subject: [PATCH] fix: go is actually worse than i thought --- controllers/project_update.go | 36 +++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/controllers/project_update.go b/controllers/project_update.go index 2531f02..d063b75 100644 --- a/controllers/project_update.go +++ b/controllers/project_update.go @@ -101,36 +101,40 @@ func UpdateProject(w http.ResponseWriter, r *http.Request) { // Attempt to fetch secondary mentor from the database secondaryMentor := models.Mentor{} if reqFields.SecondaryMentorUsername != "" { - tx = db.Table("mentors").Where("username = ?", reqFields.SecondaryMentorUsername).First(&secondaryMentor) - - if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound { + if reqFields.MentorUsername == reqFields.SecondaryMentorUsername { utils.LogErrAndRespond( r, w, err, - fmt.Sprintf("Error fetching secondary mentor `%s`.", reqFields.SecondaryMentorUsername), - http.StatusInternalServerError, + fmt.Sprintf("Error: Secondary mentor `%s` cannot be same as primary mentor.", reqFields.SecondaryMentorUsername), + http.StatusBadRequest, ) return - } else if tx.Error == gorm.ErrRecordNotFound { - utils.LogWarnAndRespond( + } + + tx = db.Table("mentors").Where("username = ?", reqFields.SecondaryMentorUsername).First(&secondaryMentor) + + if tx.Error != nil && err != gorm.ErrRecordNotFound { + utils.LogErrAndRespond( r, w, - fmt.Sprintf("Secondary mentor `%s` does not exist.", reqFields.SecondaryMentorUsername), - http.StatusBadRequest, + err, + fmt.Sprintf("Error fetching secondary mentor `%s`.", reqFields.SecondaryMentorUsername), + http.StatusInternalServerError, ) return } } + secondaryMentorId := int32(secondaryMentor.ID) updatedProj := &models.Project{ - Name: reqFields.Name, - Description: reqFields.Description, - Tags: strings.Join(reqFields.Tags, ","), - RepoLink: reqFields.RepoLink, - CommChannel: reqFields.CommChannel, - ReadmeLink: reqFields.ReadmeLink, - SecondaryMentor: secondaryMentor, + Name: reqFields.Name, + Description: reqFields.Description, + Tags: strings.Join(reqFields.Tags, ","), + RepoLink: reqFields.RepoLink, + CommChannel: reqFields.CommChannel, + ReadmeLink: reqFields.ReadmeLink, + SecondaryMentorId: &secondaryMentorId, } tx = db.