Files
backend/internal/repository/grade_repo.go
lan 2f2bbc646e
All checks were successful
Build Backend / build (push) Successful in 2m18s
Build Backend / build-docker (push) Successful in 1m20s
feat(api): implement exam and grade synchronization modules
Add new functionality for managing and synchronizing academic grades and exam schedules. This includes new models, repositories, services, and HTTP handlers, along with updated gRPC definitions and dependency injection configuration.

Additionally, improve the reliability of unread message count tracking by implementing an atomic Lua script for Redis operations in the conversation cache.
2026-05-12 01:28:18 +08:00

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
}