Files
backend/internal/repository/schedule_repo.go
lafay 2f584c1f2c
All checks were successful
Build Backend / build (push) Successful in 5m10s
Build Backend / build-docker (push) Successful in 2m22s
This is a breaking change that renames the entire project from "Carrot BBS" to "WithYou".
The Go module name has been changed from `carrot_bbs` to `with_you`, which requires
all import paths to be updated throughout the codebase and by external consumers.

BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports
and dependencies must be updated from carrot_bbs/* to with_you/*.
2026-04-22 16:01:59 +08:00

74 lines
2.0 KiB
Go

package repository
import (
"context"
"with_you/internal/model"
"gorm.io/gorm"
)
type ScheduleRepository interface {
ListByUserID(userID string) ([]*model.ScheduleCourse, error)
GetByID(id string) (*model.ScheduleCourse, error)
Create(course *model.ScheduleCourse) error
Update(course *model.ScheduleCourse) error
DeleteByID(id string) error
DeleteByUserID(ctx context.Context, userID string) error
ExistsColorByUser(userID, color, excludeID string) (bool, error)
}
type scheduleRepository struct {
db *gorm.DB
}
func NewScheduleRepository(db *gorm.DB) ScheduleRepository {
return &scheduleRepository{db: db}
}
func (r *scheduleRepository) ListByUserID(userID string) ([]*model.ScheduleCourse, error) {
var courses []*model.ScheduleCourse
err := r.db.
Where("user_id = ?", userID).
Order("day_of_week ASC, start_section ASC, created_at ASC").
Find(&courses).Error
return courses, err
}
func (r *scheduleRepository) Create(course *model.ScheduleCourse) error {
return r.db.Create(course).Error
}
func (r *scheduleRepository) GetByID(id string) (*model.ScheduleCourse, error) {
var course model.ScheduleCourse
if err := r.db.Where("id = ?", id).First(&course).Error; err != nil {
return nil, err
}
return &course, nil
}
func (r *scheduleRepository) Update(course *model.ScheduleCourse) error {
return r.db.Save(course).Error
}
func (r *scheduleRepository) DeleteByID(id string) error {
return r.db.Delete(&model.ScheduleCourse{}, "id = ?", id).Error
}
func (r *scheduleRepository) DeleteByUserID(ctx context.Context, userID string) error {
return r.db.WithContext(ctx).Delete(&model.ScheduleCourse{}, "user_id = ?", userID).Error
}
func (r *scheduleRepository) ExistsColorByUser(userID, color, excludeID string) (bool, error) {
var count int64
query := r.db.Model(&model.ScheduleCourse{}).
Where("user_id = ? AND LOWER(color) = LOWER(?)", userID, color)
if excludeID != "" {
query = query.Where("id <> ?", excludeID)
}
if err := query.Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}