Skip to content

Commit

Permalink
Merge pull request #19 from WillyTonkas/feature/back/create-course
Browse files Browse the repository at this point in the history
feat(user service): Implemented a function to create a course.
  • Loading branch information
FranCalveyra authored Dec 28, 2024
2 parents 72a01cb + 72bab2e commit 3ba692f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions models/userModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type IsEnrolled struct {
gorm.Model
UserId uint
CourseId uint
IsOwner bool
}

type Course struct {
Expand Down
26 changes: 26 additions & 0 deletions services/users/userService.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ func userInCourse(db *gorm.DB, userId, courseId uint) bool {
return db.Model(models.IsEnrolled{}).Where("UserId = ? AND CourseId = ?", userId, courseId).Error != nil
}

func CreateCourse(db *gorm.DB, userId uint, courseName, description string) error {
if !userExists(db, userId) {
return fmt.Errorf("User does not exist.")
}

currentCourse := models.Course{
Model: gorm.Model{},
Name: courseName,
Description: description,
}

if db.Model(models.Course{}).Create(&currentCourse).Error != nil {
return fmt.Errorf("Error when to create a course.")
}

db.Model(models.IsEnrolled{}).Create(models.IsEnrolled{
Model: gorm.Model{},
UserId: userId,
CourseId: currentCourse.ID,
IsOwner: true,
})

return nil
}

func EnrollToCourse(db *gorm.DB, userId, courseId uint) error {
if !userExists(db, userId) {
return fmt.Errorf("User does not exist.")
Expand All @@ -34,6 +59,7 @@ func EnrollToCourse(db *gorm.DB, userId, courseId uint) error {
Model: gorm.Model{},
UserId: userId,
CourseId: courseId,
IsOwner: false,
})

return nil
Expand Down

0 comments on commit 3ba692f

Please sign in to comment.