From f04b5af91561c311d5b12fbc202613c8fd2cccb3 Mon Sep 17 00:00:00 2001 From: Rohan Barsagade Date: Mon, 27 Nov 2023 00:36:17 +0530 Subject: [PATCH 1/2] fix: editable college and email fields Co-authored-by: shikharish --- controllers/oauth.go | 9 ++++++- controllers/student.go | 58 ++++++++++++++++++++++++++++++++++++++++++ server/routes.go | 6 +++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/controllers/oauth.go b/controllers/oauth.go index 09c01b5..1b3c293 100644 --- a/controllers/oauth.go +++ b/controllers/oauth.go @@ -21,6 +21,7 @@ type OAuthResBodyFields struct { Username string `json:"username"` Name string `json:"name"` Email string `json:"email"` + College string `json:"college"` // `mentor` or `student` Type string `json:"type"` // Whether the user has newly registered or was registered before @@ -82,14 +83,17 @@ func OAuth(w http.ResponseWriter, r *http.Request) { // Check if the user has already registered var isNewUser bool = false + student := models.Student{} if reqFields.Type == OAUTH_TYPE_STUDENT { - student := models.Student{} db. Table("students"). Where("username = ?", userInfo.Username). First(&student) isNewUser = student.Username != userInfo.Username + + userInfo.Email = student.Email + } else if reqFields.Type == OAUTH_TYPE_MENTOR { mentor := models.Mentor{} db. @@ -98,6 +102,8 @@ func OAuth(w http.ResponseWriter, r *http.Request) { First(&mentor) isNewUser = mentor.Username != userInfo.Username + + userInfo.Email = mentor.Email } // Generate a JWT string for the user @@ -113,6 +119,7 @@ func OAuth(w http.ResponseWriter, r *http.Request) { Username: userInfo.Username, Name: userInfo.Name, Email: userInfo.Email, + College: student.College, Type: reqFields.Type, IsNewUser: isNewUser, Jwt: jwtString, diff --git a/controllers/student.go b/controllers/student.go index 32a279b..420ad03 100644 --- a/controllers/student.go +++ b/controllers/student.go @@ -19,6 +19,12 @@ type RegisterStudentReqFields struct { College string `json:"college"` } +type UpdateStudentReqFields struct { + Name string `json:"name"` + Email string `json:"email"` + College string `json:"college"` +} + type StudentBlogLinkReqFields struct { Username string `json:"username"` BlogLink string `json:"blog_link"` @@ -303,6 +309,58 @@ func FetchStudentDashboard(w http.ResponseWriter, r *http.Request) { utils.RespondWithJson(r, w, student) } +func UpdateStudentDetails(w http.ResponseWriter, r *http.Request) { + app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App) + db := app.Db + + var modelStudent models.Student + + login_username := r.Context().Value(middleware.LoginCtxKey(middleware.LOGIN_CTX_USERNAME_KEY)) + tx := db. + Table("students"). + Where("username = ?", login_username). + Select("name", "username", "email", "college", "ID"). + First(&modelStudent) + + if tx.Error == gorm.ErrRecordNotFound { + utils.LogErrAndRespond( + r, + w, + tx.Error, + fmt.Sprintf("Student `%s` does not exists.", login_username), + http.StatusBadRequest, + ) + return + } + + var reqFields = UpdateStudentReqFields{} + + err := utils.DecodeJSONBody(r, &reqFields) + if err != nil { + utils.LogErrAndRespond(r, w, err, "Error decoding JSON body.", http.StatusBadRequest) + return + } + + tx = db.Model(&modelStudent).Updates(models.Student{ + Name: reqFields.Name, + Email: reqFields.Email, + College: reqFields.College, + }) + + if tx.Error != nil { + utils.LogErrAndRespond( + r, + w, + tx.Error, + "Invalid Details: Could not update student details", + http.StatusBadRequest, + ) + return + } + + utils.RespondWithJson(r, w, []string{"Student details updated successfully."}) +} + func GetStudentDetails(w http.ResponseWriter, r *http.Request) { app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App) db := app.Db diff --git a/server/routes.go b/server/routes.go index b22c8b9..a2dd202 100644 --- a/server/routes.go +++ b/server/routes.go @@ -46,6 +46,12 @@ func getRoutes(app *middleware.App) []Route { "/student/form/", middleware.WithLogin(middleware.WrapApp(app, controllers.RegisterStudent)), }, + { + "Update Student Details", + "PUT", + "/student/form/", + middleware.WithLogin(middleware.WrapApp(app, controllers.UpdateStudentDetails)), + }, { "Student Blog Submission", "POST", From 2d83361c8bea3149a1f9a90e2fe0c621b1e1a7cd Mon Sep 17 00:00:00 2001 From: Rohan Barsagade Date: Mon, 27 Nov 2023 00:44:08 +0530 Subject: [PATCH 2/2] refactor: better choices handling Co-authored-by: shikharish --- controllers/oauth.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/controllers/oauth.go b/controllers/oauth.go index 1b3c293..2cde9ec 100644 --- a/controllers/oauth.go +++ b/controllers/oauth.go @@ -83,8 +83,9 @@ func OAuth(w http.ResponseWriter, r *http.Request) { // Check if the user has already registered var isNewUser bool = false - student := models.Student{} + college := "" if reqFields.Type == OAUTH_TYPE_STUDENT { + student := models.Student{} db. Table("students"). Where("username = ?", userInfo.Username). @@ -93,6 +94,7 @@ func OAuth(w http.ResponseWriter, r *http.Request) { isNewUser = student.Username != userInfo.Username userInfo.Email = student.Email + college = student.College } else if reqFields.Type == OAUTH_TYPE_MENTOR { mentor := models.Mentor{} @@ -119,7 +121,7 @@ func OAuth(w http.ResponseWriter, r *http.Request) { Username: userInfo.Username, Name: userInfo.Name, Email: userInfo.Email, - College: student.College, + College: college, Type: reqFields.Type, IsNewUser: isNewUser, Jwt: jwtString,