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 ecf2f718929d73bea5f0b1fa3203654fbfe9ec96..88a6a5741e7b62993215f0792c2baf7f37ef96fc 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 @@ -100,4 +100,17 @@ public class GroupController { .map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); } + + /** + * Updates the open/closed status of the group with the specified ID. + * + * @param groupId the ID of the group to update + * @return a ResponseEntity with a Boolean value indicating whether the operation was successful + */ + @PutMapping("/group/{groupId}/changeOpen") + public ResponseEntity<Boolean> changeOpenValue(@PathVariable("groupId") long groupId) { + return groupService.OpenOrCloseGroup(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 2580c658c5c8f2dd9739982d6a66c8cf051d398d..567da5ed60d03c3c499178a34ce671c9e50b6ed5 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 @@ -41,6 +41,12 @@ public class Group { @Column(name = "group_name") String groupName; + @Column(name = "link_code") + String linkCode; + + @Column(name = "is_open") + boolean open; + @OneToMany @JoinColumn(name = "group_id") @JsonIgnoreProperties("group") 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 9fe7deb6430eadd74165a78c2ef51aed64a95311..135482e8af9c0e1c8b35cf150ba88fdd3558e17c 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 @@ -47,4 +47,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> { * @return the group with the given id if it exists */ Optional<Group> findByGroupId(long id); + + @Query(value = "SELECT link_code FROM groups", nativeQuery = true) + List<String> findAllLinkCode(); } 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 ba29faf21cce6d02752edd7c9057b6c466585e5c..32e2b3684d8d0f348cd76f7de244b1b27c6661c4 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,9 +3,10 @@ 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 ntnu.idatt2016.v233.SmartMat.util.GroupUtil; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Optional; /** @@ -52,6 +53,12 @@ public class GroupService { throw new IllegalArgumentException("Group must have a name"); if(groupRepository.findByGroupName(group.getGroupName()).isPresent()) throw new IllegalArgumentException("Group already exists"); + String code = GroupUtil.generateUniqueCode(); + List<String> codes = groupRepository.findAllLinkCode(); + while (codes.contains(code)){ + code = GroupUtil.generateUniqueCode(); + } + group.setLinkCode(code); return groupRepository.save(group); } @@ -77,7 +84,7 @@ public class GroupService { if (answer.isPresent()) { Group realGroup = answer.get(); realGroup.setPoints(exp); - realGroup.setLevel(LevelUtil.getLevel(exp)); + realGroup.setLevel(GroupUtil.getLevel(exp)); return Optional.of(groupRepository.save(realGroup)); } return Optional.empty(); @@ -93,8 +100,26 @@ public class GroupService { Optional<Group> answer = groupRepository.findByGroupId(id); if (answer.isPresent()) { Group realGroup = answer.get(); - return Optional.of(LevelUtil.getProgressOfLevel(realGroup.getPoints())); + return Optional.of(GroupUtil.getProgressOfLevel(realGroup.getPoints())); + } + return Optional.empty(); + } + + /** + * Updates the open/closed status of the group with the specified ID. + * + * @param id the ID of the group to update + * @return an Optional with a Boolean value indicating whether the operation was successful + */ + public Optional<Boolean> OpenOrCloseGroup(long id){ + Optional<Group> answer = groupRepository.findByGroupId(id); + if (answer.isPresent()) { + Group realGroup = answer.get(); + realGroup.setOpen(!realGroup.isOpen()); + System.out.println(realGroup.isOpen()); + return Optional.of(groupRepository.save(realGroup).isOpen()); } return Optional.empty(); } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/GroupUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/GroupUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..7e586b6161198f4cb5b80587d372d37181f1c28e --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/GroupUtil.java @@ -0,0 +1,51 @@ +package ntnu.idatt2016.v233.SmartMat.util; + + +import java.util.UUID; + + +/** + * This class provide some useful methods used with the group entity. + * + * @author Pedro Cardona + * @version 1.0 + * @since 25.04.2023 + */ +public class GroupUtil { + + /** + * Calculates the level of a group based on its experience points. + * + * @param exp The experience points of the group. + * @return The level of the group. + */ + public static int getLevel(long exp){ + return (int) (-3.0 * Math.log(10.0/ ((double)exp))); + } + + /** + * Calculates the progress of the current level of a group based on its experience points. + * + * @param exp The experience points of the group. + * @return The progress of the current level of the group as a percentage. + */ + 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)); + double baseNextLevel = expNextLevel - expCurrentLevel; + double basedCurrentExp= (double) exp - expCurrentLevel; + return (int) ((basedCurrentExp/ baseNextLevel )* 100.0); + } + + /** + * Generates a unique six-digit code using UUID. + * + * @return A unique six-digit code. + */ + public static String generateUniqueCode(){ + String uuid = UUID.randomUUID().toString(); + return uuid.replaceAll("-", "").substring(0, 6); + } + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java deleted file mode 100644 index 9242ed52488ab0e2538390b4752cb924dc0b8190..0000000000000000000000000000000000000000 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtil.java +++ /dev/null @@ -1,21 +0,0 @@ -package ntnu.idatt2016.v233.SmartMat.util; - - -// Formula: 10/(e^(-x/3)) -public class LevelUtil { - - public static int getLevel(long exp){ - return (int) (-3.0 * Math.log(10.0/ ((double)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)); - double baseNextLevel = expNextLevel - expCurrentLevel; - double basedCurrentExp= (double) exp - expCurrentLevel; - return (int) ((basedCurrentExp/ baseNextLevel )* 100.0); - - } - -} diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtilsTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtilsTest.java index 43456015a1b18d2b5afff5cc6f45a94791d2b9fe..faeaf950aecf4c87820b2398d38060dc7f87ae9d 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtilsTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/util/LevelUtilsTest.java @@ -8,23 +8,23 @@ public class LevelUtilsTest { @Test public void testGetLevel() { - assertEquals(0, LevelUtil.getLevel(10)); - assertEquals(2, LevelUtil.getLevel(20)); - assertEquals(4, LevelUtil.getLevel(45)); - assertEquals(6, LevelUtil.getLevel(90)); - assertEquals(8, LevelUtil.getLevel(160)); + assertEquals(0, GroupUtil.getLevel(10)); + assertEquals(2, GroupUtil.getLevel(20)); + assertEquals(4, GroupUtil.getLevel(45)); + assertEquals(6, GroupUtil.getLevel(90)); + assertEquals(8, GroupUtil.getLevel(160)); } @Test public void testGetPercentOfLevel() { - assertEquals(0, LevelUtil.getProgressOfLevel(10)); - assertEquals(25, LevelUtil.getProgressOfLevel(11)); - assertEquals(20, LevelUtil.getProgressOfLevel(80)); - assertEquals(10, LevelUtil.getProgressOfLevel(150)); - assertEquals(30, LevelUtil.getProgressOfLevel(225)); - assertEquals(17, LevelUtil.getProgressOfLevel(300)); - assertEquals(62, LevelUtil.getProgressOfLevel(350)); - assertEquals(85, LevelUtil.getProgressOfLevel(375)); + assertEquals(0, GroupUtil.getProgressOfLevel(10)); + assertEquals(25, GroupUtil.getProgressOfLevel(11)); + assertEquals(20, GroupUtil.getProgressOfLevel(80)); + assertEquals(10, GroupUtil.getProgressOfLevel(150)); + assertEquals(30, GroupUtil.getProgressOfLevel(225)); + assertEquals(17, GroupUtil.getProgressOfLevel(300)); + assertEquals(62, GroupUtil.getProgressOfLevel(350)); + assertEquals(85, GroupUtil.getProgressOfLevel(375)); } }