110 lines
3.0 KiB
Go
110 lines
3.0 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"learnsteam/cslms-api/internal/models"
|
|
"learnsteam/cslms-api/internal/repositories"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type userQuizPaperService struct {
|
|
repository repositories.UserQuizPaperRepository
|
|
userquizRepository repositories.UserQuizRepository
|
|
}
|
|
|
|
type UserQuizPaperService interface {
|
|
List(string, int, int) (*[]models.UserQuizPaperResponse, error)
|
|
Total(string) (int64, error)
|
|
|
|
Find(int64) (*models.UserQuizPaperResponse, error)
|
|
Get(int64) (*models.UserQuizPaper, error)
|
|
Create(*models.UserQuizPaper) (*models.UserQuizPaper, error)
|
|
Insert([]models.UserQuizPaper) ([]models.UserQuizPaper, error)
|
|
Update(*models.UserQuizPaper) (*models.UserQuizPaper, error)
|
|
Delete(string) error
|
|
Generate([]int64, *models.QuizPaper) []models.UserQuizPaper
|
|
UpdateScore(int64) (*models.UserQuizPaper, error)
|
|
}
|
|
|
|
func NewUserQuizPaperService(repository repositories.UserQuizPaperRepository, userquizRepository repositories.UserQuizRepository) UserQuizPaperService {
|
|
return &userQuizPaperService{
|
|
repository: repository,
|
|
userquizRepository: userquizRepository,
|
|
}
|
|
}
|
|
|
|
func (s *userQuizPaperService) List(q string, page int, limit int) (*[]models.UserQuizPaperResponse, error) {
|
|
return s.repository.List(q, page, limit)
|
|
}
|
|
|
|
func (s *userQuizPaperService) Total(q string) (int64, error) {
|
|
return s.repository.Total(q)
|
|
}
|
|
|
|
func (s *userQuizPaperService) Find(id int64) (*models.UserQuizPaperResponse, error) {
|
|
return s.repository.Find(id)
|
|
}
|
|
|
|
func (s *userQuizPaperService) Get(id int64) (*models.UserQuizPaper, error) {
|
|
return s.repository.Get(id)
|
|
}
|
|
|
|
func (s *userQuizPaperService) Create(userQuizPaper *models.UserQuizPaper) (*models.UserQuizPaper, error) {
|
|
result, err := s.repository.Create(userQuizPaper)
|
|
|
|
return result, err
|
|
}
|
|
|
|
func (s *userQuizPaperService) Insert(items []models.UserQuizPaper) ([]models.UserQuizPaper, error) {
|
|
result, err := s.repository.Insert(items)
|
|
|
|
return result, err
|
|
}
|
|
|
|
func (s *userQuizPaperService) Update(userQuizPaper *models.UserQuizPaper) (*models.UserQuizPaper, error) {
|
|
result, err := s.repository.Update(userQuizPaper)
|
|
|
|
return result, err
|
|
}
|
|
|
|
func (s *userQuizPaperService) Delete(id string) error {
|
|
err := s.repository.Delete(id)
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *userQuizPaperService) Generate(users []int64, quizPaper *models.QuizPaper) []models.UserQuizPaper {
|
|
var userQuizPapers []models.UserQuizPaper
|
|
|
|
for _, user_id := range users {
|
|
userQuizPaper := models.UserQuizPaper{
|
|
GUID: uuid.NewString(),
|
|
CenterID: quizPaper.CenterID,
|
|
UserID: user_id,
|
|
QuizPaperID: quizPaper.ID,
|
|
Status: "ready",
|
|
}
|
|
|
|
userQuizPapers = append(userQuizPapers, userQuizPaper)
|
|
}
|
|
|
|
return userQuizPapers
|
|
}
|
|
|
|
func (s *userQuizPaperService) UpdateScore(userquizpaper_id int64) (*models.UserQuizPaper, error) {
|
|
score, err := s.userquizRepository.SumScore(userquizpaper_id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userquizpaper, err := s.repository.Get(userquizpaper_id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Println("score", score)
|
|
userquizpaper.UserScore = score
|
|
return s.repository.Update(userquizpaper)
|
|
}
|