Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/back/create exercise #20

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions models/courseModels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package models

import "gorm.io/gorm"

type Course struct {
gorm.Model
Name string
Description string
}

type Exercise struct {
gorm.Model
Name string
Description string
BaseCode string
Points int
UnitNumber int
TestIds []uint `gorm:"foreignkey:ExerciseId"`
}

type Test struct {
gorm.Model
Name string
Input []string
Output []string
}

type TestDTO struct {
Name string
Input []string
Output []string
}

type ExerciseDTO struct {
Name string
Description string
BaseCode string
Points int
UnitNumber int
TestData []TestDTO
}
11 changes: 1 addition & 10 deletions models/userModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ import (
"gorm.io/gorm"
)

type User struct {
gorm.Model
}

type IsEnrolled struct {
gorm.Model
UserId uint
CourseId uint
}

type Course struct {
gorm.Model
Name string
Description string
IsOwner bool
}

type Profile struct {
Expand Down
89 changes: 74 additions & 15 deletions services/users/userService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ import (
"rpl-service/models"
)

func userExists(db *gorm.DB, id uint) bool {
return db.Model(models.User{}).Where("ID = ?", id).Error != nil
}

func courseExists(db *gorm.DB, courseId uint) bool {
return db.Model(models.Course{}).Where("ID = ?", courseId).Error != nil
}
func CreateCourse(db *gorm.DB, userId uint, courseName, description string) error {
currentCourse := models.Course{
Model: gorm.Model{},
Name: courseName,
Description: description,
}

func userInCourse(db *gorm.DB, userId, courseId uint) bool {
if !courseExists(db, courseId) {
return false
if db.Model(models.Course{}).Create(&currentCourse).Error != nil {
return fmt.Errorf("Error when creating a course.")
}
return db.Model(models.IsEnrolled{}).Where("UserId = ? AND CourseId = ?", userId, courseId).Error != nil

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.")
}

if userInCourse(db, userId, courseId) {
return fmt.Errorf("User is already in course.")
}
Expand All @@ -34,7 +36,64 @@ func EnrollToCourse(db *gorm.DB, userId, courseId uint) error {
Model: gorm.Model{},
UserId: userId,
CourseId: courseId,
IsOwner: false,
})

return nil
}

func CreateExercise(db *gorm.DB, exercise models.ExerciseDTO, userId, courseId uint) error {
if !isOwner(db, userId, courseId) {
return fmt.Errorf("This user doesn't have permission to create a unit.")
}

testIds := []uint{}
for _, test := range exercise.TestData {
testIds = append(testIds, CreateTest(db, test))
}

db.Model(models.Exercise{}).Create(models.Exercise{
Model: gorm.Model{},
Name: exercise.Name,
Description: exercise.Description,
BaseCode: exercise.BaseCode,
TestIds: testIds,
Points: exercise.Points,
UnitNumber: exercise.UnitNumber,
})

return nil
}

func CreateTest(db *gorm.DB, test models.TestDTO) uint {
db.Model(models.Test{}).Create(models.Test{
Model: gorm.Model{},
Name: test.Name,
Input: test.Input,
Output: test.Output,
})

var currentTestId uint
db.Model(models.Test{}).Select("ID").Last(&currentTestId)

return currentTestId
}

// ------------------------- Private functions -------------------------

func isOwner(db *gorm.DB, userId uint, courseId uint) 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 uint) bool {
return db.Model(models.Course{}).Where("ID = ?", courseId).Error != nil
}

func userInCourse(db *gorm.DB, userId, courseId uint) bool {
if !courseExists(db, courseId) {
return false
}
return db.Model(models.IsEnrolled{}).Where("UserId = ? AND CourseId = ?", userId, courseId).Error != nil
}
Loading