diff --git a/build.gradle b/build.gradle index d3c5650..c401fe5 100644 --- a/build.gradle +++ b/build.gradle @@ -86,8 +86,6 @@ dependencies { } testImplementation 'org.springframework.security:spring-security-test' testImplementation 'com.tngtech.archunit:archunit-junit5:0.13.1' - implementation 'junit:junit:4.12' - implementation 'junit:junit:4.12' } test { diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 298c19d..57ef550 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -1,38 +1,20 @@ package mops.gruppen2.controller; -import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Role; import mops.gruppen2.domain.User; -import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.exception.EventException; -import mops.gruppen2.domain.exception.GroupFullException; -import mops.gruppen2.domain.exception.GroupNotFoundException; -import mops.gruppen2.domain.exception.NoAccessException; -import mops.gruppen2.domain.exception.NoAdminAfterActionException; import mops.gruppen2.domain.exception.PageNotFoundException; -import mops.gruppen2.domain.exception.UserAlreadyExistsException; -import mops.gruppen2.domain.exception.WrongFileException; import mops.gruppen2.security.Account; -import mops.gruppen2.service.ControllerService; -import mops.gruppen2.service.CsvService; -import mops.gruppen2.service.GroupService; -import mops.gruppen2.service.KeyCloakService; -import mops.gruppen2.service.UserService; +import mops.gruppen2.service.*; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; import org.springframework.web.context.annotation.SessionScope; import org.springframework.web.multipart.MultipartFile; - import javax.annotation.security.RolesAllowed; import javax.servlet.http.HttpServletRequest; -import java.io.CharConversionException; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -47,12 +29,14 @@ public class WebController { private final GroupService groupService; private final UserService userService; private final ControllerService controllerService; + private final ValidationService validationService; - public WebController(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService) { + public WebController(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService, ValidationService validationService) { this.keyCloakService = keyCloakService; this.groupService = groupService; this.userService = userService; this.controllerService = controllerService; + this.validationService = validationService; } /** @@ -96,7 +80,11 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); UUID parentUUID = controllerService.getUUID(parent); - controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file); + List userList = new ArrayList<>(); + validationService.checkFields(description, title, userMaximum, maxInfiniteUsers); + Group group = userService.getGroupById(controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID)); + userList = validationService.checkFile(file, userList, group.getId().toString(), group, account); + controllerService.addUserList(userList, group.getId()); return "redirect:/gruppen2/"; } @@ -121,6 +109,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); UUID parentUUID = controllerService.getUUID(parent); + validationService.checkFields(description, title, userMaximum, maxInfiniteUsers); controllerService.createGroup(account, title, description, visibility, maxInfiniteUsers, userMaximum, parentUUID); return "redirect:/gruppen2/"; } @@ -133,17 +122,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); List userList = new ArrayList<>(); Group group = userService.getGroupById(UUID.fromString(groupId)); - if (!file.isEmpty()) { - try { - userList = CsvService.read(file.getInputStream()); - if (userList.size() + group.getMembers().size() > group.getUserMaximum()) { - controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size()); - } - } catch (UnrecognizedPropertyException | CharConversionException ex) { - throw new WrongFileException(file.getOriginalFilename()); - } - } - + userList = validationService.checkFile(file, userList, groupId, group, account); UUID groupUUID = controllerService.getUUID(groupId); controllerService.addUserList(userList, groupUUID); return "redirect:/gruppen2/details/members/" + groupId; @@ -155,13 +134,11 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); + validationService.checkIfAdmin(group, user); model.addAttribute("account", account); UUID parentId = group.getParent(); Group parent = new Group(); - if (!group.getMembers().contains(user)) { - if (group.getVisibility() == Visibility.PRIVATE) { - throw new NoAccessException("Die Gruppe ist privat"); - } + if (!validationService.checkIfUserInGroup(group, user)) { model.addAttribute("group", group); model.addAttribute("parentId", parentId); model.addAttribute("parent", parent); @@ -184,9 +161,10 @@ public class WebController { @RequestParam("groupId") String groupId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); - controllerService.updateTitle(account, UUID.fromString(groupId), title); - controllerService.updateDescription(account, UUID.fromString(groupId), description); - + User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); + Group group = userService.getGroupById(UUID.fromString(groupId)); + validationService.checkIfAdmin(group, user); + validationService.checkTitleAndDescription(title, description, account, groupId); return "redirect:/gruppen2/details/" + groupId; } @@ -196,12 +174,8 @@ public class WebController { Model model, @RequestParam(value = "suchbegriff", required = false) String search) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); - List groups = new ArrayList<>(); - if (search != null) { - groups = groupService.findGroupWith(search, account); - } - + groups = validationService.checkSearch(search, groups, account); model.addAttribute("account", account); model.addAttribute("gruppen", groups); return "search"; @@ -215,28 +189,18 @@ public class WebController { Group group = userService.getGroupById(UUID.fromString(groupId)); Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); - UUID parentId = group.getParent(); - Group parent = new Group(); - if (group.getTitle() == null) { - throw new GroupNotFoundException("@details"); - } + validationService.checkGroup(group.getTitle()); + Group parent = validationService.checkParent(parentId); - if (!group.getMembers().contains(user)) { - if (group.getVisibility() == Visibility.PRIVATE) { - throw new NoAccessException("Die Gruppe ist privat"); - } + if (!validationService.checkIfUserInGroup(group, user)) { model.addAttribute("group", group); model.addAttribute("parentId", parentId); model.addAttribute("parent", parent); return "detailsNoMember"; } - if (!controllerService.idIsEmpty(parentId)) { - parent = userService.getGroupById(parentId); - } - model.addAttribute("parentId", parentId); model.addAttribute("parent", parent); model.addAttribute("group", group); @@ -246,7 +210,6 @@ public class WebController { String URL = request.getRequestURL().toString(); String serverURL = URL.substring(0, URL.indexOf("gruppen2/")); - model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + groupId); return "detailsMember"; @@ -260,14 +223,9 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); - if (group.getMembers().contains(user)) { - throw new UserAlreadyExistsException("Du bist bereits in dieser Gruppe."); - } + validationService.checkIfUserInGroupJoin(group, user); + validationService.checkIfGroupFull(group); controllerService.addUser(account, UUID.fromString(groupId)); - if (group.getUserMaximum() < group.getMembers().size()) { - throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten."); - } - //controllerService.addUser(account, groupId); return "redirect:/gruppen2/"; } @@ -279,20 +237,14 @@ public class WebController { model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); Group group = userService.getGroupById(UUID.fromString(groupId)); UUID parentId = group.getParent(); - Group parent = new Group(); + Group parent = validationService.checkParent(parentId); - if (!controllerService.idIsEmpty(parentId)) { - parent = userService.getGroupById(parentId); - } + validationService.checkIfGroupFull(group); + model.addAttribute("group", group); + model.addAttribute("parentId", parentId); + model.addAttribute("parent", parent); - if (group.getUserMaximum() > group.getMembers().size()) { - model.addAttribute("group", group); - model.addAttribute("parentId", parentId); - model.addAttribute("parent", parent); - - return "detailsNoMember"; - } - throw new GroupNotFoundException("@search"); + return "detailsNoMember"; } @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @@ -301,11 +253,9 @@ public class WebController { Model model, @PathVariable String groupId) throws EventException { model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); Group group = userService.getGroupById(UUID.fromString(groupId)); - if (group != null) { - model.addAttribute("group", group); - return "redirect:/gruppen2/detailsSearch?id=" + group.getId(); - } - throw new GroupNotFoundException("@accept"); + validationService.checkGroup(group.getTitle()); + model.addAttribute("group", group); + return "redirect:/gruppen2/detailsSearch?id=" + group.getId(); } @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @@ -316,11 +266,7 @@ public class WebController { User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); controllerService.passIfLastAdmin(account, UUID.fromString(groupId)); controllerService.deleteUser(user.getId(), UUID.fromString(groupId)); - - if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) { - controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); - } - + validationService.checkIfGroupEmpty(groupId, user); return "redirect:/gruppen2/"; } @@ -331,9 +277,7 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(UUID.fromString(groupId)); - if (group.getRoles().get(user.getId()) != Role.ADMIN) { - throw new NoAccessException(""); - } + validationService.checkIfAdmin(group, user); controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); return "redirect:/gruppen2/"; } @@ -346,19 +290,12 @@ public class WebController { Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); User user = new User(account.getName(), "", "", ""); - if (group.getMembers().contains(user)) { - if (group.getRoles().get(account.getName()) == Role.ADMIN) { - model.addAttribute("account", account); - model.addAttribute("members", group.getMembers()); - model.addAttribute("group", group); - model.addAttribute("admin", Role.ADMIN); - return "editMembers"; - } else { - return "redirect:/details/"; - } - } else { - throw new NoAccessException("Die Gruppe ist privat"); - } + validationService.checkIfAdmin(group, user); + model.addAttribute("account", account); + model.addAttribute("members", group.getMembers()); + model.addAttribute("group", group); + model.addAttribute("admin", Role.ADMIN); + return "editMembers"; } @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"}) @@ -367,14 +304,9 @@ public class WebController { @RequestParam("group_id") String groupId, @RequestParam("user_id") String userId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); - if (userId.equals(account.getName())) { - if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) { - throw new NoAdminAfterActionException("Du otto bist letzter Admin"); - } - controllerService.updateRole(userId, UUID.fromString(groupId)); + if (validationService.checkIfDemotingSelf(userId, groupId, account)) { return "redirect:/gruppen2/details/" + groupId; } - controllerService.updateRole(userId, UUID.fromString(groupId)); return "redirect:/gruppen2/details/members/" + groupId; } @@ -392,10 +324,9 @@ public class WebController { @PostMapping("/details/members/deleteUser") public String deleteUser(@RequestParam("group_id") String groupId, @RequestParam("user_id") String userId) throws EventException { + User user = new User(userId, "", "", ""); controllerService.deleteUser(userId, UUID.fromString(groupId)); - if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) { - controllerService.deleteGroupEvent(userId, UUID.fromString(groupId)); - } + validationService.checkIfGroupEmpty(groupId, user); return "redirect:/gruppen2/details/members/" + groupId; } diff --git a/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java b/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java index 0e297fa..9020b0c 100644 --- a/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java +++ b/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java @@ -39,7 +39,7 @@ public class AddUserEvent extends Event { throw new UserAlreadyExistsException(this.getClass().toString()); } - if (group.getMembers().size() == group.getUserMaximum()) { + if (group.getMembers().size() >= group.getUserMaximum()) { throw new GroupFullException(this.getClass().toString()); } diff --git a/src/main/java/mops/gruppen2/repository/EventRepository.java b/src/main/java/mops/gruppen2/repository/EventRepository.java index b306452..f2199e8 100644 --- a/src/main/java/mops/gruppen2/repository/EventRepository.java +++ b/src/main/java/mops/gruppen2/repository/EventRepository.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Repository; import java.util.List; @Repository +//TODO Rename Queries + Formatting public interface EventRepository extends CrudRepository { @Query("select distinct group_id from event where user_id =:id") diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 777ccc6..649b331 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -46,26 +46,6 @@ public class ControllerService { this.logger = Logger.getLogger("controllerServiceLogger"); } - /** - * Überprüft ob alle Felder richtig gesetzt sind. - * @param description - * @param title - * @param userMaximum - */ - private void checkFields(String description, String title, Long userMaximum ) { - if(description == null) { - throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben"); - } - - if(title == null) { - throw new BadParameterException("Der Titel wurde nicht korrekt angegeben"); - } - - if (userMaximum == null) { - throw new BadParameterException("Teilnehmeranzahl wurde nicht korrekt angegeben"); - } - } - /** * Erzeugt eine neue Gruppe, fügt den User, der die Gruppe erstellt hat, hinzu und setzt seine Rolle als Admin fest. * Zudem wird der Gruppentitel und die Gruppenbeschreibung erzeugt, welche vorher der Methode übergeben wurden. @@ -77,12 +57,10 @@ public class ControllerService { */ public void createGroup(Account account, String title, String description, Boolean isVisibilityPrivate, Boolean isMaximumInfinite, Long userMaximum, UUID parent) throws EventException { Visibility groupVisibility = setGroupVisibility(isVisibilityPrivate); - UUID groupId = eventService.checkGroup(); + UUID groupId = UUID.randomUUID(); userMaximum = checkInfiniteUsers(isMaximumInfinite, userMaximum); - checkFields(description, title, userMaximum); - CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, GroupType.SIMPLE, groupVisibility, userMaximum); eventService.saveEvent(createGroupEvent); @@ -92,18 +70,16 @@ public class ControllerService { updateRole(account.getName(), groupId); } - public void createOrga(Account account, String title, String description, Boolean isVisibilityPrivate, Boolean isLecture, Boolean isMaximumInfinite, Long userMaximum, UUID parent, MultipartFile file) throws EventException, IOException { + public UUID createOrga(Account account, String title, String description, Boolean isVisibilityPrivate, Boolean isLecture, Boolean isMaximumInfinite, Long userMaximum, UUID parent, MultipartFile file) throws EventException, IOException { userMaximum = checkInfiniteUsers(isMaximumInfinite, userMaximum); - checkFields(description, title, userMaximum); - List userList = readCsvFile(file); if (userList.size() > userMaximum) { userMaximum = (long) userList.size() + 1; } - UUID groupId = eventService.checkGroup(); + UUID groupId = UUID.randomUUID(); Visibility groupVisibility = setGroupVisibility(isVisibilityPrivate); GroupType groupType = setGroupType(isLecture); @@ -114,7 +90,8 @@ public class ControllerService { updateTitle(account, groupId, title); updateDescription(account, groupId, description); updateRole(account.getName(), groupId); - addUserList(userList, groupId); + + return groupId; } private Long checkInfiniteUsers(Boolean isMaximumInfinite, Long userMaximum) { diff --git a/src/main/java/mops/gruppen2/service/EventService.java b/src/main/java/mops/gruppen2/service/EventService.java index 4d52888..92a671f 100644 --- a/src/main/java/mops/gruppen2/service/EventService.java +++ b/src/main/java/mops/gruppen2/service/EventService.java @@ -12,6 +12,7 @@ import java.util.UUID; import java.util.stream.Collectors; @Service +//TODO: Evtl aufsplitten in EventRepoService und EventService? public class EventService { private final JsonService jsonService; @@ -28,8 +29,22 @@ public class EventService { * @param event Event, welches gespeichert wird */ public void saveEvent(Event event) { - EventDTO eventDTO = getDTO(event); - eventStore.save(eventDTO); + eventStore.save(getDTO(event)); + } + + public void saveAll(Event... events) { + for (Event event : events) { + eventStore.save(getDTO(event)); + } + } + + @SafeVarargs + public final void saveAll(List... events) { + for (List eventlist : events) { + for (Event event : eventlist) { + eventStore.save(getDTO(event)); + } + } } /** @@ -39,6 +54,7 @@ public class EventService { * @param event Event, welches in DTO übersetzt wird * @return EventDTO Neues DTO */ + //TODO Rename: getDTOFromEvent? public EventDTO getDTO(Event event) { String payload = ""; try { @@ -56,26 +72,9 @@ public class EventService { return event.getClass().getName().substring(lastDot + 1); } - /** - * Gibt die nächst höhere groupID zurück die belegt werden kann. - * Gibt 1 zurück, falls keine Gruppe vorhanden ist. - * - * @return Long GruppenId - */ - public UUID checkGroup() { - return UUID.randomUUID(); - - /*Long maxGroupID = eventStore.getMaxGroupID(); - - if (maxGroupID == null) { - return 1L; - } - - return maxGroupID + 1;*/ - } - /** * Findet alle Events welche ab dem neuen Status hinzugekommen sind. + * Sucht alle Events mit event_id > status * * @param status Die Id des zuletzt gespeicherten Events * @return Liste von neueren Events @@ -93,6 +92,7 @@ public class EventService { * @param eventDTOS Liste von DTOs * @return Liste von Events */ + //TODO Rename: getEventsFromDTO? public List translateEventDTOs(Iterable eventDTOS) { List events = new ArrayList<>(); @@ -106,17 +106,6 @@ public class EventService { return events; } - /** - * Sichert eine Liste von Event Objekten mithilfe der Methode saveEvent(Event event). - * - * @param eventList Liste von Event Objekten - */ - public void saveEventList(List eventList) { - for (Event event : eventList) { - saveEvent(event); - } - } - public Long getMaxEvent_id() { return eventStore.getHighesEvent_ID(); } @@ -126,6 +115,7 @@ public class EventService { return translateEventDTOs(eventDTOList); } + //TODO: Nur AddUserEvents betrachten public List findGroupIdsByUser(String userId) { return eventStore.findGroup_idsWhereUser_id(userId).stream() .map(UUID::fromString) diff --git a/src/main/java/mops/gruppen2/service/GroupService.java b/src/main/java/mops/gruppen2/service/GroupService.java index aa22132..28ceb33 100644 --- a/src/main/java/mops/gruppen2/service/GroupService.java +++ b/src/main/java/mops/gruppen2/service/GroupService.java @@ -35,6 +35,7 @@ public class GroupService { * @param groupIds Liste an IDs * @return Liste an Events */ + //TODO Das vielleicht in den EventRepoService? public List getGroupEvents(List groupIds) { List eventDTOS = new ArrayList<>(); for (UUID groupId : groupIds) { @@ -75,6 +76,7 @@ public class GroupService { * @return Liste von projizierten Gruppen * @throws EventException Projektionsfehler */ + //TODO Rename public List getAllGroupWithVisibilityPublic(String userId) throws EventException { List groupEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); groupEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent"))); @@ -84,7 +86,9 @@ public class GroupService { List visibleGroups = projectEventList(groupEvents); - return visibleGroups.parallelStream() + sortByGroupType(visibleGroups); + + return visibleGroups.stream() .filter(group -> group.getType() != null) .filter(group -> !eventService.userInGroup(group.getId(), userId)) .filter(group -> group.getVisibility() == Visibility.PUBLIC) @@ -101,11 +105,11 @@ public class GroupService { List createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent")); createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent"))); createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent"))); + createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent"))); List visibleGroups = projectEventList(createEvents); - return visibleGroups.parallelStream() - .filter(group -> group.getType() != null) + return visibleGroups.stream() .filter(group -> group.getType() == GroupType.LECTURE) .filter(group -> group.getVisibility() == Visibility.PUBLIC) .collect(Collectors.toList()); @@ -120,6 +124,7 @@ public class GroupService { * @return Liste von projizierten Gruppen * @throws EventException Projektionsfehler */ + //Todo Rename public List findGroupWith(String search, Account account) throws EventException { if (search.isEmpty()) { return getAllGroupWithVisibilityPublic(account.getName()); @@ -127,9 +132,21 @@ public class GroupService { return getAllGroupWithVisibilityPublic(account.getName()) .parallelStream() - .filter(group -> - group.getTitle().toLowerCase().contains(search.toLowerCase()) || - group.getDescription().toLowerCase().contains(search.toLowerCase())) + .filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase()) + || group.getDescription().toLowerCase().contains(search.toLowerCase())) .collect(Collectors.toList()); } + + public void sortByGroupType(List groups) { + groups.sort((g1, g2) -> { + if (g1.getType() == GroupType.LECTURE) { + return -1; + } + if (g2.getType() == GroupType.LECTURE) { + return 0; + } + + return 1; + }); + } } diff --git a/src/main/java/mops/gruppen2/service/UserService.java b/src/main/java/mops/gruppen2/service/UserService.java index d4db839..39fecff 100644 --- a/src/main/java/mops/gruppen2/service/UserService.java +++ b/src/main/java/mops/gruppen2/service/UserService.java @@ -35,6 +35,9 @@ public class UserService { newGroups.add(group); } } + + groupService.sortByGroupType(newGroups); + return newGroups; } diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java new file mode 100644 index 0000000..9b830d5 --- /dev/null +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -0,0 +1,138 @@ +package mops.gruppen2.service; + +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; +import mops.gruppen2.domain.Group; +import mops.gruppen2.domain.Role; +import mops.gruppen2.domain.User; +import mops.gruppen2.domain.Visibility; +import mops.gruppen2.domain.exception.*; +import mops.gruppen2.security.Account; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.swing.text.StyledEditorKit; +import javax.validation.ValidationException; +import java.io.CharConversionException; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.UUID; + +@Service +public class ValidationService { + + private final ControllerService controllerService; + private final UserService userService; + private final GroupService groupService; + + public ValidationService(ControllerService controllerService, UserService userService, GroupService groupService) { + this.controllerService = controllerService; + this.userService = userService; + this.groupService = groupService; + } + + public void checkTitleAndDescription(String title, String description, Account account, String groupId) { + if (title == null || description == null) { + throw new NoValueException("Titel und Beschreibung müssen ausgefüllt werden"); + } + controllerService.updateTitle(account, UUID.fromString(groupId), title); + controllerService.updateDescription(account, UUID.fromString(groupId), description); + } + + public List checkSearch(String search, List groups, Account account) { + if (search != null) { + groups = groupService.findGroupWith(search, account); + } + return groups; + } + + public void checkGroup(String title) { + if (title == null) throw new GroupNotFoundException("@details"); + } + + public boolean checkIfUserInGroup(Group group, User user) { + if (!group.getMembers().contains(user) && group.getVisibility() == Visibility.PRIVATE) { + throw new NoAccessException(""); + } else return group.getMembers().contains(user); + } + + public Group checkParent(UUID parentId) { + Group parent = new Group(); + if (!controllerService.idIsEmpty(parentId)) { + parent = userService.getGroupById(parentId); + } + return parent; + } + + public void checkIfUserInGroupJoin(Group group, User user) { + if (group.getMembers().contains(user)) { + throw new UserAlreadyExistsException("@details"); + } + } + + public void checkIfGroupFull(Group group) { + if (group.getUserMaximum() < group.getMembers().size() + 1) { + throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten."); + } + } + + + public void checkIfGroupEmpty(String groupId, User user) { + if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) { + controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId)); + } + } + + public void checkIfAdmin(Group group, User user) { + checkIfUserInGroup(group, user); + if (group.getRoles().get(user.getId()) != Role.ADMIN) { + throw new NoAccessException(""); + } + } + + public boolean checkIfDemotingSelf(String userId, String groupId, Account account) { + if (userId.equals(account.getName())) { + if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) { + throw new NoAdminAfterActionException("Du Otto bist letzter Admin"); + } + controllerService.updateRole(userId, UUID.fromString(groupId)); + return true; + } + controllerService.updateRole(userId, UUID.fromString(groupId)); + return false; + } + + public List checkFile(MultipartFile file, List userList, String groupId, Group group, Account account) { + if (!file.isEmpty()) { + try { + userList = CsvService.read(file.getInputStream()); + if (userList.size() + group.getMembers().size() > group.getUserMaximum()) { + controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size()); + } + } catch (IOException ex) { + throw new WrongFileException(file.getOriginalFilename()); + } + } + return userList; + } + + /** + * Überprüft ob alle Felder richtig gesetzt sind. + * @param description + * @param title + * @param userMaximum + */ + public void checkFields(String description, String title, Long userMaximum, Boolean maxInfiniteUsers) { + if (description == null) { + throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben"); + } + + if (title == null) { + throw new BadParameterException("Der Titel wurde nicht korrekt angegeben"); + } + + if (userMaximum == null && maxInfiniteUsers == null) { + throw new BadParameterException("Teilnehmeranzahl wurde nicht korrekt angegeben"); + } + } +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index a464f26..06bd43b 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -10,7 +10,8 @@
-