Skip to content

Commit

Permalink
Fix last issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Pelayori committed Apr 28, 2024
1 parent 5a76ba9 commit 5b6d6cb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/uniovi/controllers/PlayersController.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public PlayersController(PlayerService playerService, SignUpValidator signUpVali
this.signUpValidator = signUpValidator;
this.gameSessionService = gameSessionService;
this.roleService = roleService;
this.questionService = questionService;
}

@GetMapping("/signup")
Expand Down Expand Up @@ -269,13 +270,7 @@ public String deleteAllQuestions() throws IOException {
public String saveQuestions(HttpServletResponse response, @RequestBody String json) throws IOException {
try {
JsonNode node = new ObjectMapper().readTree(json);
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter();
printer.indentObjectsWith(indenter); // Indent JSON objects
printer.indentArraysWith(indenter); // Indent JSON arrays

ObjectMapper mapper = new ObjectMapper();
mapper.writer(printer).writeValue(new ClassPathResource(QuestionGeneratorService.JSON_FILE_PATH).getFile(), node);
questionService.setJsonGenerator(node);
return "Questions saved";
}
catch (Exception e) {
Expand All @@ -284,8 +279,16 @@ public String saveQuestions(HttpServletResponse response, @RequestBody String js
}
}

@GetMapping("/questions/getJson")
@ResponseBody
public String getJson() {
return questionService.getJsonGenerator().toString();
}

@GetMapping("/player/admin/monitoring")
public String showMonitoring() {
public String showMonitoring(HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
return "player/admin/monitoring";
}
}
16 changes: 13 additions & 3 deletions src/main/java/com/uniovi/services/QuestionGeneratorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public QuestionGeneratorService(QuestionService questionService, Environment env
}

private void parseQuestionTypes() throws IOException {
Resource resource = new ClassPathResource(JSON_FILE_PATH);
ObjectMapper objectMapper = new ObjectMapper();
json = objectMapper.readTree(resource.getInputStream());
if (json == null) {
Resource resource = new ClassPathResource(JSON_FILE_PATH);
ObjectMapper objectMapper = new ObjectMapper();
json = objectMapper.readTree(resource.getInputStream());
}
JsonNode categories = json.findValue("categories");
for (JsonNode category : categories) {
String categoryName = category.get("name").textValue();
Expand Down Expand Up @@ -127,11 +129,19 @@ public void generateTestQuestions(String cat) {
questionService.addNewQuestion(new QuestionDto(q));
}

public void setJsonGeneration(JsonNode json) {
this.json = json;
}

public void resetGeneration() throws IOException {
types.clear();
parseQuestionTypes();
}

public JsonNode getJsonGeneration() {
return json;
}

@Getter
@AllArgsConstructor
private static class QuestionType {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/uniovi/services/QuestionService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uniovi.services;

import com.fasterxml.jackson.databind.JsonNode;
import com.uniovi.dto.QuestionDto;
import com.uniovi.entities.Category;
import com.uniovi.entities.Question;
Expand Down Expand Up @@ -102,4 +103,16 @@ public interface QuestionService {
* Delete all the questions
*/
void deleteAllQuestions() throws IOException;

/**
* Set the json generator
* @param json The json generator
*/
void setJsonGenerator(JsonNode json);

/**
* Get the json generator
* @return The json generator
*/
JsonNode getJsonGenerator();
}
13 changes: 11 additions & 2 deletions src/main/java/com/uniovi/services/impl/QuestionServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uniovi.services.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.uniovi.dto.QuestionDto;
import com.uniovi.entities.Answer;
import com.uniovi.entities.Associations;
Expand All @@ -21,8 +22,6 @@

import java.io.IOException;
import java.security.SecureRandom;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -179,4 +178,14 @@ public void deleteAllQuestions() throws IOException {
questionRepository.deleteAll();
}

@Override
public void setJsonGenerator(JsonNode json) {
questionGeneratorService.setJsonGeneration(json);
}

@Override
public JsonNode getJsonGenerator() {
return questionGeneratorService.getJsonGeneration();
}

}
4 changes: 2 additions & 2 deletions src/main/resources/static/script/questionManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ function setupQuestionManagement() {
});

$.ajax({
url: '/JSON/QuestionTemplates.json',
url: '/questions/getJson',
type: 'GET',
success: function (data) {
let json = data;
let json = JSON.parse(data);
const element = document.getElementById('jsonEditorElement');
const options = {}
editor = new JSONEditor(element, options)
Expand Down

0 comments on commit 5b6d6cb

Please sign in to comment.