Skip to content

Commit

Permalink
Merge pull request #166 from kossiitkgp/shikhar-did-this
Browse files Browse the repository at this point in the history
College and email fields are editable
  • Loading branch information
chirag-ghosh authored Nov 26, 2023
2 parents 96c746c + 2d83361 commit f9395d9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
9 changes: 9 additions & 0 deletions controllers/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,6 +83,7 @@ func OAuth(w http.ResponseWriter, r *http.Request) {
// Check if the user has already registered
var isNewUser bool = false

college := ""
if reqFields.Type == OAUTH_TYPE_STUDENT {
student := models.Student{}
db.
Expand All @@ -90,6 +92,10 @@ func OAuth(w http.ResponseWriter, r *http.Request) {
First(&student)

isNewUser = student.Username != userInfo.Username

userInfo.Email = student.Email
college = student.College

} else if reqFields.Type == OAUTH_TYPE_MENTOR {
mentor := models.Mentor{}
db.
Expand All @@ -98,6 +104,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
Expand All @@ -113,6 +121,7 @@ func OAuth(w http.ResponseWriter, r *http.Request) {
Username: userInfo.Username,
Name: userInfo.Name,
Email: userInfo.Email,
College: college,
Type: reqFields.Type,
IsNewUser: isNewUser,
Jwt: jwtString,
Expand Down
58 changes: 58 additions & 0 deletions controllers/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f9395d9

Please sign in to comment.