46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"Rezina-Backend/internal/domain"
|
|
"context"
|
|
)
|
|
|
|
func (s *Store) ListChapterQuizzes(ctx context.Context, chapterID int64, publishedOnly bool, limit, offset int32) ([]domain.ExamPrepPractice, int64, error) {
|
|
var total int64
|
|
const filter = ` FROM exam_prep.lesson_practices p
|
|
JOIN question_sets qs ON qs.id=p.question_set_id
|
|
JOIN exam_prep.practice_parents pp ON pp.practice_id=p.id
|
|
WHERE pp.parent_kind='UNIT' AND pp.parent_id=$1 AND qs.set_type='QUIZ'
|
|
AND (NOT $2 OR (p.publish_status='PUBLISHED' AND qs.status='PUBLISHED'))`
|
|
if err := s.conn.QueryRow(ctx, `SELECT COUNT(*)`+filter, chapterID, publishedOnly).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rows, err := s.conn.Query(ctx, `SELECT p.id`+filter+` ORDER BY p.created_at,p.id LIMIT $3 OFFSET $4`, chapterID, publishedOnly, limit, offset)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
ids := []int64{}
|
|
for rows.Next() {
|
|
var id int64
|
|
if err = rows.Scan(&id); err != nil {
|
|
rows.Close()
|
|
return nil, 0, err
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
err = rows.Err()
|
|
rows.Close()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
items := make([]domain.ExamPrepPractice, 0, len(ids))
|
|
for _, id := range ids {
|
|
p, err := s.GetExamPrepLessonPracticeByID(ctx, id)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
items = append(items, p)
|
|
}
|
|
return items, total, nil
|
|
}
|