67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package services
|
|
|
|
import (
|
|
"learnsteam/cslms-api/internal/models"
|
|
"learnsteam/cslms-api/internal/repositories"
|
|
)
|
|
|
|
type paperService struct {
|
|
repository repositories.PaperRepository
|
|
paperQuizRepository repositories.PaperQuizRepository
|
|
}
|
|
|
|
type PaperService interface {
|
|
List(int64, string, int, int) (*[]models.PaperResponse, error)
|
|
Total(int64, string) (int64, error)
|
|
|
|
Find(int64) (*models.PaperResponse, error)
|
|
Get(int64) (*models.Paper, error)
|
|
Update(*models.Paper) (*models.Paper, error)
|
|
|
|
UpdateScore(int64) (*models.Paper, error)
|
|
}
|
|
|
|
func NewPaperService(repository repositories.PaperRepository, paperQuizRepository repositories.PaperQuizRepository) PaperService {
|
|
return &paperService{
|
|
repository: repository,
|
|
paperQuizRepository: paperQuizRepository,
|
|
}
|
|
}
|
|
|
|
func (s *paperService) List(user_id int64, q string, page int, limit int) (*[]models.PaperResponse, error) {
|
|
return s.repository.List(user_id, q, page, limit)
|
|
}
|
|
|
|
func (s *paperService) Total(user_id int64, q string) (int64, error) {
|
|
return s.repository.Total(user_id, q)
|
|
}
|
|
|
|
func (s *paperService) Find(id int64) (*models.PaperResponse, error) {
|
|
return s.repository.Find(id)
|
|
}
|
|
|
|
func (s *paperService) Get(id int64) (*models.Paper, error) {
|
|
return s.repository.Get(id)
|
|
}
|
|
|
|
func (s *paperService) Update(paper *models.Paper) (*models.Paper, error) {
|
|
result, err := s.repository.Update(paper)
|
|
|
|
return result, err
|
|
}
|
|
|
|
func (s *paperService) UpdateScore(userquizpaper_id int64) (*models.Paper, error) {
|
|
score, err := s.paperQuizRepository.SumScore(userquizpaper_id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userquizpaper, err := s.repository.Get(userquizpaper_id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userquizpaper.UserScore = score
|
|
return s.repository.Update(userquizpaper)
|
|
}
|