57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"with_you/internal/model"
|
||
|
|
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
type GradeRepository interface {
|
||
|
|
ListByUserAndSemester(userID, semester string) ([]*model.Grade, error)
|
||
|
|
DeleteByUserAndSemester(ctx context.Context, userID, semester string) error
|
||
|
|
BatchCreate(ctx context.Context, grades []*model.Grade) error
|
||
|
|
GetGpaSummary(userID, semester string) (*model.GpaSummary, error)
|
||
|
|
SaveGpaSummary(ctx context.Context, summary *model.GpaSummary) error
|
||
|
|
}
|
||
|
|
|
||
|
|
type gradeRepository struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewGradeRepository(db *gorm.DB) GradeRepository {
|
||
|
|
return &gradeRepository{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *gradeRepository) ListByUserAndSemester(userID, semester string) ([]*model.Grade, error) {
|
||
|
|
var grades []*model.Grade
|
||
|
|
err := r.db.Where("user_id = ? AND term = ?", userID, semester).
|
||
|
|
Order("created_at ASC").
|
||
|
|
Find(&grades).Error
|
||
|
|
return grades, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *gradeRepository) DeleteByUserAndSemester(ctx context.Context, userID, semester string) error {
|
||
|
|
return r.db.WithContext(ctx).
|
||
|
|
Where("user_id = ? AND term = ?", userID, semester).
|
||
|
|
Delete(&model.Grade{}).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *gradeRepository) BatchCreate(ctx context.Context, grades []*model.Grade) error {
|
||
|
|
return r.db.WithContext(ctx).CreateInBatches(grades, 100).Error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *gradeRepository) GetGpaSummary(userID, semester string) (*model.GpaSummary, error) {
|
||
|
|
var summary model.GpaSummary
|
||
|
|
err := r.db.Where("user_id = ? AND semester = ?", userID, semester).First(&summary).Error
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return &summary, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *gradeRepository) SaveGpaSummary(ctx context.Context, summary *model.GpaSummary) error {
|
||
|
|
return r.db.WithContext(ctx).Save(summary).Error
|
||
|
|
}
|