From a6b461603239f398f3c885cd6d9de42f55118c77 Mon Sep 17 00:00:00 2001 From: GuidoM197 Date: Mon, 30 Dec 2024 19:33:23 -0300 Subject: [PATCH] fix (use case): Made requested changes. Fixes: #27 --- controllers/courseController.go | 14 +++++++------- services/users/userService.go | 32 +++++++++++++------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/controllers/courseController.go b/controllers/courseController.go index 0d2a5b2..8e2294b 100644 --- a/controllers/courseController.go +++ b/controllers/courseController.go @@ -70,8 +70,8 @@ func CreateCourse(w http.ResponseWriter, r *http.Request, db *gorm.DB) { // TODO: Test this function after implementing auth0. func EnrollToCourse(w http.ResponseWriter, r *http.Request, db *gorm.DB) { var enrollmentRequest struct { - UserID uuid.UUID `json:"user_id"` - CourseID uuid.UUID `json:"course_id"` + UserID uuid.UUID `json:"UserID"` + CourseID uuid.UUID `json:"CourseID"` } if json.NewDecoder(r.Body).Decode(&enrollmentRequest) != nil { @@ -96,8 +96,8 @@ func EnrollToCourse(w http.ResponseWriter, r *http.Request, db *gorm.DB) { // TODO: Test this function after implementing auth0. func StudentExists(w http.ResponseWriter, r *http.Request, db *gorm.DB) { var enrollmentRequest struct { - UserID uuid.UUID `json:"user_id"` - CourseID uuid.UUID `json:"course_id"` + UserID uuid.UUID `json:"UserID"` + CourseID uuid.UUID `json:"CourseID"` } if json.NewDecoder(r.Body).Decode(&enrollmentRequest) != nil { @@ -121,9 +121,9 @@ func StudentExists(w http.ResponseWriter, r *http.Request, db *gorm.DB) { // TODO: Test this function after implementing auth0. func DeleteStudent(w http.ResponseWriter, r *http.Request, db *gorm.DB) { var deleteRequest struct { - UserID uuid.UUID `json:"user_id"` - CourseID uuid.UUID `json:"course_id"` - StudentID uuid.UUID `json:"student_id"` + UserID uuid.UUID `json:"UserID"` + CourseID uuid.UUID `json:"CourseID"` + StudentID uuid.UUID `json:"StudentID"` } if json.NewDecoder(r.Body).Decode(&deleteRequest) != nil { diff --git a/services/users/userService.go b/services/users/userService.go index ca566fb..abd2a16 100644 --- a/services/users/userService.go +++ b/services/users/userService.go @@ -13,7 +13,7 @@ func EnrollToCourse(db *gorm.DB, userID, courseID uuid.UUID) error { // return errors.New("user does not exist") //} - if userInCourse(db, userID, courseID) { + if IsUserInCourse(db, userID, courseID) { return errors.New("user is already in course") } @@ -57,7 +57,7 @@ func RemoveStudent(db *gorm.DB, userID, courseID, studentID uuid.UUID) error { return errors.New("this user doesn't have permission to remove any student") } - if !userInCourse(db, studentID, courseID) { + if !IsUserInCourse(db, studentID, courseID) { return errors.New("the user does not exist in the course") } @@ -105,32 +105,26 @@ func CreateTest(db *gorm.DB, test models.TestDTO) uuid.UUID { return currentTestID } -func CourseExists(db *gorm.DB, courseID uuid.UUID) bool { - return db.Model(models.Course{}).Where("ID = ?", courseID).Error == nil -} - func IsUserInCourse(db *gorm.DB, userID, courseID uuid.UUID) bool { - var enrollment models.IsEnrolled - result := db.Where("user_id = ? AND course_id = ?", userID, courseID).First(&enrollment) - return result.Error == nil + if !CourseExists(db, courseID) { + return false + } + return db.Model(models.IsEnrolled{}).Where("UserID = ? AND CourseID = ?", userID, courseID).Error == nil } // ------------------------- Private functions ------------------------- -func isOwner(db *gorm.DB, userID, courseID uuid.UUID) bool { - currentUser := models.IsEnrolled{} - db.Model(models.IsEnrolled{}).Where("UserID = ? AND CourseID = ?", userID, courseID).First(¤tUser) - return currentUser.IsOwner +func CourseExists(db *gorm.DB, courseID uuid.UUID) bool { + return db.Model(models.Course{}).Where("ID = ?", courseID).Error == nil } // func userExists(db *gorm.DB, id uint) bool { -// //TODO: use Auth0 +// // TODO: use Auth0 // return true //} -func userInCourse(db *gorm.DB, userID, courseID uuid.UUID) bool { - if !CourseExists(db, courseID) { - return false - } - return db.Model(models.IsEnrolled{}).Where("UserID = ? AND CourseID = ?", userID, courseID).Error == nil +func isOwner(db *gorm.DB, userID, courseID uuid.UUID) bool { + currentUser := models.IsEnrolled{} + db.Model(models.IsEnrolled{}).Where("UserID = ? AND CourseID = ?", userID, courseID).First(¤tUser) + return currentUser.IsOwner }