Skip to content
Snippets Groups Projects
Commit 7adbc5c8 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch 'feature/uniqueLinkCode' into 'main'

Feature/unique link code

See merge request idatt2106-v23-03/backend!106
parents f9273909 3911b5a0
No related branches found
No related tags found
No related merge requests found
......@@ -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());
}
}
......@@ -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")
......
......@@ -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();
}
......@@ -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();
}
}
package ntnu.idatt2016.v233.SmartMat.util;
// Formula: 10/(e^(-x/3))
public class LevelUtil {
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));
......@@ -15,7 +36,16 @@ public class LevelUtil {
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);
}
}
......@@ -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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment