Skip to content

Commit

Permalink
Merge pull request #160 from dnd-side-project/feature/#154
Browse files Browse the repository at this point in the history
Add querying the number of quiz participants api
  • Loading branch information
miraexhoi authored Sep 24, 2024
2 parents 73987e2 + 04cb39b commit ab1af1f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public QuizResponseDto getQuiz(Long quizId) {
return QuizServiceMapper.toResponse(quiz.getId(), quiz.getQuestions());
}

public long getParticipantsCountToday() {
return quizResultRepository.countParticipantsToday();
}

@Transactional
public List<QuizResult> submitAnswers(Long quizId, QuizRequestDto requestDto) {
List<QuizQuestion> questions = findQuizById(quizId).getQuestions();
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/dnd/spaced/domain/quiz/domain/QuizResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.persistence.Id;
import lombok.*;

import java.time.LocalDate;

@Entity
@Getter
@EqualsAndHashCode(of = "id")
Expand All @@ -17,6 +19,8 @@ public class QuizResult {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private Long accountId;

@ManyToOne
@JoinColumn(name = "quiz_question_id")
private QuizQuestion quizQuestion;
Expand All @@ -27,14 +31,20 @@ public class QuizResult {

private boolean isCorrect;

private LocalDate createdAt;

@Builder
private QuizResult(
QuizQuestion quizQuestion,
QuizOption selectedOption,
boolean isCorrect
boolean isCorrect,
Long accountId,
LocalDate createdAt
) {
this.quizQuestion = quizQuestion;
this.selectedOption = selectedOption;
this.isCorrect = isCorrect;
this.accountId = accountId;
this.createdAt = createdAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.dnd.spaced.domain.quiz.domain.QuizResult;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface QuizResultRepository extends CrudRepository<QuizResult, Long> {
@Query("SELECT COUNT(DISTINCT qr.accountId) FROM QuizResult qr WHERE DATE(qr.createdAt) = CURRENT_DATE")
long countParticipantsToday();
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public ResponseEntity<List<QuizResult>> submitQuiz(@PathVariable Long id,
List<QuizResult> results = quizService.submitAnswers(id, requestDto);
return ResponseEntity.ok(results);
}

@GetMapping("/today/participants")
public ResponseEntity<Long> getParticipantsCountToday() {
long participantsCount = quizService.getParticipantsCountToday();
return ResponseEntity.ok(participantsCount);
}
}

0 comments on commit ab1af1f

Please sign in to comment.