@Path("/{username}/create") @PUT public javax.ws.rs.core.Response addQuestionForUser(@PathParam("username") String userName) { List<Game> gamesAllReadyPlayed = gameUserDataManager.getGames(userName); List<Question> allQuestions = questionManager.loadQuestions().getQuestions(); if (gamesAllReadyPlayed.size() == allQuestions.size()) { // NOTHING TO DO : the player allready answered all questions return javax.ws.rs.core.Response.ok().build(); } Question randomQuestion = getRandomQuestion(gamesAllReadyPlayed, allQuestions); Game game = new Game(); game.setId(randomQuestion.getId()); game.setType(ResponseType.NEED_RESPONSE); try { gameUserDataManager.addOrUpdateGame(userName, game); } catch (StorageException e) { LOGGER.error("unable to store result in mongoDb: {}", e.getMessage()); } questionsNotifier.notifyListener(userName, randomQuestion); return javax.ws.rs.core.Response.ok().build(); }
@Path("/{username}/reply") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public ResponseResponseDto giveResponse(ResponseRequestDto responseDto) { Response res = dozerMapper.map(responseDto, Response.class); ResponseResponseDto response = new ResponseResponseDto(); Set<ConstraintViolation<Response>> constraintViolations = validator.validate(res); if (constraintViolations.size() != 0) { LOGGER.error("Invalid response from user"); response.setResponseType(ResponseType.INVALID); return response; } Question question = questionManager.loadQuestions().getQuestionById(res.getId()); List<String> answers = question.getAnswers(); List<String> responses = responseDto.getResponses(); response.setId(res.getId()); response.setAnswer(responses); response.setResponseType(ResponseType.FAIL); if (answers.size() == responses.size()) { // TODO : gerer le cas des majuscules/minuscules if (answers.containsAll(responses)) { response.setResponseType(ResponseType.SUCCESS); } } Game game = gameUserDataManager.getGameById(responseDto.getUserName(), res.getId()); if (game == null) { game = new Game(); game.setId(response.getId()); } game.setGivenAnswers(response.getAnswer()); game.setType(response.getResponseType()); try { gameUserDataManager.addOrUpdateGame(responseDto.getUserName(), game); } catch (StorageException e) { LOGGER.error("unable to store result in mongoDb: {}", e.getMessage()); } return response; }