Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

College and email fields are editable #166

Merged
merged 2 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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