2026-05-12 01:28:18 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"with_you/internal/model"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GradeRepository interface {
|
2026-06-07 00:11:47 +08:00
|
|
|
ListByUser(userID string) ([]*model.Grade, error)
|
|
|
|
|
DeleteByUser(ctx context.Context, userID string) error
|
2026-05-12 01:28:18 +08:00
|
|
|
BatchCreate(ctx context.Context, grades []*model.Grade) error
|
2026-06-07 00:11:47 +08:00
|
|
|
GetLatestGpaSummary(userID string) (*model.GpaSummary, error)
|
2026-05-12 01:28:18 +08:00
|
|
|
SaveGpaSummary(ctx context.Context, summary *model.GpaSummary) error
|
2026-06-07 00:11:47 +08:00
|
|
|
DeleteGpaSummaryByUser(ctx context.Context, userID string) error
|
2026-05-12 01:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type gradeRepository struct {
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewGradeRepository(db *gorm.DB) GradeRepository {
|
|
|
|
|
return &gradeRepository{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-07 00:11:47 +08:00
|
|
|
func (r *gradeRepository) ListByUser(userID string) ([]*model.Grade, error) {
|
2026-05-12 01:28:18 +08:00
|
|
|
var grades []*model.Grade
|
2026-06-07 00:11:47 +08:00
|
|
|
err := r.db.Where("user_id = ?", userID).
|
|
|
|
|
Order("term DESC, created_at ASC").
|
2026-05-12 01:28:18 +08:00
|
|
|
Find(&grades).Error
|
|
|
|
|
return grades, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-07 00:11:47 +08:00
|
|
|
func (r *gradeRepository) DeleteByUser(ctx context.Context, userID string) error {
|
2026-05-12 01:28:18 +08:00
|
|
|
return r.db.WithContext(ctx).
|
2026-06-07 00:11:47 +08:00
|
|
|
Where("user_id = ?", userID).
|
2026-05-12 01:28:18 +08:00
|
|
|
Delete(&model.Grade{}).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *gradeRepository) BatchCreate(ctx context.Context, grades []*model.Grade) error {
|
|
|
|
|
return r.db.WithContext(ctx).CreateInBatches(grades, 100).Error
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-07 00:11:47 +08:00
|
|
|
func (r *gradeRepository) GetLatestGpaSummary(userID string) (*model.GpaSummary, error) {
|
2026-05-12 01:28:18 +08:00
|
|
|
var summary model.GpaSummary
|
2026-06-07 00:11:47 +08:00
|
|
|
err := r.db.Where("user_id = ?", userID).
|
|
|
|
|
Order("updated_at DESC").
|
|
|
|
|
First(&summary).Error
|
2026-05-12 01:28:18 +08:00
|
|
|
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
|
|
|
|
|
}
|
2026-06-07 00:11:47 +08:00
|
|
|
|
|
|
|
|
func (r *gradeRepository) DeleteGpaSummaryByUser(ctx context.Context, userID string) error {
|
|
|
|
|
return r.db.WithContext(ctx).
|
|
|
|
|
Where("user_id = ?", userID).
|
|
|
|
|
Delete(&model.GpaSummary{}).Error
|
|
|
|
|
}
|