Skip to content

Commit

Permalink
Fetch Mentor and Student Details from beackend using jwt token
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivharlalka committed Nov 14, 2023
1 parent 76d16b3 commit e5be917
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
27 changes: 27 additions & 0 deletions controllers/mentor.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,30 @@ func UpdateMentorDetails(w http.ResponseWriter, r *http.Request) {

utils.RespondWithJson(r, w, []string{"Mentor details updated successfully."})
}

func GetMentorDetails(w http.ResponseWriter, r *http.Request) {
app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App)
db := app.Db

login_username := r.Context().Value(middleware.LoginCtxKey(middleware.LOGIN_CTX_USERNAME_KEY))

mentor := models.Mentor{}
tx := db.
Table("mentors").
Where("username = ?", login_username).
Select("name", "username", "email", "ID").
First(&mentor)

if tx.Error == gorm.ErrRecordNotFound {
utils.LogErrAndRespond(
r,
w,
tx.Error,
fmt.Sprintf("Mentor `%s` does not exists.", login_username),
http.StatusBadRequest,
)
return
}

utils.RespondWithJson(r, w, mentor)
}
31 changes: 31 additions & 0 deletions controllers/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,34 @@ func FetchStudentDashboard(w http.ResponseWriter, r *http.Request) {
student := CreateStudentDashboard(modelStudent, db)
utils.RespondWithJson(r, w, student)
}

func GetStudentDetails(w http.ResponseWriter, r *http.Request) {
app := r.Context().Value(middleware.APP_CTX_KEY).(*middleware.App)
db := app.Db

login_username := r.Context().Value(middleware.LoginCtxKey(middleware.LOGIN_CTX_USERNAME_KEY))

student := models.Student{}
tx := db.
Table("students").
Where("username = ?", login_username).
First(&student)

if tx.Error != nil && tx.Error != gorm.ErrRecordNotFound {
utils.LogErrAndRespond(r, w, tx.Error, "Database error.", http.StatusInternalServerError)
return
}

if tx.Error == gorm.ErrRecordNotFound {
utils.LogErrAndRespond(
r,
w,
tx.Error,
fmt.Sprintf("Student `%s` does not exists.", login_username),
http.StatusBadRequest,
)
return
}

utils.RespondWithJson(r, w, student)
}
12 changes: 12 additions & 0 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ func getRoutes(app *middleware.App) []Route {
"/oauth/",
middleware.WrapApp(app, controllers.OAuth),
},
{
"Fetch Student Details",
"GET",
"/student/",
middleware.WithLogin(middleware.WrapApp(app, controllers.GetStudentDetails)),
},
{
"Fetch Mentor Details",
"GET",
"/mentor/",
middleware.WithLogin(middleware.WrapApp(app, controllers.GetMentorDetails)),
},
{
"Student Registration",
"POST",
Expand Down

0 comments on commit e5be917

Please sign in to comment.