Skip to content

Commit

Permalink
fix: go is actually worse than i thought
Browse files Browse the repository at this point in the history
  • Loading branch information
harshkhandeparkar committed Nov 18, 2023
1 parent 3a3dbbc commit 3cd5fdb
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions controllers/project_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3cd5fdb

Please sign in to comment.