cslms-api/internal/services/userquizpaper.go

110 lines
3.0 KiB
Go
Raw Normal View History

2023-12-27 17:31:49 +09:00
package services
import (
2024-02-19 22:43:31 +09:00
"fmt"
2023-12-27 17:31:49 +09:00
"learnsteam/cslms-api/internal/models"
"learnsteam/cslms-api/internal/repositories"
"github.com/google/uuid"
)
type userQuizPaperService struct {
2024-02-19 22:43:31 +09:00
repository repositories.UserQuizPaperRepository
userquizRepository repositories.UserQuizRepository
2023-12-27 17:31:49 +09:00
}
type UserQuizPaperService interface {
List(string, int, int) (*[]models.UserQuizPaperResponse, error)
2023-12-27 17:31:49 +09:00
Total(string) (int64, error)
Find(int64) (*models.UserQuizPaperResponse, error)
Get(int64) (*models.UserQuizPaper, error)
2023-12-27 17:31:49 +09:00
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
2024-02-19 22:43:31 +09:00
UpdateScore(int64) (*models.UserQuizPaper, error)
2023-12-27 17:31:49 +09:00
}
2024-02-19 22:43:31 +09:00
func NewUserQuizPaperService(repository repositories.UserQuizPaperRepository, userquizRepository repositories.UserQuizRepository) UserQuizPaperService {
2023-12-27 17:31:49 +09:00
return &userQuizPaperService{
2024-02-19 22:43:31 +09:00
repository: repository,
userquizRepository: userquizRepository,
2023-12-27 17:31:49 +09:00
}
}
func (s *userQuizPaperService) List(q string, page int, limit int) (*[]models.UserQuizPaperResponse, error) {
2023-12-27 17:31:49 +09:00
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) {
2023-12-27 17:31:49 +09:00
return s.repository.Find(id)
}
func (s *userQuizPaperService) Get(id int64) (*models.UserQuizPaper, error) {
return s.repository.Get(id)
}
2023-12-27 17:31:49 +09:00
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,
2024-02-19 22:43:31 +09:00
Status: "ready",
2023-12-27 17:31:49 +09:00
}
userQuizPapers = append(userQuizPapers, userQuizPaper)
}
return userQuizPapers
}
2024-02-19 22:43:31 +09:00
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)
}