Skip to content

Commit

Permalink
Añadidos api docs y traducción
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelayori committed Mar 4, 2024
1 parent abd7f41 commit 2940287
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 21 deletions.
37 changes: 23 additions & 14 deletions src/main/java/com/uniovi/services/InsertSampleDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,39 @@ public class InsertSampleDataService {
private final QuestionService questionService;
private final CategoryService categoryService;
private final QuestionRepository questionRepository;
private final GameSessionRepository gameSessionRepository;

public InsertSampleDataService(PlayerService playerService, QuestionService questionService,
CategoryService categoryService, QuestionRepository questionRepository) {
CategoryService categoryService, QuestionRepository questionRepository,
GameSessionRepository gameSessionRepository) {
this.playerService = playerService;
this.questionService = questionService;
this.categoryService = categoryService;
this.questionRepository = questionRepository;
this.gameSessionRepository = gameSessionRepository;
}

@PostConstruct
public void init() {
if (playerService.getUserByEmail("[email protected]").isPresent())
return;

PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
player.setUsername("test");
player.setPassword("test");
player.setRoles(new String[]{"ROLE_USER"});
playerService.generateApiKey(playerService.addNewPlayer(player));
}

@Transactional
@EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup
public void insertSampleQuestions() {
if (!playerService.getUserByEmail("[email protected]").isPresent()) {
PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
player.setUsername("test");
player.setPassword("test");
player.setRoles(new String[]{"ROLE_USER"});
playerService.generateApiKey(playerService.addNewPlayer(player));
}

GameSession gameSession = new GameSession();
gameSession.setFinishTime(LocalDateTime.now().plusMinutes(5));
gameSession.setCreatedAt(LocalDateTime.now());
gameSession.setTotalQuestions(40);
gameSession.setCorrectQuestions(10);
gameSession.setPlayer(playerService.getUserByEmail("[email protected]").get());
playerService.getUserByEmail("[email protected]").get().getGameSessions().add(gameSession);
gameSessionRepository.save(gameSession);

questionRepository.deleteAll();

QuestionGenerator border = new BorderQuestionGenerator(categoryService);
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,26 @@ signup.passwordConfirm.placeholder=Repita la contraseña
signup.submit=Registrarse
signup.title=Regístrate

# -------------------Statements for the apiHome.html file---------------------
api.doc.title=Documentación de la API
api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso.
api.doc.table.parameters=Parámetros
api.doc.table.description=Descripción
api.doc.table.example=Ejemplo
api.doc.endpoints.players.title=Extremo /api/players
api.doc.endpoints.players.description=Devuelve una lista de jugadores filtrada por los criterios especificados.
api.doc.endpoints.questions.title=Extremo /api/questions
api.doc.endpoints.questions.description=Devuelve una lista de preguntas filtrada por los criterios especificados.
api.doc.exampleRequest=Ejemplo de petición
api.doc.exampleResponse=Ejemplo de respuesta
api.doc.apikey=Clave de la API (obligatorio)

api.doc.player.username=Nombre de usuario (opcional)
api.doc.player.email=Correo electrónico (opcional)
api.doc.player.id=ID del jugador en el sistema (opcional)
api.doc.player.usernames=Nombre de usuario, separados por comas (opcional)
api.doc.player.emails=Correos electrónicos, separados por comas (opcional)

api.doc.question.category=Categorña (opcional). Nombre o ID de la categoría.
api.doc.question.id=ID de la pregunta (opcional)
api.doc.question.statement=Enunciado de la pregunta (opcional). Texto que debe contener el enunciado de la pregunta.
27 changes: 26 additions & 1 deletion src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ navbar.play=Play
navbar.game1=Game 1
navbar.game2=Game 2
navbar.history=History
navbar.changeLanguage=Idiom
navbar.changeLanguage=Language
navbar.toEnglish=English
navbar.toSpanish=Spanish

Expand Down Expand Up @@ -57,6 +57,31 @@ signup.passwordConfirm.placeholder=Confirm your password
signup.submit=Sign up
signup.title=Sign up

# -------------------Statements for the apiHome.html file---------------------
api.doc.title=API Documentation
api.doc.description=This document describes the REST API endpoints.
api.doc.table.parameters=Parameters
api.doc.table.description=Description
api.doc.table.example=Example
api.doc.endpoints.players.title=/api/players Endpoint
api.doc.endpoints.players.description=Retrieves a list of players based on the given parameters.
api.doc.endpoints.questions.title=/api/questions Endpoint
api.doc.endpoints.questions.description=Fetches a list of questions filtered by the specified criteria.
api.doc.exampleRequest=Example Request
api.doc.exampleResponse=Example Response
api.doc.apikey=Your API Key (required)

api.doc.player.username=Username (optional)
api.doc.player.email=Email (optional)
api.doc.player.id=Player ID in the system (optional)
api.doc.player.usernames=Usernames, comma separated (optional)
api.doc.player.emails=Emails, comma separated (optional)

api.doc.question.category=Category (optional). Category ID or name.
api.doc.question.id=Question ID in the system (optional)
api.doc.question.statement=Statement (optional). Text to search in the question statement.





22 changes: 22 additions & 0 deletions src/main/resources/messages_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,26 @@ signup.passwordConfirm.placeholder=Repita la contraseña
signup.submit=Registrarse
signup.title=Regístrate

# -------------------Statements for the apiHome.html file---------------------
api.doc.title=Documentación de la API
api.doc.description=Esta es la documentación de la API de WIQ. Aquí puedes encontrar información sobre los recursos disponibles, los parámetros que aceptan y los ejemplos de uso.
api.doc.table.parameters=Parámetros
api.doc.table.description=Descripción
api.doc.table.example=Ejemplo
api.doc.endpoints.players.title=Extremo /api/players
api.doc.endpoints.players.description=Devuelve una lista de jugadores filtrada por los criterios especificados.
api.doc.endpoints.questions.title=Extremo /api/questions
api.doc.endpoints.questions.description=Devuelve una lista de preguntas filtrada por los criterios especificados.
api.doc.exampleRequest=Ejemplo de petición
api.doc.exampleResponse=Ejemplo de respuesta
api.doc.apikey=Clave de la API (obligatorio)

api.doc.player.username=Nombre de usuario (opcional)
api.doc.player.email=Correo electrónico (opcional)
api.doc.player.id=ID del jugador en el sistema (opcional)
api.doc.player.usernames=Nombre de usuario, separados por comas (opcional)
api.doc.player.emails=Correos electrónicos, separados por comas (opcional)

api.doc.question.category=Categorña (opcional). Nombre o ID de la categoría.
api.doc.question.id=ID de la pregunta (opcional)
api.doc.question.statement=Enunciado de la pregunta (opcional). Texto que debe contener el enunciado de la pregunta.
206 changes: 200 additions & 6 deletions src/main/resources/templates/api/apiHome.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,204 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{fragments/head}">
<body>
<style>
.api-table th, .api-table td {
vertical-align: top;
text-align: left;
}
.example-response {
background-color: #f0f4f8; /* Light grey-blue background for readability */
border-radius: 0.25rem; /* Rounded corners */
padding: 1rem; /* Padding inside the box */
border: 1px solid #d3d3d3; /* Light grey border */
}
</style>
<nav th:replace="~{fragments/nav}"></nav>

<div class="container-fluid mt-5">
<div class="row">
<!-- Lateral Navigation Sidebar -->
<div class="col-md-2">
<h2 th:text="#{api.doc.title}"></h2>
<p th:text="#{api.doc.description}"></p>
<div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
<a class="nav-link active" id="v-pills-players-tab" data-toggle="pill" href="#v-pills-players" role="tab" aria-controls="v-pills-players" aria-selected="true">/api/players</a>
<a class="nav-link" id="v-pills-questions-tab" data-toggle="pill" href="#v-pills-questions" role="tab" aria-controls="v-pills-questions" aria-selected="false">/api/questions</a>
</div>
</div>

<!-- Main Content Area -->
<div class="col-md-10">
<div class="tab-content" id="v-pills-tabContent">
<!-- /api/players Endpoint -->
<div class="tab-pane fade show active" id="v-pills-players" role="tabpanel" aria-labelledby="v-pills-players-tab">
<h2 th:text="#{api.doc.endpoints.players.title}"></h2>
<p th:text="#{api.doc.endpoints.players.description}"></p>

<table class="table api-table">
<thead class="thead-light">
<tr>
<th th:text="#{api.doc.table.parameters}"></th>
<th th:text="#{api.doc.table.description}"></th>
<th th:text="#{api.doc.table.example}"></th>
</tr>
</thead>
<tbody>
<tr>
<td>apiKey</td>
<td th:text="#{api.doc.apikey}"></td>
<td>4640gdfsj4654-hi54egs</td>
</tr>
<tr>
<td>username</td>
<td th:text="#{api.doc.player.username}"></td>
<td>student1</td>
</tr>
<tr>
<td>email</td>
<td th:text="#{api.doc.player.email}"></td>
<td>[email protected]</td>
</tr>
<tr>
<td>id</td>
<td th:text="#{api.doc.player.id}"></td>
<td>1</td>
</tr>
<tr>
<td>usernames</td>
<td th:text="#{api.doc.player.usernames}"></td>
<td>student1,student2</td>
</tr>
<tr>
<td>emails</td>
<td th:text="#{api.doc.player.emails}"></td>
<td>[email protected],[email protected]</td>
</tr>
</tbody>
</table>

<div class="example-response">
<h3 th:text="#{api.doc.exampleRequest}"></h3>
<code>GET /api/players?apiKey={api_key}&username=student1</code>
<h3 th:text="#{api.doc.exampleResponse}"></h3>
<pre class="bg-light p-3 rounded">
{
"players":[
{
"id":1,
"username":"student1",
"email":"[email protected]",
"roles":[
"ROLE_USER"
],
"gameSessions":[
{
"id":1,
"player":1,
"correctQuestions":10,
"totalQuestions":40,
"createdAt":"2024-03-04T22:44:41.067901",
"finishTime":"2024-03-04T22:49:41.067901",
"score":0
}
]
}
]
}</pre>
</div>
</div>

<!-- /api/questions Endpoint -->
<div class="tab-pane fade" id="v-pills-questions" role="tabpanel" aria-labelledby="v-pills-questions-tab">
<h2 th:text="#{api.doc.endpoints.questions.title}"></h2>
<p th:text="#{api.doc.endpoints.questions.description}"></p>

<table class="table api-table">
<thead class="thead-light">
<tr>
<th th:text="#{api.doc.table.parameters}"></th>
<th th:text="#{api.doc.table.description}"></th>
<th th:text="#{api.doc.table.example}"></th>
</tr>
</thead>
<tbody>
<tr>
<td>apiKey</td>
<td th:text="#{api.doc.apikey}"></td>
<td>4640gdfsj4654-hi54egs</td>
</tr>
<tr>
<td>category</td>
<td th:text="#{api.doc.question.category}"></td>
<td>Geography</td>
</tr>
<tr>
<td>id</td>
<td th:text="#{api.doc.question.id}"></td>
<td>1</td>
</tr>
<tr>
<td>statement</td>
<td th:text="#{api.doc.question.statement}"></td>
<td>capital</td>
</tr>
</tbody>
</table>

<div class="example-response">
<h3 th:text="#{api.doc.exampleRequest}"></h3>
<code>GET /api/questions?apiKey={api_key}&category=Geography</code>
<h3 th:text="#{api.doc.exampleResponse}"></h3>
<pre class="bg-light p-3 rounded">
{
"questions":[
{
"id":11802,
"statement":"Which countries share a border with Solomon Islands?",
"category":{
"id":1,
"name":"Geography",
"description":"Questions about geography"
},
"options":[
{
"id":46252,
"text":"Papua New Guinea",
"correct":true,
"question":11802
},
{
"id":46253,
"text":"Venezuela",
"correct":false,
"question":11802
},
{
"id":46254,
"text":"Austria",
"correct":false,
"question":11802
},
{
"id":46255,
"text":"United States of America",
"correct":false,
"question":11802
}
]
}
]
}</pre>
</div>
</div>
</div>
</div>
</div>
<!-- Adding padding at the bottom to avoid overlap with the footer -->
<div class="pb-5"></div>
</div>

<footer th:replace="~{fragments/footer}"></footer>
</body>
</html>
</html>

0 comments on commit 2940287

Please sign in to comment.