Skip to content

Commit

Permalink
feat: question cat has a proper dto
Browse files Browse the repository at this point in the history
  • Loading branch information
Toto-hitori committed Apr 18, 2024
1 parent f9e97f1 commit 304e990
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lab.en2b.quizapi.game.Game;
import lab.en2b.quizapi.game.GameMode;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lab.en2b.quizapi.questions.question.dtos.QuestionCategoryDto;

import java.util.List;

Expand Down Expand Up @@ -54,4 +55,71 @@ public static void setGamemodeParams(Game game){
game.setRoundDuration(30);
}
}

public static List<QuestionCategoryDto> getQuestionCategories(String lang) {
if(lang == null)
lang = "en";
if(lang.equals("en"))
return getQuestionCategoriesEn();
return getQuestionCategoriesEs();
}
private static List<QuestionCategoryDto> getQuestionCategoriesEn(){
return List.of(
QuestionCategoryDto.builder()
.name("Art")
.description("Are you an art expert? Prove it!")
.internalRepresentation(QuestionCategory.ART)
.build(),
QuestionCategoryDto.builder()
.name("Music")
.description("Are you a music lover? Prove it!")
.internalRepresentation(QuestionCategory.MUSIC)
.build(),
QuestionCategoryDto.builder()
.name("Geography")
.description("Are you a geography expert? Prove it!")
.internalRepresentation(QuestionCategory.GEOGRAPHY)
.build(),
QuestionCategoryDto.builder()
.name("Sports")
.description("Are you a sports fanatic? Prove it!")
.internalRepresentation(QuestionCategory.SPORTS)
.build(),
QuestionCategoryDto.builder()
.name("Video Games")
.description("Are you a gamer? Prove it!")
.internalRepresentation(QuestionCategory.VIDEOGAMES)
.build()
);
}

private static List<QuestionCategoryDto> getQuestionCategoriesEs(){
return List.of(
QuestionCategoryDto.builder()
.name("Arte")
.description("¿Eres un experto en arte? ¡Demuéstralo!")
.internalRepresentation(QuestionCategory.ART)
.build(),
QuestionCategoryDto.builder()
.name("Música")
.description("¿Eres un melómano? ¡Demuéstralo!")
.internalRepresentation(QuestionCategory.MUSIC)
.build(),
QuestionCategoryDto.builder()
.name("Geografía")
.description("¿Eres un experto en geografía? ¡Demuéstralo!")
.internalRepresentation(QuestionCategory.GEOGRAPHY)
.build(),
QuestionCategoryDto.builder()
.name("Deportes")
.description("¿Eres un fanático de los deportes? ¡Demuéstralo!")
.internalRepresentation(QuestionCategory.SPORTS)
.build(),
QuestionCategoryDto.builder()
.name("Videojuegos")
.description("¿Eres un gamer? ¡Demuéstralo!")
.internalRepresentation(QuestionCategory.VIDEOGAMES)
.build()
);
}
}
5 changes: 3 additions & 2 deletions api/src/main/java/lab/en2b/quizapi/game/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.validation.Valid;
import lab.en2b.quizapi.game.dtos.*;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lab.en2b.quizapi.questions.question.dtos.QuestionCategoryDto;
import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -133,8 +134,8 @@ public ResponseEntity<List<GameModeDto>> getQuestionGameModes(){
@ApiResponse(responseCode = "403", description = "You are not logged in", content = @io.swagger.v3.oas.annotations.media.Content)
})
@GetMapping("/question-categories")
public ResponseEntity<List<QuestionCategory>> getQuestionCategories(){
return ResponseEntity.ok(gameService.getQuestionCategories());
public ResponseEntity<List<QuestionCategoryDto>> getQuestionCategories(@RequestParam(required = false) String lang){
return ResponseEntity.ok(gameService.getQuestionCategories(lang));
}

}
6 changes: 4 additions & 2 deletions api/src/main/java/lab/en2b/quizapi/game/GameService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package lab.en2b.quizapi.game;

import lab.en2b.quizapi.commons.user.UserService;
import lab.en2b.quizapi.commons.utils.GameModeUtils;
import lab.en2b.quizapi.game.dtos.*;
import lab.en2b.quizapi.game.mappers.GameResponseDtoMapper;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lab.en2b.quizapi.questions.question.QuestionService;
import lab.en2b.quizapi.questions.question.dtos.QuestionCategoryDto;
import lab.en2b.quizapi.questions.question.dtos.QuestionResponseDto;
import lab.en2b.quizapi.questions.question.mappers.QuestionResponseDtoMapper;
import lab.en2b.quizapi.statistics.Statistics;
Expand Down Expand Up @@ -151,8 +153,8 @@ public GameResponseDto getGameDetails(Long id, Authentication authentication) {
return gameResponseDtoMapper.apply(game);
}

public List<QuestionCategory> getQuestionCategories() {
return Arrays.asList(QuestionCategory.values());
public List<QuestionCategoryDto> getQuestionCategories(String lang) {
return GameModeUtils.getQuestionCategories(lang);
}

private boolean wasGameMeantToBeOver(Game game) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package lab.en2b.quizapi.questions.question.dtos;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lab.en2b.quizapi.questions.question.QuestionCategory;
import lombok.*;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Setter
public class QuestionCategoryDto {
@Schema(description = "Beautified name of the question category",example = "Sports")
private String name;
@Schema(description = "Description of the question category",example = "Test description of the question category")
private String description;
@JsonProperty("internal_representation")
@Schema(description = "Internal code used for describing the question category",example = "SPORTS")
private QuestionCategory internalRepresentation;
}
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public void getGameDetailsInvalidId(){

@Test
public void testGetQuestionCategories(){
assertEquals(Arrays.asList(QuestionCategory.values()), gameService.getQuestionCategories());
assertEquals(QuestionCategory.values().length, gameService.getQuestionCategories(null).size());
}

@Test
Expand Down

0 comments on commit 304e990

Please sign in to comment.