Skip to content

Commit

Permalink
Merge pull request #104 from Arquisoft/hotfix/questionLanguage
Browse files Browse the repository at this point in the history
Hotfix/question language
  • Loading branch information
Toto-hitori authored Mar 11, 2024
2 parents 02944f0 + adacaa7 commit e91dc9f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,20 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questions")
@RequiredArgsConstructor
public class QuestionController {
private final QuestionService questionService;

// TODO: REMOVE WHEN NOT USED FOR TESTING
@GetMapping
private ResponseEntity<List<QuestionResponseDto>> getQuestions() {
return ResponseEntity.ok(questionService.getQuestions());
}

@PostMapping("/{questionId}/answer")
private ResponseEntity<AnswerCheckResponseDto> answerQuestion(@PathVariable @PositiveOrZero Long questionId, @Valid @RequestBody AnswerDto answerDto){
return ResponseEntity.ok(questionService.answerQuestion(questionId,answerDto));
}

@GetMapping("/new")
private ResponseEntity<QuestionResponseDto> generateQuestion(){
return ResponseEntity.ok(questionService.getRandomQuestion());
private ResponseEntity<QuestionResponseDto> generateQuestion(@RequestParam(required = false) String lang){
return ResponseEntity.ok(questionService.getRandomQuestion(lang));
}

@GetMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import org.springframework.data.jpa.repository.Query;

public interface QuestionRepository extends JpaRepository<Question,Long> {
@Query(value = "SELECT * FROM questions ORDER BY RANDOM() LIMIT 1", nativeQuery = true)
Question findRandomQuestion();
@Query(value = "SELECT q FROM questions WHERE q.language=?1 ORDER BY RANDOM() LIMIT 1", nativeQuery = true)
Question findRandomQuestion(String lang);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public class QuestionService {
private final QuestionRepository questionRepository;
private final QuestionResponseDtoMapper questionResponseDtoMapper;

public List<QuestionResponseDto> getQuestions() {
return questionRepository.findAll().stream().map(questionResponseDtoMapper).toList();
}

public AnswerCheckResponseDto answerQuestion(Long id, AnswerDto answerDto) {
Question question = questionRepository.findById(id).orElseThrow();
if(question.getCorrectAnswer().getId().equals(answerDto.getAnswerId())){
Expand All @@ -37,8 +33,11 @@ else if(question.getAnswers().stream().noneMatch(i -> i.getId().equals(answerDto
}
}

public QuestionResponseDto getRandomQuestion() {
return questionResponseDtoMapper.apply(questionRepository.findRandomQuestion());
public QuestionResponseDto getRandomQuestion(String lang) {
if (lang==null || lang.isBlank()) {
lang = "en";
}
return questionResponseDtoMapper.apply(questionRepository.findRandomQuestion(lang));
}

public QuestionResponseDto getQuestionById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,26 @@ public class QuestionControllerTest {
QuestionService questionService;
@MockBean
UserService userService;

@Test
void getQuestionNoAuthShouldReturn403() throws Exception {
mockMvc.perform(get("/questions")
void newQuestionShouldReturn403() throws Exception{
mockMvc.perform(get("/questions/new?lang=en")
.contentType("application/json")
.with(csrf()))
.andExpect(status().isForbidden());
}

@Test
void getQuestionShouldReturn200() throws Exception {
mockMvc.perform(get("/questions")
void newQuestionShouldReturn200() throws Exception{
mockMvc.perform(get("/questions/new?lang=en")
.with(user("test").roles("user"))
.contentType("application/json")
.with(csrf()))
.andExpect(status().isOk());
}

@Test
void newQuestionShouldReturn403() throws Exception{
mockMvc.perform(get("/questions/new")
.contentType("application/json")
.with(csrf()))
.andExpect(status().isForbidden());
}

@Test
void newQuestionShouldReturn200() throws Exception{
void newQuestionNoLangShouldReturn200() throws Exception{
mockMvc.perform(get("/questions/new")
.with(user("test").roles("user"))
.contentType("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ void setUp() {

@Test
void testGetRandomQuestion() {
when(questionRepository.findRandomQuestion()).thenReturn(defaultQuestion);
QuestionResponseDto response = questionService.getRandomQuestion();
when(questionRepository.findRandomQuestion("en")).thenReturn(defaultQuestion);
QuestionResponseDto response = questionService.getRandomQuestion("");

assertEquals(response, defaultResponseDto);
}
Expand Down

0 comments on commit e91dc9f

Please sign in to comment.