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.
This commit is contained in:
56
internal/repository/grade_repo.go
Normal file
56
internal/repository/grade_repo.go
Normal file
@@ -0,0 +1,56 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user