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 3fc3fa678964fb62a2d02b65eea46d0aa741fe1a..ecf2f718929d73bea5f0b1fa3203654fbfe9ec96 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 faeb35a1f9ed7a14e57e843e2df6b0bbf2d5f758..9fe7deb6430eadd74165a78c2ef51aed64a95311 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 b4e9042f28f6d3b0449236fe64a9838603a3e015..ff2dd81dd1e764ee594f1a0541ef5946aca80afe 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())); }