Spaces:
Runtime error
Runtime error
File size: 5,302 Bytes
b55a115 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | package services
import (
"context"
"errors"
entity "abdanhafidz.com/go-boilerplate/models/entity"
"abdanhafidz.com/go-boilerplate/repositories"
"github.com/google/uuid"
)
var (
ErrProblemSetNotFound = errors.New("problem set not found")
ErrQuestionNotFound = errors.New("question not found")
)
type ProblemSetService interface {
CreateProblemSet(ctx context.Context, ps entity.ProblemSet) error
GetProblemSet(ctx context.Context, id uuid.UUID) (entity.ProblemSet, error)
ListProblemSets(ctx context.Context) ([]entity.ProblemSet, error)
UpdateProblemSet(ctx context.Context, ps entity.ProblemSet) error
DeleteProblemSet(ctx context.Context, id uuid.UUID) error
AddQuestion(ctx context.Context, q entity.Questions) error
UpdateQuestion(ctx context.Context, q entity.Questions) error
DeleteQuestion(ctx context.Context, qID uuid.UUID) error
ListQuestions(ctx context.Context, psID uuid.UUID) ([]entity.Questions, error)
AssignProblemSetToExam(ctx context.Context, examId uuid.UUID, problemSetId uuid.UUID) error
RemoveAssignedProblemSet(ctx context.Context, assignId uuid.UUID) error
GetQuestionById(ctx context.Context, qID uuid.UUID) (entity.Questions, error)
ListQuestionsByExam(ctx context.Context, examId uuid.UUID) ([]entity.Questions, error)
}
type problemSetService struct {
problemSetRepository repositories.ProblemSetRepository
questionsRepository repositories.QuestionsRepository
problemSetExamAssignRepository repositories.ProblemSetExamAssignRepository
}
func NewProblemSetService(
problemSetRepository repositories.ProblemSetRepository,
questionsRepository repositories.QuestionsRepository,
problemSetExamAssignRepository repositories.ProblemSetExamAssignRepository,
) ProblemSetService {
return &problemSetService{
problemSetRepository: problemSetRepository,
questionsRepository: questionsRepository,
problemSetExamAssignRepository: problemSetExamAssignRepository,
}
}
// ---------------- Problem Set CRUD ----------------
func (s *problemSetService) CreateProblemSet(ctx context.Context, ps entity.ProblemSet) error {
return s.problemSetRepository.Create(ctx, ps)
}
func (s *problemSetService) GetProblemSet(ctx context.Context, id uuid.UUID) (entity.ProblemSet, error) {
ps, err := s.problemSetRepository.Get(ctx, id)
if err != nil {
return entity.ProblemSet{}, ErrProblemSetNotFound
}
return ps, nil
}
func (s *problemSetService) ListProblemSets(ctx context.Context) ([]entity.ProblemSet, error) {
return s.problemSetRepository.List(ctx)
}
func (s *problemSetService) UpdateProblemSet(ctx context.Context, ps entity.ProblemSet) error {
_, err := s.problemSetRepository.Get(ctx, ps.Id)
if err != nil {
return ErrProblemSetNotFound
}
return s.problemSetRepository.Update(ctx, ps)
}
func (s *problemSetService) DeleteProblemSet(ctx context.Context, id uuid.UUID) error {
_, err := s.problemSetRepository.Get(ctx, id)
if err != nil {
return ErrProblemSetNotFound
}
return s.problemSetRepository.Delete(ctx, id)
}
// ---------------- Questions ----------------
func (s *problemSetService) AddQuestion(ctx context.Context, q entity.Questions) error {
_, err := s.problemSetRepository.Get(ctx, q.ProblemSetId)
if err != nil {
return ErrProblemSetNotFound
}
return s.questionsRepository.Create(ctx, q)
}
func (s *problemSetService) UpdateQuestion(ctx context.Context, q entity.Questions) error {
_, err := s.questionsRepository.Get(ctx, q.Id)
if err != nil {
return ErrQuestionNotFound
}
return s.questionsRepository.Update(ctx, q)
}
func (s *problemSetService) DeleteQuestion(ctx context.Context, qID uuid.UUID) error {
_, err := s.questionsRepository.Get(ctx, qID)
if err != nil {
return ErrQuestionNotFound
}
return s.questionsRepository.Delete(ctx, qID)
}
func (s *problemSetService) ListQuestions(ctx context.Context, psID uuid.UUID) ([]entity.Questions, error) {
_, err := s.problemSetRepository.Get(ctx, psID)
if err != nil {
return nil, ErrProblemSetNotFound
}
return s.questionsRepository.ListByProblemSet(ctx, psID)
}
// ---------------- Exam ↔ Problem Set (Mapping Table) ----------------
func (s *problemSetService) AssignProblemSetToExam(ctx context.Context, examId uuid.UUID, problemSetId uuid.UUID) error {
_, err := s.problemSetRepository.Get(ctx, problemSetId)
if err != nil {
return ErrProblemSetNotFound
}
assign := entity.ProblemSetExamAssign{
Id: uuid.New(),
ExamId: examId,
ProblemSetId: problemSetId,
}
return s.problemSetExamAssignRepository.Create(ctx, assign)
}
func (s *problemSetService) RemoveAssignedProblemSet(ctx context.Context, assignId uuid.UUID) error {
return s.problemSetExamAssignRepository.Delete(ctx, assignId)
}
func (s *problemSetService) GetQuestionById(ctx context.Context, qID uuid.UUID) (entity.Questions, error) {
question, err := s.questionsRepository.Get(ctx, qID)
if err != nil {
return entity.Questions{}, err
}
return question, err
}
func (s *problemSetService) ListQuestionsByExam(ctx context.Context, examId uuid.UUID) ([]entity.Questions, error) {
assign, err := s.problemSetExamAssignRepository.GetByExam(ctx, examId)
if err != nil {
return []entity.Questions{}, err
}
return s.questionsRepository.ListByProblemSet(ctx, assign.ProblemSetId)
}
|