From 0fa6964cfce837a704157469ea25dcb9fb2f8363 Mon Sep 17 00:00:00 2001 From: Omar Yasser Date: Thu, 4 Apr 2024 22:02:35 +0100 Subject: [PATCH] Format --- .../controllers/ChallengeController.java | 163 +++++++++--------- 1 file changed, 82 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/azkar/controllers/ChallengeController.java b/src/main/java/com/azkar/controllers/ChallengeController.java index 2e13dd2..1d5983f 100644 --- a/src/main/java/com/azkar/controllers/ChallengeController.java +++ b/src/main/java/com/azkar/controllers/ChallengeController.java @@ -127,7 +127,8 @@ public static ArrayList getWordMeaningPairs(TafseerCacher tafse int lastRandomIndex = randomIndex1; for (int i = 0; i < numberOfWords - 1; i++) { int randomIndex = - (lastRandomIndex + RANDOMLY_CHOSEN_WORDS_INDEX_DIFF) % tafseerCacher.getWordMeaningPairs() + (lastRandomIndex + RANDOMLY_CHOSEN_WORDS_INDEX_DIFF) + % tafseerCacher.getWordMeaningPairs() .size(); wordMeaningPairs.add(tafseerCacher.getWordMeaningPairs().get(randomIndex)); @@ -136,6 +137,70 @@ public static ArrayList getWordMeaningPairs(TafseerCacher tafse return wordMeaningPairs; } + // Note: This function may modify oldSubChallenges. + private static Optional> updateOldSubChallenges( + List oldSubChallenges, + List newSubChallenges) { + UpdateChallengeResponse response = new UpdateChallengeResponse(); + if (newSubChallenges.size() != oldSubChallenges.size()) { + response + .setStatus(new Status(Status.MISSING_OR_DUPLICATED_SUB_CHALLENGE_ERROR)); + return Optional.of(ResponseEntity.badRequest().body(response)); + } + + // Set to make sure that the zekr IDs of both old and modified sub-challenges are identical. + Set newZekrIds = new HashSet<>(); + for (SubChallenge newSubChallenge : newSubChallenges) { + newZekrIds.add(newSubChallenge.getZekr().getId()); + Optional subChallenge = findSubChallenge(oldSubChallenges, newSubChallenge); + if (!subChallenge.isPresent()) { + response.setStatus(new Status(Status.NON_EXISTENT_SUB_CHALLENGE_ERROR)); + return Optional.of(ResponseEntity.badRequest().body(response)); + } + Optional error = updateSubChallenge(subChallenge.get(), newSubChallenge); + if (error.isPresent()) { + response.setStatus(error.get()); + return Optional.of(ResponseEntity.badRequest().body(response)); + } + } + if (newZekrIds.size() != oldSubChallenges.size()) { + response + .setStatus(new Status(Status.MISSING_OR_DUPLICATED_SUB_CHALLENGE_ERROR)); + return Optional.of(ResponseEntity.badRequest().body(response)); + } + return Optional.empty(); + } + + private static Optional findSubChallenge( + List oldSubChallenges, + SubChallenge newSubChallenge) { + for (SubChallenge subChallenge : oldSubChallenges) { + if (subChallenge.getZekr().getId().equals(newSubChallenge.getZekr().getId())) { + return Optional.of(subChallenge); + } + } + return Optional.empty(); + } + + /** + * Updates the subChallenge as requested in newSubChallenge. If an error occurred the function + * returns an error, and returns empty object otherwise. + */ + private static Optional updateSubChallenge( + SubChallenge subChallenge, + SubChallenge newSubChallenge) { + int newLeftRepetitions = newSubChallenge.getRepetitions(); + if (newLeftRepetitions > subChallenge.getRepetitions()) { + return Optional.of(new Status(Status.INCREMENTING_LEFT_REPETITIONS_ERROR)); + } + if (newLeftRepetitions < 0) { + logger.warn("Received UpdateChallenge request with negative leftRepetition value of: " + + newLeftRepetitions); + newLeftRepetitions = 0; + } + subChallenge.setRepetitions(newLeftRepetitions); + return Optional.empty(); + } @GetMapping("{challengeId}") public ResponseEntity getChallenge( @@ -176,40 +241,6 @@ public ResponseEntity getMeaningChallenge( return ResponseEntity.ok(response); } - // Note: This function may modify oldSubChallenges. - private static Optional> updateOldSubChallenges( - List oldSubChallenges, - List newSubChallenges) { - UpdateChallengeResponse response = new UpdateChallengeResponse(); - if (newSubChallenges.size() != oldSubChallenges.size()) { - response - .setStatus(new Status(Status.MISSING_OR_DUPLICATED_SUB_CHALLENGE_ERROR)); - return Optional.of(ResponseEntity.badRequest().body(response)); - } - - // Set to make sure that the zekr IDs of both old and modified sub-challenges are identical. - Set newZekrIds = new HashSet<>(); - for (SubChallenge newSubChallenge : newSubChallenges) { - newZekrIds.add(newSubChallenge.getZekr().getId()); - Optional subChallenge = findSubChallenge(oldSubChallenges, newSubChallenge); - if (!subChallenge.isPresent()) { - response.setStatus(new Status(Status.NON_EXISTENT_SUB_CHALLENGE_ERROR)); - return Optional.of(ResponseEntity.badRequest().body(response)); - } - Optional error = updateSubChallenge(subChallenge.get(), newSubChallenge); - if (error.isPresent()) { - response.setStatus(error.get()); - return Optional.of(ResponseEntity.badRequest().body(response)); - } - } - if (newZekrIds.size() != oldSubChallenges.size()) { - response - .setStatus(new Status(Status.MISSING_OR_DUPLICATED_SUB_CHALLENGE_ERROR)); - return Optional.of(ResponseEntity.badRequest().body(response)); - } - return Optional.empty(); - } - @GetMapping("/original/{challengeId}") public ResponseEntity getOriginalChallenge( @PathVariable(value = "challengeId") String challengeId) { @@ -630,18 +661,6 @@ public ResponseEntity addReadingQuranChallenge return ResponseEntity.ok(response); } - private static Optional findSubChallenge( - List oldSubChallenges, - SubChallenge newSubChallenge) { - for (SubChallenge subChallenge : oldSubChallenges) { - if (subChallenge.getZekr().getId().equals(newSubChallenge.getZekr().getId())) { - return Optional.of(subChallenge); - } - } - return Optional.empty(); - } - - // Returns all challenges with all types. @GetMapping(path = "/v2") public ResponseEntity getAllChallengesV2( @@ -670,7 +689,6 @@ public ResponseEntity getAllChallengesV2( return ResponseEntity.ok(response); } - @GetMapping(path = "/groups/{groupId}/") public ResponseEntity getAllChallengesInGroup( @PathVariable(value = "groupId") String groupId) { @@ -693,7 +711,6 @@ public ResponseEntity getAllChallengesInGroup( return ResponseEntity.ok(response); } - // TODO(issue/204): This is not an atomic operation anymore, i.e. it is not guranteed that if // something wrong happened in the middle of handling a request, the state will remain the // same as before doing the request. @@ -805,9 +822,10 @@ public ResponseEntity finishReadingQuranCha User currentUser = getCurrentUser(userRepo); Optional currentUserChallenge = currentUser.getReadingQuranChallenges() .stream() - .filter(challenge -> challenge.getId() - .equals( - challengeId)) + .filter( + challenge -> challenge.getId() + .equals( + challengeId)) .findFirst(); if (!currentUserChallenge.isPresent()) { FinishReadingQuranChallengeResponse response = new FinishReadingQuranChallengeResponse(); @@ -849,16 +867,16 @@ public ResponseEntity finishReadingQuranCha // question is 0-based. @PutMapping(path = "/finish/memorization/{challengeId}/{question}") - public ResponseEntity - finishMemorizationChallengeQuestion( + public ResponseEntity finishMemorizationQuestion( @PathVariable(value = "challengeId") String challengeId, @PathVariable(value = "question") String question) { User currentUser = getCurrentUser(userRepo); Optional currentUserChallenge = currentUser.getMemorizationChallenges() .stream() - .filter(challenge -> challenge.getId() - .equals( - challengeId)) + .filter( + challenge -> challenge.getId() + .equals( + challengeId)) .findFirst(); if (!currentUserChallenge.isPresent()) { FinishMemorizationChallengeQuestionResponse response = @@ -950,26 +968,6 @@ public ResponseEntity finishGlobalChallenge() { return ResponseEntity.ok(response); } - /** - * Updates the subChallenge as requested in newSubChallenge. If an error occurred the function - * returns an error, and returns empty object otherwise. - */ - private static Optional updateSubChallenge( - SubChallenge subChallenge, - SubChallenge newSubChallenge) { - int newLeftRepetitions = newSubChallenge.getRepetitions(); - if (newLeftRepetitions > subChallenge.getRepetitions()) { - return Optional.of(new Status(Status.INCREMENTING_LEFT_REPETITIONS_ERROR)); - } - if (newLeftRepetitions < 0) { - logger.warn("Received UpdateChallenge request with negative leftRepetition value of: " - + newLeftRepetitions); - newLeftRepetitions = 0; - } - subChallenge.setRepetitions(newLeftRepetitions); - return Optional.empty(); - } - /* Can be used for all types of challenges. */ @@ -1021,7 +1019,9 @@ public ResponseEntity deleteChallenge( challengeId)) .findFirst(); if (!azkarChallenge.isPresent() && !meaningChallenge.isPresent() && !readingQuranChallenge - .isPresent() && !memorizationChallenge.isPresent() && !customSimpleChallenge.isPresent()) { + .isPresent() + && !memorizationChallenge.isPresent() + && !customSimpleChallenge.isPresent()) { response.setStatus(new Status(Status.CHALLENGE_NOT_FOUND_ERROR)); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response); } @@ -1121,9 +1121,10 @@ public ResponseEntity finishCustomSimpleCha User currentUser = getCurrentUser(userRepo); Optional currentUserChallenge = currentUser.getCustomSimpleChallenges() .stream() - .filter(challenge -> challenge.getId() - .equals( - challengeId)) + .filter( + challenge -> challenge.getId() + .equals( + challengeId)) .findFirst(); if (!currentUserChallenge.isPresent()) { FinishCustomSimpleChallengeResponse response = new FinishCustomSimpleChallengeResponse();