Skip to content

Commit

Permalink
feat(quiz): update quiz endpoint to restrain the visibility to the st…
Browse files Browse the repository at this point in the history
…udent

Closes #102
  • Loading branch information
michaelcoll committed Jun 11, 2023
1 parent c603f46 commit fd43011
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 268 deletions.
35 changes: 2 additions & 33 deletions db/queries/quiz.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,7 @@ SET active = 0
WHERE filename = ?
AND version <> ?;

-- name: FindQuizBySha1 :one
SELECT *
FROM quiz
WHERE sha1 = ?;

-- name: FindQuizFullBySha1 :many
SELECT q.sha1 AS quiz_sha1,
q.filename AS quiz_filename,
q.name AS quiz_name,
q.version AS quiz_version,
q.created_at AS quiz_created_at,
q.duration AS quiz_duration,
q.active AS quiz_active,
qq.sha1 AS question_sha1,
qq.content AS question_content,
qa.sha1 AS answer_sha1,
qa.content AS answer_content,
qa.valid AS answer_valid
FROM quiz q
JOIN quiz_question_quiz qqq ON q.sha1 = qqq.quiz_sha1
JOIN quiz_question qq ON qq.sha1 = qqq.question_sha1
JOIN quiz_question_answer qqa ON qq.sha1 = qqa.question_sha1
JOIN quiz_answer qa ON qa.sha1 = qqa.answer_sha1
WHERE q.sha1 = ?;

-- name: FindQuizFullBySha1RestrictedToClass :many
SELECT q.sha1 AS quiz_sha1,
q.filename AS quiz_filename,
q.name AS quiz_name,
Expand All @@ -71,7 +46,7 @@ FROM quiz q
JOIN student_class sc ON sc.uuid = qcv.class_uuid
JOIN user u ON sc.uuid = u.class_uuid
WHERE q.sha1 = ?
AND u.id = ?;
AND u.id IS NULL OR u.id = ?;

-- name: FindQuizByFilenameAndLatestVersion :one
SELECT *
Expand All @@ -82,18 +57,12 @@ LIMIT 1;

-- name: FindAllActiveQuiz :many
SELECT *
FROM quiz
WHERE active = 1
LIMIT ? OFFSET ?;

-- name: FindAllActiveQuizRestrictedToClass :many
SELECT *
FROM quiz q
JOIN quiz_class_visibility qcv ON q.sha1 = qcv.quiz_sha1
JOIN student_class sc ON sc.uuid = qcv.class_uuid
JOIN user u ON sc.uuid = u.class_uuid
WHERE q.active = 1
AND u.id = ?
AND u.id IS NULL OR u.id = ?
LIMIT ? OFFSET ?;

-- name: CountAllActiveQuiz :one
Expand Down
88 changes: 88 additions & 0 deletions internal/back/domain/mock_ClassRepository_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 30 additions & 83 deletions internal/back/domain/mock_QuizRepository_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/back/domain/quizService.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func NewQuizService(r QuizRepository) QuizService {
return QuizService{r: r}
}

func (s *QuizService) FindFullBySha1(ctx context.Context, sha1 string) (*Quiz, error) {
quiz, err := s.r.FindFullBySha1(ctx, sha1)
func (s *QuizService) FindFullBySha1(ctx context.Context, sha1 string, userId string) (*Quiz, error) {
quiz, err := s.r.FindFullBySha1(ctx, sha1, userId)
if err != nil {
return nil, err
}

return quiz, nil
}

func (s *QuizService) FindAllActive(ctx context.Context, limit uint16, offset uint16) ([]*Quiz, uint32, error) {
quizzes, err := s.r.FindAllActive(ctx, limit, offset)
func (s *QuizService) FindAllActive(ctx context.Context, userId string, limit uint16, offset uint16) ([]*Quiz, uint32, error) {
quizzes, err := s.r.FindAllActive(ctx, userId, limit, offset)
if err != nil {
return nil, 0, err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/back/domain/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import (

//go:generate mockery --name QuizRepository
type QuizRepository interface {
FindBySha1(ctx context.Context, sha1 string) (*Quiz, error)
FindFullBySha1(ctx context.Context, sha1 string) (*Quiz, error)
FindFullBySha1(ctx context.Context, sha1 string, userId string) (*Quiz, error)
FindLatestVersionByFilename(ctx context.Context, filename string) (*Quiz, error)
FindAllActive(ctx context.Context, limit uint16, offset uint16) ([]*Quiz, error)
FindAllActive(ctx context.Context, userId string, limit uint16, offset uint16) ([]*Quiz, error)
CountAllActive(ctx context.Context) (uint32, error)
Create(ctx context.Context, quiz *Quiz) error
ActivateOnlyVersion(ctx context.Context, filename string, version int) error
Expand Down
Loading

0 comments on commit fd43011

Please sign in to comment.