From 830684231063aa6163afea2c7ec4f2f0d4f797fd Mon Sep 17 00:00:00 2001 From: Ke Chen Date: Thu, 28 Sep 2023 13:28:30 +0800 Subject: [PATCH] feat: maintain course_group.credits --- internal/repository/course.go | 49 +++++++++++++++++++++++++++++++---- internal/service/course.go | 29 ++++++++++++++++++--- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/internal/repository/course.go b/internal/repository/course.go index 530c12d..fa5765f 100644 --- a/internal/repository/course.go +++ b/internal/repository/course.go @@ -2,6 +2,9 @@ package repository import ( "context" + "slices" + "strconv" + "strings" "gorm.io/gorm" @@ -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 { @@ -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 diff --git a/internal/service/course.go b/internal/service/course.go index 3ac0d06..a9a83c2 100644 --- a/internal/service/course.go +++ b/internal/service/course.go @@ -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 { @@ -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") }) @@ -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 }