Skip to content

Commit 6a9d040

Browse files
committed
feat: upload student id for verify
1 parent 7eac636 commit 6a9d040

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

api/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func SetupRoutes() *gin.Engine {
8585
userRoutes.PUT("/edit", userHandlers.EditUser)
8686
userRoutes.DELETE("/delete", userHandlers.DeleteUser)
8787
userRoutes.POST("/upload-profile-picture", userHandlers.UploadProfilePicture)
88+
userRoutes.POST("/upload-student-id", userHandlers.UploadStudentID)
8889
userRoutes.PUT("/update-user", userHandlers.AdminUpdateRoleAndStudentIDVerified)
8990
userRoutes.POST("/2fa/enable", userHandlers.EnableTwoFA)
9091
userRoutes.POST("/2fa/verify", userHandlers.VerifyTwoFA)

internal/database/app/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,8 @@ func UploadProfilePicture(userID uuid.UUID, profilePicture string) error {
337337
_, err := database.DB.Exec(context.Background(), "UPDATE users SET profile_picture = $1 WHERE id = $2", profilePicture, userID)
338338
return err
339339
}
340+
341+
func UploadStudentID(userID uuid.UUID, studentID string) error {
342+
_, err := database.DB.Exec(context.Background(), "UPDATE users SET student_id_verification = $1 WHERE id = $2", studentID, userID)
343+
return err
344+
}

internal/handlers/user/user_handlers.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,75 @@ func (h *Handlers) UploadProfilePicture(c *gin.Context) {
356356
})
357357
}
358358

359+
func (h *Handlers) UploadStudentID(c *gin.Context) {
360+
userID, err := utils.GetUserIDFromContext(c)
361+
if err != nil {
362+
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": []string{err.Error()}})
363+
return
364+
}
365+
366+
user, err := h.UserService.GetUserByID(userID)
367+
if err != nil {
368+
c.JSON(http.StatusNotFound, gin.H{"success": false, "message": []string{err.Error()}})
369+
return
370+
}
371+
372+
file, _, err := c.Request.FormFile("student_id")
373+
if err != nil {
374+
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": []string{err.Error()}})
375+
return
376+
}
377+
378+
// Check if file size is greater than 2MB
379+
optimizedImage, err := utils.OptimizeImage(file, 2800, 1080)
380+
if err != nil {
381+
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": []string{err.Error()}})
382+
return
383+
}
384+
385+
optimizedImageBytes, err := io.ReadAll(optimizedImage)
386+
if err != nil {
387+
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": []string{err.Error()}})
388+
return
389+
}
390+
391+
// Choose storage service to upload image to (AWS or R2)
392+
upload := utils.ChooseStorageService()
393+
394+
// Upload image to storage service
395+
if upload == utils.R2Service {
396+
err = h.R2Service.UploadFileToR2(context.Background(), "users", "student_id_"+userID.String(), optimizedImageBytes)
397+
if err != nil {
398+
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": []string{err.Error()}})
399+
return
400+
}
401+
402+
imageUrl, _ := h.R2Service.GetFileR2("users", "student_id_"+userID.String())
403+
user.StudentIDVerification = &imageUrl
404+
} else {
405+
err = h.AWSService.UploadFileToAWS(context.Background(), "users", "student_id_"+userID.String(), optimizedImageBytes)
406+
if err != nil {
407+
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": []string{err.Error()}})
408+
return
409+
}
410+
411+
imageUrl, _ := h.AWSService.GetFileAWS("users", "student_id_"+userID.String())
412+
user.StudentIDVerification = &imageUrl
413+
}
414+
415+
// Update user profile picture
416+
if err := h.UserService.UploadProfilePicture(userID, user.ProfilePicture); err != nil {
417+
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": []string{err.Error()}})
418+
return
419+
}
420+
421+
c.JSON(http.StatusOK, gin.H{
422+
"success": true,
423+
"message": "Profile Picture Uploaded Successfully",
424+
"data": user.ProfilePicture,
425+
})
426+
}
427+
359428
func (h *Handlers) EnableTwoFA(c *gin.Context) {
360429
userID, err := (&auth.Handlers{}).ExtractUserIDAndCheckPermission(c, "users:2fa")
361430
if err != nil {

internal/services/user_services.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func (us *UserService) UploadProfilePicture(userID uuid.UUID, profilePicture str
112112
return app.UploadProfilePicture(userID, profilePicture)
113113
}
114114

115+
func (us *UserService) UploadStudentID(userID uuid.UUID, profilePicture string) error {
116+
return app.UploadStudentID(userID, profilePicture)
117+
}
118+
115119
func (us *UserService) EnableTwoFA(userID uuid.UUID) (string, string, error) {
116120
user, err := app.GetUserByID(userID)
117121
if err != nil {

0 commit comments

Comments
 (0)