Skip to content

Commit

Permalink
[feat] quiz 구현 #32
Browse files Browse the repository at this point in the history
  • Loading branch information
yunji118 committed Jan 12, 2024
1 parent d124a0f commit 26b30fa
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ALGo.ALGo_server.quiz.Controller;

import com.ALGo.ALGo_server.quiz.Dto.QuizResponseDto;
import com.ALGo.ALGo_server.quiz.Service.QuizService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/quiz")
@RequiredArgsConstructor
public class QuizController {
private final QuizService quizService;

@GetMapping()
public QuizResponseDto getQuiz(){
return quizService.getQuiz();
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/ALGo/ALGo_server/quiz/Dto/QuizDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ALGo.ALGo_server.quiz.Dto;

import com.ALGo.ALGo_server.entity.Quiz;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
public class QuizDto {
private Long id;

public QuizDto(Quiz quiz){
this.id = quiz.getQuiz_id();
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/ALGo/ALGo_server/quiz/Dto/QuizResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ALGo.ALGo_server.quiz.Dto;

import lombok.Getter;

import java.util.List;

@Getter
public class QuizResponseDto {
List<QuizDto> quizList;

public QuizResponseDto(List<QuizDto> quizList){
this.quizList = quizList;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/ALGo/ALGo_server/quiz/Service/QuizService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.ALGo.ALGo_server.quiz.Service;


import com.ALGo.ALGo_server.entity.Quiz;
import com.ALGo.ALGo_server.quiz.Dto.QuizDto;
import com.ALGo.ALGo_server.quiz.Dto.QuizResponseDto;
import com.ALGo.ALGo_server.repository.QuizRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class QuizService {
private final QuizRepository quizRepository;

@Transactional(readOnly = true)
public QuizResponseDto getQuiz(){
Random random = new Random();

List<QuizDto> quizDtoList = new ArrayList<>();
for(int i=0;i<4;i++){
Long randomNum = Long.valueOf(random.nextInt(16) + 1);
Quiz q = quizRepository.findById(randomNum).get();
QuizDto quizDto = new QuizDto(q);
quizDtoList.add(quizDto);
}
QuizResponseDto responseDto = new QuizResponseDto(quizDtoList);

return responseDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.ALGo.ALGo_server.entity.Quiz;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

public interface QuizRepository extends JpaRepository<Quiz, Long> {

Quiz findByQuiz_id(Long quiz_id);

}

0 comments on commit 26b30fa

Please sign in to comment.