[Updated] Frontend now uses categories retrieved from the backend

This commit is contained in:
Ninjaws
2024-11-03 13:29:52 +01:00
parent c26c298a41
commit b82dfdbe17
12 changed files with 80 additions and 58 deletions
@@ -1,5 +1,6 @@
package com.ninjaws.quiz.controllers;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,9 +12,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ninjaws.quiz.entities.Category;
import com.ninjaws.quiz.models.Answers;
import com.ninjaws.quiz.models.QuizSettings;
import com.ninjaws.quiz.models.Session;
import com.ninjaws.quiz.services.DataService;
import com.ninjaws.quiz.services.QuizService;
@RestController
@@ -22,6 +25,8 @@ public class QuizController {
@Autowired
private QuizService quizService;
@Autowired
private DataService dataService;
/**
@@ -69,4 +74,9 @@ public class QuizController {
String jsonString = String.format("{\"score\":\"%s\"}", quizService.getScore(answers));
return jsonString;
}
@GetMapping("/categories")
public List<Category> getCategories(){
return dataService.getCategories();
}
}
@@ -50,6 +50,9 @@ public class DataService {
return categoryRepository.saveAll(categories);
}
public List<Category> getCategories(){
return categoryRepository.findAll();
}
/**
* Moving the collected questions to the questions list
*
@@ -24,6 +24,8 @@ public class QuizService {
private DataService dataService;
@Autowired
private CleanerService cleanerService;
@Autowired
private StateManagementService stateManager;
/**
* Actions:
@@ -38,19 +40,20 @@ public class QuizService {
/** First, we check if there are already enough items in our db */
List<QuestionEntity> questionsRetrieved = dataService.lookupQuestions(quizSettings);
/** If the items are not matching the required amount, and the db is not currently being filled up, then they simply don't exist */
if (questionsRetrieved.size() < quizSettings.getAmount() && stateManager.getCurrentProcessingState() != StateManagementService.ProcessingState.STARTUP) {
session.setStatusCode(1);
cacheService.addToCache(session.getId(), session);
return Optional.of(session.getId());
}
/** All items are collected in from the database, it doesn't need to be added to the queue */
if(!questionsRetrieved.isEmpty() && questionsRetrieved.size() == quizSettings.getAmount()) {
session.setQuestions(cleanerService.questionListEntitytoDTO(questionsRetrieved));
}
cacheService.addToCache(session.getId(), session);
/**
* If the questions were retrieved from the DB, then it doesn't need to be added to the queue
*/
if(!questionsRetrieved.isEmpty() && questionsRetrieved.size() == quizSettings.getAmount()) {
cacheService.addToCache(session.getId(), session);
return Optional.of(session.getId());
}
cacheService.addToCache(session.getId(), session);
boolean addedToQueue = queueService.addToQueue(new Request(session.getId(), quizSettings));
/** In this order, because doing the queue before the cache could lead to race conditions (request completion immediately calls cache, but then the item doesn't exist) */
if(!addedToQueue) {
@@ -75,16 +78,17 @@ public class QuizService {
throw new IllegalArgumentException("Invalid session ID: " + sessionId);
}
/** Anything other than statuscode 0 doesn't result in active sessions, so remove them from the cache after reporting to the frontend */
if(session.getStatusCode() != 0) {
cacheService.removeFromCache(session.getId());
return Optional.of(session);
}
/** Still waiting for a response */
if (session.getQuestions() == null) {
return Optional.empty();
}
/** Anything other than statuscode 0 doesn't result in active sessions, so remove them from the cache after reporting to the frontend */
// if(session.getStatusCode() != 0) {
/** Regardless of status code, this link is not reusable */
cacheService.removeFromCache(session.getId());
// }
return Optional.of(session);
}
@@ -93,7 +97,7 @@ public class QuizService {
* Receives: sessionId and answers
*
* Actions:
* - retrieves session from cache
* - Retrieves session from cache
* - Deletes session from cache
* - Matches answers answers to correctAnswers
* - Calculates score
@@ -3,6 +3,7 @@ import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
/** Does not work, use the LoadTestMultiple.sh in the /scripts folder in the root instead */
class QuestionStatusSimulation extends Simulation {
val httpProtocol = http.baseUrl("http://localhost:9090").acceptHeader("application/json")