cslms-api/internal/services/quiz.go

86 lines
1.9 KiB
Go
Raw Normal View History

2023-12-27 17:31:49 +09:00
package services
import (
"learnsteam/cslms-api/internal/models"
"learnsteam/cslms-api/internal/repositories"
"time"
"github.com/google/uuid"
)
type quizService struct {
repository repositories.QuizRepository
}
type QuizService interface {
List(int64, int, int) (*[]models.Quiz, error)
Total(int64) (int64, error)
Find(int64) (*models.Quiz, error)
Create(*models.Quiz) (*models.Quiz, error)
Update(*models.Quiz) (*models.Quiz, error)
Delete(int64) error
}
func NewQuizService(repository repositories.QuizRepository) QuizService {
return &quizService{
repository: repository,
}
}
func (s *quizService) List(quiz_paper_id int64, page int, limit int) (*[]models.Quiz, error) {
return s.repository.List(quiz_paper_id, page, limit)
}
func (s *quizService) Total(quiz_paper_id int64) (int64, error) {
return s.repository.Total(quiz_paper_id)
}
func (s *quizService) Find(id int64) (*models.Quiz, error) {
return s.repository.Find(id)
}
func (s *quizService) Create(quiz *models.Quiz) (*models.Quiz, error) {
result, err := s.repository.Create(quiz)
return result, err
}
func (s *quizService) Update(quiz *models.Quiz) (*models.Quiz, error) {
result, err := s.repository.Update(quiz)
return result, err
}
func (s *quizService) Delete(id int64) error {
err := s.repository.Delete(id)
return err
}
func (s *quizService) Generate(quizpaper *models.QuizPaper) ([]models.Quiz, error) {
templates, err := s.repository.List(quizpaper.ID, 1, 1000)
if err != nil {
return nil, err
}
var quizzes []models.Quiz
for _, q := range *templates {
quiz := models.Quiz{
GUID: uuid.NewString(),
CenterID: quizpaper.CenterID,
QuizPaperID: quizpaper.ID,
No: q.No,
QuestionType: q.QuestionType,
Question: q.Question,
Content: q.Content,
Answer: q.Answer,
UpdatedAt: time.Now(),
CreatedAt: time.Now(),
}
quizzes = append(quizzes, quiz)
}
return quizzes, nil
}