RezinaLMS-BackEnd/internal/repository/exam_prep_assessments.go

63 lines
1.7 KiB
Go

package repository
import (
"context"
"Rezina-Backend/internal/domain"
)
func (s *Store) LinkExamPrepAssessment(ctx context.Context, questionSetID int64, parent domain.ExamPrepAssessmentParent) error {
_, err := s.conn.Exec(ctx, `
INSERT INTO exam_prep.assessment_parents (question_set_id, parent_kind, parent_id)
VALUES ($1, $2, $3)
ON CONFLICT (question_set_id, parent_kind, parent_id) DO NOTHING
`, questionSetID, parent.Kind, parent.ID)
return err
}
func (s *Store) ListExamPrepAssessmentsByParent(ctx context.Context, parent domain.ExamPrepAssessmentParent) ([]domain.QuestionSet, error) {
rows, err := s.conn.Query(ctx, `
SELECT question_set_id
FROM exam_prep.assessment_parents
WHERE parent_kind = $1 AND parent_id = $2
ORDER BY created_at, question_set_id
`, parent.Kind, parent.ID)
if err != nil {
return nil, err
}
defer rows.Close()
ids := make([]int64, 0)
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
sets := make([]domain.QuestionSet, 0, len(ids))
for _, id := range ids {
set, err := s.GetQuestionSetByID(ctx, id)
if err != nil {
return nil, err
}
sets = append(sets, set)
}
return sets, nil
}
func (s *Store) UnlinkExamPrepAssessment(ctx context.Context, questionSetID int64, parent domain.ExamPrepAssessmentParent) (bool, error) {
result, err := s.conn.Exec(ctx, `
DELETE FROM exam_prep.assessment_parents
WHERE question_set_id = $1 AND parent_kind = $2 AND parent_id = $3
`, questionSetID, parent.Kind, parent.ID)
if err != nil {
return false, err
}
return result.RowsAffected() > 0, nil
}