diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index f83739fbdd27e45946e6ee1b7d3b43c7d8fd7a4d..b8495ea327eb0c5257e31d21d2dd0cc0e70e9bbc 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="SqlDialectMappings"> + <file url="file://$PROJECT_DIR$/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java" dialect="GenericSQL" /> <file url="file://$PROJECT_DIR$/src/main/resources/db/sql/dbschema.sql" dialect="H2" /> </component> </project> \ No newline at end of file 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 e9840c072f912159ea2caa4255de5600a6043059..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 @@ -67,9 +67,37 @@ public class GroupController { * @return a ResponseEntity containing the level of the group if it exists, or a 404 if it doesn't */ @GetMapping("/group/{groupId}/level") - public ResponseEntity<Integer> getGroupLevel(@PathVariable("groupId") long groupId) { + public ResponseEntity<Long> getGroupLevel(@PathVariable("groupId") long groupId) { return groupService.getGroupById(groupId) .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) + .map(group -> ResponseEntity.ok(group.getLevel())) + .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) { + return groupService.getProgressOfLevel(groupId) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); + } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java index 83fb34e59f98934bde8df90d82bcd2f9699152d9..2580c658c5c8f2dd9739982d6a66c8cf051d398d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java @@ -33,10 +33,10 @@ public class Group { long groupId; @Column(name = "level") - int level; + long level; @Column(name = "points") - int points; + long points; @Column(name = "group_name") String groupName; 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 5d5ff0ad46d65e92bfc4980329639ec1e62ae217..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 @@ -1,8 +1,12 @@ package ntnu.idatt2016.v233.SmartMat.repository.group; +import jakarta.transaction.Transactional; import org.springframework.data.jpa.repository.JpaRepository; import ntnu.idatt2016.v233.SmartMat.entity.group.Group; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; import java.util.Optional; @@ -35,5 +39,12 @@ public interface GroupRepository extends JpaRepository<Group, Long> { * @param id the id of the group * @return the level of the group */ - int getLevelByGroupId(long id); + 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 0c7392532d8e47a54c89238fc5321128957a6b9a..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 @@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.service.group; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; +import ntnu.idatt2016.v233.SmartMat.util.LevelUtil; import org.springframework.stereotype.Service; import java.util.Optional; @@ -60,7 +61,40 @@ public class GroupService { * @param id the id of the group * @return the level of the group */ - public int getLevelByGroupId(long id) { + public Optional<Long> getLevelByGroupId(long id) { return groupRepository.getLevelByGroupId(id); } + + /** + * 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()) { + Group realGroup = answer.get(); + realGroup.setPoints(exp); + realGroup.setLevel(LevelUtil.getLevel(exp)); + return Optional.of(groupRepository.save(realGroup)); + } + return Optional.empty(); + } + + /** + * 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()) { + Group realGroup = answer.get(); + return Optional.of(LevelUtil.getProgressOfLevel(realGroup.getPoints())); + } + return Optional.empty(); + } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java index 7419d6514f9b933b892edc96fc827137f4dae6e8..9242ed52488ab0e2538390b4752cb924dc0b8190 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java @@ -4,11 +4,11 @@ package ntnu.idatt2016.v233.SmartMat.util; // Formula: 10/(e^(-x/3)) public class LevelUtil { - public static int getLevel(int exp){ + public static int getLevel(long exp){ return (int) (-3.0 * Math.log(10.0/ ((double)exp))); } - public static int getProgressOfLevel(int exp) { + public static int getProgressOfLevel(long exp) { int currentLevel = getLevel(exp); double expNextLevel = (10.0 / Math.pow(Math.E, (currentLevel+1) / -3.0)); double expCurrentLevel = (10.0 / Math.pow(Math.E, (currentLevel) / -3.0));