Skip to content

Commit

Permalink
feat: maintain course_group.credits
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Sep 28, 2023
1 parent 489ceb5 commit 8306842
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
49 changes: 44 additions & 5 deletions internal/repository/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package repository

import (
"context"
"slices"
"strconv"
"strings"

"gorm.io/gorm"

Expand All @@ -11,9 +14,22 @@ import (
type CourseRepository interface {
Repository

FindCourseByID(ctx context.Context, id int, conditions ...func(db *gorm.DB) *gorm.DB) (course *model.Course, err error)
FindCoursesByGroupID(ctx context.Context, groupID int) (courses []*model.Course, err error)
CreateCourse(ctx context.Context, course *model.Course) (err error)
FindCourseByID(
ctx context.Context,
id int,
conditions ...func(db *gorm.DB) *gorm.DB,
) (course *model.Course, err error)

FindCoursesByGroupID(
ctx context.Context,
groupID int,
) (courses []*model.Course, err error)

CreateCourse(
ctx context.Context,
course *model.CourseGroup,
courseGroup *model.Course,
) (err error)
}

type courseRepository struct {
Expand Down Expand Up @@ -42,14 +58,37 @@ func (r *courseRepository) FindCoursesByGroupID(ctx context.Context, groupID int
return
}

func (r *courseRepository) CreateCourse(ctx context.Context, course *model.Course) (err error) {
func (r *courseRepository) CreateCourse(
ctx context.Context,
courseGroup *model.CourseGroup,
course *model.Course,
) (err error) {
err = r.Transaction(ctx, func(ctx context.Context) error {
err = r.GetDB(ctx).Create(course).Error
if err != nil {
return err
}

return r.GetDB(ctx).Model(&model.CourseGroup{ID: course.CourseGroupID}).Update("course_count", gorm.Expr("course_count + 1")).Error
updateColumes := map[string]any{
"course_count": gorm.Expr("course_count + 1"),
}

if !slices.Contains(courseGroup.Credits, course.Credit) {
courseGroup.Credits = append(courseGroup.Credits, course.Credit)
var creditsString strings.Builder
creditsString.WriteByte('[')
for i, credit := range courseGroup.Credits {
if i != 0 {
creditsString.WriteByte(',')
}
creditsString.WriteString(strconv.FormatFloat(credit, 'f', -1, 64))
}
creditsString.WriteByte(']')
updateColumes["credits"] = creditsString.String()
}

return r.GetDB(ctx).Model(&model.CourseGroup{ID: course.CourseGroupID}).
Updates(updateColumes).Error
})
if err != nil {
return err
Expand Down
29 changes: 25 additions & 4 deletions internal/service/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ type CourseService interface {
Service

ListCoursesV1(ctx context.Context) (response []*schema.CourseGroupV1Response, err error)
GetCourseV1(ctx context.Context, user *model.User, id int) (response *schema.CourseV1Response, err error)
AddCourseV1(ctx context.Context, request *schema.CreateCourseV1Request) (response *schema.CourseV1Response, err error)

GetCourseV1(
ctx context.Context,
user *model.User,
id int,
) (
response *schema.CourseV1Response,
err error,
)

AddCourseV1(
ctx context.Context,
request *schema.CreateCourseV1Request,
) (
response *schema.CourseV1Response,
err error,
)
}

type courseService struct {
Expand Down Expand Up @@ -85,7 +100,13 @@ func (s *courseService) GetCourseV1(ctx context.Context, user *model.User, id in
return new(schema.CourseV1Response).FromModel(user, course, votesMap), nil
}

func (s *courseService) AddCourseV1(ctx context.Context, request *schema.CreateCourseV1Request) (response *schema.CourseV1Response, err error) {
func (s *courseService) AddCourseV1(
ctx context.Context,
request *schema.CreateCourseV1Request,
) (
response *schema.CourseV1Response,
err error,
) {
group, err := s.courseGroupRepository.FindGroupByCode(ctx, request.Code, func(db *gorm.DB) *gorm.DB {
return db.Preload("Courses")
})
Expand All @@ -103,7 +124,7 @@ func (s *courseService) AddCourseV1(ctx context.Context, request *schema.CreateC
}

course := request.ToModel(group.ID)
err = s.courseRepository.CreateCourse(ctx, course)
err = s.courseRepository.CreateCourse(ctx, group, course)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8306842

Please sign in to comment.