Skip to content

Commit

Permalink
Merge pull request #29 from WillyTonkas/development/back/create-end-p…
Browse files Browse the repository at this point in the history
…oints

fix (use case): Made requested changes.
  • Loading branch information
GuidoM197 authored Dec 31, 2024
2 parents 68609c3 + a6b4616 commit dcde13d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
14 changes: 7 additions & 7 deletions controllers/courseController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
32 changes: 13 additions & 19 deletions services/users/userService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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(&currentUser)
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(&currentUser)
return currentUser.IsOwner
}

0 comments on commit dcde13d

Please sign in to comment.