From fcd6e19cbb628922cc8319336e794fdc44794be1 Mon Sep 17 00:00:00 2001 From: Pedro Cardona <pedropca@stud.ntnu.no> Date: Tue, 25 Apr 2023 11:30:59 +0200 Subject: [PATCH] Comments for the methods were added --- .../controller/group/GroupController.java | 26 ++++++++++++++----- .../repository/group/GroupRepository.java | 5 ++++ .../SmartMat/service/group/GroupService.java | 21 ++++++++++++--- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java index 3fc3fa67..ecf2f718 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java @@ -72,18 +72,32 @@ public class GroupController { .map(group -> ResponseEntity.ok(group.getLevel())) .orElseGet(() -> ResponseEntity.notFound().build()); } - + /** + * Sets the level of the group identified by the given ID to the level corresponding to the given experience points. + * Returns a ResponseEntity containing the new level of the group, or a 404 Not Found response if no Group with the given ID was found. + * + * @param groupId the ID of the group to update + * @param exp the new experience points of the group + * @return a ResponseEntity containing the new level of the group, or a 404 Not Found response if no Group with the given ID was found + */ @PutMapping("/group/{groupId}/newLevel/{exp}") - public ResponseEntity<Long> setNewLevel(@PathVariable("groupId") long groupId, @PathVariable("exp") long exp){ - return groupService.setLevelByGroupId(groupId,exp) + public ResponseEntity<Long> setNewLevel(@PathVariable("groupId") long groupId, @PathVariable("exp") long exp) { + return groupService.setLevelByGroupId(groupId, exp) .map(group -> ResponseEntity.ok(group.getLevel())) - .orElseGet(()->ResponseEntity.notFound().build()); + .orElseGet(() -> ResponseEntity.notFound().build()); } + /** + * Returns the progress of the level for the group identified by the given ID. + * Returns a ResponseEntity containing the progress of the current level as a percentage, or a 404 Not Found response if no Group with the given ID was found. + * + * @param groupId the ID of the group to query + * @return a ResponseEntity containing the progress of the current level as a percentage, or a 404 Not Found response if no Group with the given ID was found + */ @GetMapping("/group/{groupId}/progress") - public ResponseEntity<Integer> getProgressOfLevel(@PathVariable("groupId") long groupId){ + public ResponseEntity<Integer> getProgressOfLevel(@PathVariable("groupId") long groupId) { return groupService.getProgressOfLevel(groupId) .map(ResponseEntity::ok) - .orElseGet(()->ResponseEntity.notFound().build()); + .orElseGet(() -> ResponseEntity.notFound().build()); } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java index faeb35a1..9fe7deb6 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java @@ -41,5 +41,10 @@ public interface GroupRepository extends JpaRepository<Group, Long> { */ Optional<Long> getLevelByGroupId(long id); + /** + * Finds a group by group id + * @param id the id of the group + * @return the group with the given id if it exists + */ Optional<Group> findByGroupId(long id); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java index b4e9042f..ff2dd81d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java @@ -65,9 +65,16 @@ public class GroupService { return groupRepository.getLevelByGroupId(id); } - public Optional<Group> setLevelByGroupId(long id, long exp){ + /** + * Sets the level of the group identified by the given ID to the level corresponding to the given experience points. + * + * @param id the ID of the group to update + * @param exp the new experience points of the group + * @return an Optional containing the updated Group, or an empty Optional if no Group with the given ID was found + */ + public Optional<Group> setLevelByGroupId(long id, long exp) { Optional<Group> answer = groupRepository.findByGroupId(id); - if(answer.isPresent()){ + if (answer.isPresent()) { Group realGroup = answer.get(); realGroup.setPoints(exp); realGroup.setLevel(LevelUtil.getLevel(exp)); @@ -76,9 +83,15 @@ public class GroupService { return Optional.empty(); } - public Optional<Integer> getProgressOfLevel(long id){ + /** + * Returns the progress of the level for the group identified by the given ID. + * + * @param id the ID of the group to query + * @return an Optional containing the progress of the current level as a percentage, or an empty Optional if no Group with the given ID was found + */ + public Optional<Integer> getProgressOfLevel(long id) { Optional<Group> answer = groupRepository.findByGroupId(id); - if(answer.isPresent()){ + if (answer.isPresent()) { Group realGroup = answer.get(); return Optional.of(LevelUtil.getProgressOfLevel(realGroup.getPoints())); } -- GitLab