1

ControllerService + CreationController + GroupService improvements + disabled controllerservice tests, the need to be rewritten

This commit is contained in:
Christoph
2020-04-07 17:58:04 +02:00
parent e13af57ab6
commit 897722fbd7
12 changed files with 331 additions and 364 deletions

View File

@ -1,6 +1,7 @@
package mops.gruppen2.controller;
import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.IdService;
@ -20,6 +21,10 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.security.RolesAllowed;
import java.util.UUID;
import static mops.gruppen2.service.ControllerService.getGroupType;
import static mops.gruppen2.service.ControllerService.getUserMaximum;
import static mops.gruppen2.service.ControllerService.getVisibility;
@Controller
@SessionScope
@RequestMapping("/gruppen2")
@ -56,27 +61,27 @@ public class GroupCreationController {
public String postCrateGroupAsOrga(KeycloakAuthenticationToken token,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "lecture", required = false) Boolean lecture,
@RequestParam("userMaximum") Long userMaximum,
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam("visibility") boolean isPrivate,
@RequestParam("lecture") boolean isLecture,
@RequestParam("maxInfiniteUsers") boolean isInfinite,
@RequestParam("userMaximum") long maxUsers,
@RequestParam(value = "parent", required = false) String parent,
@RequestParam(value = "file", required = false) MultipartFile file) {
Account account = new Account(token);
UUID parentUUID = IdService.stringToUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
validationService.checkFields(description, title, maxUsers, isInfinite);
groupService.createGroupAsOrga(account,
title,
description,
getVisibility(isPrivate),
getGroupType(isLecture),
getUserMaximum(isInfinite, maxUsers),
parentUUID,
file);
controllerService.createGroupAsOrga(account,
title,
description,
visibility,
lecture,
maxInfiniteUsers,
userMaximum,
parentUUID,
file);
return "redirect:/gruppen2";
}
@ -99,24 +104,23 @@ public class GroupCreationController {
public String postCreateGroupAsStudent(KeycloakAuthenticationToken token,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("userMaximum") Long userMaximum,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam("visibility") boolean isPrivate,
@RequestParam("maxInfiniteUsers") boolean isInfinite,
@RequestParam("userMaximum") long maxUsers,
@RequestParam(value = "parent", required = false) String parent) {
Account account = new Account(token);
UUID parentUUID = IdService.stringToUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
validationService.checkFields(description, title, maxUsers, isInfinite);
controllerService.createGroup(account,
title,
description,
visibility,
null,
maxInfiniteUsers,
userMaximum,
parentUUID);
groupService.createGroup(account,
title,
description,
getVisibility(isPrivate),
GroupType.SIMPLE,
getUserMaximum(isInfinite, maxUsers),
parentUUID);
return "redirect:/gruppen2";
}

View File

@ -1,5 +1,6 @@
package mops.gruppen2.domain;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@ -14,22 +15,27 @@ import java.util.UUID;
*/
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Group {
//TODO: List to Hashmap
private final List<User> members;
private final Map<String, Role> roles;
@EqualsAndHashCode.Include
private UUID id;
private UUID parent;
private GroupType type;
private Visibility visibility;
private String title;
private String description;
private Long userMaximum;
private GroupType type;
private Visibility visibility;
private UUID parent;
public Group() {
members = new ArrayList<>();
roles = new HashMap<>();
//TODO: List to Hashmap
private final List<User> members = new ArrayList<>();
private final Map<String, Role> roles = new HashMap<>();
@Override
public String toString() {
return title + ": " + description;
}
}

View File

@ -7,11 +7,13 @@ import lombok.NoArgsConstructor;
@Getter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(exclude = {"givenname", "familyname", "email"})
@NoArgsConstructor // Für Jackson: CSV-Import
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class User {
@EqualsAndHashCode.Include
private String id;
private String givenname;
private String familyname;
private String email;

View File

@ -29,6 +29,13 @@ public class AddUserEvent extends Event {
this.email = email;
}
public AddUserEvent(UUID groupId, User user) {
super(groupId, user.getId());
givenname = user.getGivenname();
familyname = user.getFamilyname();
email = user.getEmail();
}
@Override
protected void applyEvent(Group group) throws EventException {
User user = new User(userId, givenname, familyname, email);

View File

@ -1,127 +1,36 @@
package mops.gruppen2.service;
import lombok.extern.log4j.Log4j2;
import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.CreateGroupEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
@Log4j2
public class ControllerService {
public final class ControllerService {
private final EventStoreService eventStoreService;
private final InviteService inviteService;
private final GroupService groupService;
private ControllerService() {}
public ControllerService(EventStoreService eventStoreService, InviteService inviteService, GroupService groupService) {
this.eventStoreService = eventStoreService;
this.inviteService = inviteService;
this.groupService = groupService;
public static Visibility getVisibility(boolean isPrivate) {
return isPrivate ? Visibility.PRIVATE : Visibility.PUBLIC;
}
public static GroupType getGroupType(boolean isLecture) {
return isLecture ? GroupType.LECTURE : GroupType.SIMPLE;
}
/**
* Wie createGroup, nur das hier die Gruppe auch als Veranstaltung gesetzt werden kann und CSV Dateien mit Nutzern
* eingelesen werden können.
* Wenn die maximale Useranzahl unendlich ist, wird das Maximum auf 100000 gesetzt.
* Praktisch gibt es also maximal 100000 Nutzer pro Gruppe.
*
* @param account Der Nutzer der die Gruppe erstellt
* @param title Parameter für die neue Gruppe
* @param description Parameter für die neue Gruppe
* @param isVisibilityPrivate Parameter für die neue Gruppe
* @param isLecture Parameter für die neue Gruppe
* @param isMaximumInfinite Parameter für die neue Gruppe
* @param userMaximum Parameter für die neue Gruppe
* @param parent Parameter für die neue Gruppe
* @param file Parameter für die neue Gruppe
*/
//TODO: remove booleans + add wrapper?
//TODO: auslagern teilweise -> EventBuilderService
public void createGroupAsOrga(Account account,
String title,
String description,
Boolean isVisibilityPrivate,
Boolean isLecture,
Boolean isMaximumInfinite,
Long userMaximum,
UUID parent,
MultipartFile file) {
userMaximum = GroupService.checkInfiniteUsers(isMaximumInfinite, userMaximum);
List<User> newUsers = CsvService.readCsvFile(file);
List<User> oldUsers = new ArrayList<>();
User user = new User(account);
oldUsers.add(user);
GroupService.removeOldUsersFromNewUsers(oldUsers, newUsers);
userMaximum = GroupService.adjustUserMaximum((long) newUsers.size(), 1L, userMaximum);
UUID groupId = createGroup(account,
title,
description,
isVisibilityPrivate,
isLecture,
isMaximumInfinite,
userMaximum, parent);
groupService.addUserList(newUsers, groupId);
}
/**
* 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.
* Aus diesen Event-Objekten wird eine Liste erzeugt, welche daraufhin mithilfe des EventServices gesichert wird.
* @param isInfinite Gibt an, ob es unendlich viele User geben soll
* @param userMaximum Das Maximum an Usern, falls es eins gibt
*
* @param account Keycloak-Account
* @param title Gruppentitel
* @param description Gruppenbeschreibung
* @return Maximum an Usern
*/
//TODO: remove booleans + add wrapper?
//TODO: auslagern teilweise -> EventBuilderService
public UUID createGroup(Account account,
String title,
String description,
Boolean isVisibilityPrivate,
Boolean isLecture,
Boolean isMaximumInfinite,
Long userMaximum,
UUID parent) {
userMaximum = GroupService.checkInfiniteUsers(isMaximumInfinite, userMaximum);
Visibility groupVisibility = GroupService.setGroupVisibility(isVisibilityPrivate);
UUID groupId = UUID.randomUUID();
GroupType groupType = GroupService.setGroupType(isLecture);
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId,
account.getName(),
parent,
groupType,
groupVisibility,
userMaximum);
eventStoreService.saveEvent(createGroupEvent);
inviteService.createLink(groupId);
User user = new User(account.getName(), "", "", "");
groupService.addUser(account, groupId);
groupService.updateTitle(account, groupId, title);
groupService.updateDescription(account, groupId, description);
groupService.updateRole(user, groupId);
return groupId;
public static long getUserMaximum(boolean isInfinite, long userMaximum) {
return isInfinite ? Long.MAX_VALUE : userMaximum;
}
}

View File

@ -12,7 +12,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@ -31,20 +31,21 @@ public final class CsvService {
return reader.<User>readValues(stream).readAll();
}
//TODO: CsvService
static List<User> readCsvFile(MultipartFile file) throws EventException {
if (file == null) {
return new ArrayList<>();
return Collections.emptyList();
}
if (!file.isEmpty()) {
try {
List<User> userList = read(file.getInputStream());
return userList.stream().distinct().collect(Collectors.toList()); //filters duplicates from list
return userList.stream()
.distinct()
.collect(Collectors.toList()); //filter duplicates from list
} catch (IOException ex) {
log.error("File konnte nicht gelesen werden");
log.error("File konnte nicht gelesen werden:\n{}", ex.getMessage());
throw new WrongFileException(file.getOriginalFilename());
}
}
return new ArrayList<>();
return Collections.emptyList();
}
}

View File

@ -8,6 +8,7 @@ import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.DeleteUserEvent;
import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent;
@ -42,93 +43,109 @@ public class GroupService {
this.projectionService = projectionService;
}
static User getVeteranMember(Account account, Group group) {
List<User> members = group.getMembers();
String newAdminId;
if (members.get(0).getId().equals(account.getName())) {
newAdminId = members.get(1).getId();
} else {
newAdminId = members.get(0).getId();
}
return new User(newAdminId, "", "", "");
// ################################# GRUPPE ERSTELLEN ########################################
/**
* Wie createGroup, nur das hier die Gruppe auch als Veranstaltung gesetzt werden kann und CSV Dateien mit Nutzern
* eingelesen werden können.
*
* @param account Der Nutzer der die Gruppe erstellt
* @param title Parameter für die neue Gruppe
* @param description Parameter für die neue Gruppe
* @param visibility Parameter für die neue Gruppe
* @param userMaximum Parameter für die neue Gruppe
* @param parent Parameter für die neue Gruppe
* @param file Parameter für die neue Gruppe
*/
//TODO: add wrapper (GroupMeta)?
//TODO: auslagern teilweise -> EventBuilderService
public void createGroupAsOrga(Account account,
String title,
String description,
Visibility visibility,
GroupType groupType,
long userMaximum,
UUID parent,
MultipartFile file) {
// CSV-Import
List<User> newUsers = CsvService.readCsvFile(file);
newUsers.remove(new User(account));
long newUserMaximum = adjustUserMaximum(newUsers.size(), 1L, userMaximum);
UUID groupId = createGroup(account,
title,
description,
visibility,
groupType,
newUserMaximum,
parent);
addUserList(newUsers, groupId);
}
/**
* Wenn die maximale Useranzahl unendlich ist, wird das Maximum auf 100000 gesetzt. Praktisch gibt es also Maximla 100000
* Nutzer pro Gruppe.
* 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.
* Aus diesen Event-Objekten wird eine Liste erzeugt, welche daraufhin mithilfe des EventServices gesichert wird.
*
* @param isMaximumInfinite Gibt an ob es unendlich viele User geben soll
* @param userMaximum Das Maximum an Usern, falls es eins gibt
*
* @return Maximum an Usern
* @param account Keycloak-Account
* @param title Gruppentitel
* @param description Gruppenbeschreibung
*/
static Long checkInfiniteUsers(Boolean isMaximumInfinite, Long userMaximum) {
isMaximumInfinite = isMaximumInfinite != null;
//TODO: add wrapper?
//TODO: auslagern teilweise -> EventBuilderService
public UUID createGroup(Account account,
String title,
String description,
Visibility visibility,
GroupType groupType,
Long userMaximum,
UUID parent) {
if (isMaximumInfinite) {
userMaximum = 100_000L;
}
UUID groupId = UUID.randomUUID();
return userMaximum;
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId,
account.getName(),
parent,
groupType,
visibility,
userMaximum);
eventStoreService.saveEvent(createGroupEvent);
inviteService.createLink(groupId);
User user = new User(account);
addUser(account, groupId);
updateTitle(account, groupId, title);
updateDescription(account, groupId, description);
updateRole(user, groupId);
return groupId;
}
static void removeOldUsersFromNewUsers(List<User> oldUsers, List<User> newUsers) {
for (User oldUser : oldUsers) {
newUsers.remove(oldUser);
}
}
static Long adjustUserMaximum(Long newUsers, Long oldUsers, Long maxUsers) {
if (oldUsers + newUsers > maxUsers) {
maxUsers = oldUsers + newUsers;
}
return maxUsers;
}
// ################################ GRUPPENMANIPULATION ######################################
static Visibility setGroupVisibility(Boolean isVisibilityPrivate) {
isVisibilityPrivate = isVisibilityPrivate != null;
if (isVisibilityPrivate) {
return Visibility.PRIVATE;
} else {
return Visibility.PUBLIC;
}
}
static GroupType setGroupType(Boolean isLecture) {
isLecture = isLecture != null;
if (isLecture) {
return GroupType.LECTURE;
} else {
return GroupType.SIMPLE;
}
}
//TODO: GroupService/eventbuilderservice
void addUserList(List<User> newUsers, UUID groupId) {
Group group = projectionService.projectSingleGroup(groupId);
for (User user : newUsers) {
Group group = projectionService.projectSingleGroup(groupId);
if (group.getMembers().contains(user)) {
log.info("Benutzer {} ist bereits in Gruppe", user.getId());
} else {
AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail());
AddUserEvent addUserEvent = new AddUserEvent(groupId, user);
eventStoreService.saveEvent(addUserEvent);
}
}
}
//TODO: GroupService/eventbuilderservice
public void addUser(Account account, UUID groupId) {
AddUserEvent addUserEvent = new AddUserEvent(groupId, account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
eventStoreService.saveEvent(addUserEvent);
}
//TODO: GroupService/eventbuilderservice
void updateTitle(Account account, UUID groupId, String title) {
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(groupId, account.getName(), title);
eventStoreService.saveEvent(updateGroupTitleEvent);
}
//TODO: GroupService/eventbuilderservice
public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
@ -143,12 +160,6 @@ public class GroupService {
eventStoreService.saveEvent(updateRoleEvent);
}
//TODO: GroupService/eventbuilderservice
void updateDescription(Account account, UUID groupId, String description) {
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(groupId, account.getName(), description);
eventStoreService.saveEvent(updateGroupDescriptionEvent);
}
//TODO: GroupService
public void addUsersFromCsv(Account account, MultipartFile file, String groupId) {
Group group = projectionService.projectSingleGroup(UUID.fromString(groupId));
@ -158,7 +169,10 @@ public class GroupService {
UUID groupUUID = IdService.stringToUUID(groupId);
Long newUserMaximum = adjustUserMaximum((long) newUserList.size(), (long) group.getMembers().size(), group.getUserMaximum());
Long newUserMaximum = adjustUserMaximum(newUserList.size(),
group.getMembers().size(),
group.getUserMaximum());
if (newUserMaximum > group.getUserMaximum()) {
updateMaxUser(account, groupUUID, newUserMaximum);
}
@ -166,12 +180,6 @@ public class GroupService {
addUserList(newUserList, groupUUID);
}
//TODO: GroupService/eventbuilderservice
public void updateMaxUser(Account account, UUID groupId, Long userMaximum) {
UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId, account.getName(), userMaximum);
eventStoreService.saveEvent(updateUserMaxEvent);
}
//TODO: GroupService
public void changeMetaData(Account account, Group group, String title, String description) {
if (!title.equals(group.getTitle())) {
@ -183,15 +191,6 @@ public class GroupService {
}
}
//TODO: GroupService oder in Group?
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!IdService.idIsEmpty(parentId)) {
parent = projectionService.projectSingleGroup(parentId);
}
return parent;
}
//TODO: GroupService
public void deleteUser(Account account, User user, Group group) throws EventException {
changeRoleIfLastAdmin(account, group);
@ -205,19 +204,6 @@ public class GroupService {
}
}
//TODO: GroupService/eventbuilderservice
private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventStoreService.saveEvent(deleteUserEvent);
}
//TODO: GroupService/eventbuilderservice
public void deleteGroupEvent(String userId, UUID groupId) {
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(groupId, userId);
inviteService.destroyLink(groupId);
eventStoreService.saveEvent(deleteGroupEvent);
}
//TODO: GroupService
private void promoteVeteranMember(Account account, Group group) {
if (validationService.checkIfLastAdmin(account, group)) {
@ -245,4 +231,81 @@ public class GroupService {
updateRole(user, group.getId());
}
// ############################### GRUPEN ANFRAGEN ###########################################
static User getVeteranMember(Account account, Group group) {
List<User> members = group.getMembers();
String newAdminId;
if (members.get(0).getId().equals(account.getName())) {
newAdminId = members.get(1).getId();
} else {
newAdminId = members.get(0).getId();
}
return new User(newAdminId);
}
static long adjustUserMaximum(long newUsers, long oldUsers, long maxUsers) {
return Math.max(oldUsers + newUsers, maxUsers);
}
private static void removeOldUsersFromNewUsers(List<User> oldUsers, List<User> newUsers) {
for (User oldUser : oldUsers) {
newUsers.remove(oldUser);
}
}
//TODO: GroupService oder in Group?
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!IdService.idIsEmpty(parentId)) {
parent = projectionService.projectSingleGroup(parentId);
}
return parent;
}
//TODO: Eventbuilderservice
// ################################### EVENTS ################################################
//TODO: GroupService/eventbuilderservice
private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventStoreService.saveEvent(deleteUserEvent);
}
//TODO: GroupService/eventbuilderservice
public void deleteGroupEvent(String userId, UUID groupId) {
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(groupId, userId);
inviteService.destroyLink(groupId);
eventStoreService.saveEvent(deleteGroupEvent);
}
//TODO: GroupService/eventbuilderservice
void updateDescription(Account account, UUID groupId, String description) {
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(groupId, account.getName(), description);
eventStoreService.saveEvent(updateGroupDescriptionEvent);
}
//TODO: GroupService/eventbuilderservice
public void updateMaxUser(Account account, UUID groupId, Long userMaximum) {
UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId, account.getName(), userMaximum);
eventStoreService.saveEvent(updateUserMaxEvent);
}
//TODO: GroupService/eventbuilderservice
public void addUser(Account account, UUID groupId) {
AddUserEvent addUserEvent = new AddUserEvent(groupId, account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
eventStoreService.saveEvent(addUserEvent);
}
//TODO: GroupService/eventbuilderservice
void updateTitle(Account account, UUID groupId, String title) {
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(groupId, account.getName(), title);
eventStoreService.saveEvent(updateGroupTitleEvent);
}
}

View File

@ -16,7 +16,7 @@ public final class JsonService {
private JsonService() {}
/**
* Übersetzt mithilfe der Jackson-Library eine Java-Event-Repräsentation zu einem JSON-Event-Payload.
* Übersetzt eine Java-Event-Repräsentation zu einem JSON-Event-Payload.
*
* @param event Java-Event-Repräsentation
*
@ -31,7 +31,7 @@ public final class JsonService {
}
/**
* Übersetzt mithilfe der Jackson-Library einen JSON-Event-Payload zu einer Java-Event-Repräsentation.
* Übersetzt eine JSON-Event-Payload zu einer Java-Event-Repräsentation.
*
* @param json JSON-Event-Payload als String
*

View File

@ -21,27 +21,9 @@ public class SearchService {
}
/**
* Sortiert die übergebene Liste an Gruppen, sodass Veranstaltungen am Anfang der Liste sind.
*
* @param groups Die Liste von Gruppen die sortiert werden soll
*/
//TODO: ProjectionService/SearchSortService
static void sortByGroupType(List<Group> groups) {
groups.sort((Group g1, Group g2) -> {
if (g1.getType() == GroupType.LECTURE) {
return -1;
}
if (g2.getType() == GroupType.LECTURE) {
return 0;
}
return 1;
});
}
/**
* Filtert alle öffentliche Gruppen nach dem Suchbegriff und gibt diese als Liste von Gruppen zurück.
* Groß und Kleinschreibung wird nicht beachtet.
* Filtert alle öffentliche Gruppen nach dem Suchbegriff und gibt diese als sortierte Liste zurück.
* Groß- und Kleinschreibung wird nicht beachtet.
* Der Suchbegriff wird im Gruppentitel und in der Beschreibung gesucht.
*
* @param search Der Suchstring
*
@ -49,7 +31,6 @@ public class SearchService {
*
* @throws EventException Projektionsfehler
*/
//TODO: remove account
@Cacheable("groups")
public List<Group> searchPublicGroups(String search, String userId) throws EventException {
List<Group> groups = projectionService.projectPublicGroups();
@ -63,14 +44,25 @@ public class SearchService {
log.trace("Es wurde gesucht nach: {}", search);
return groups.stream()
.filter(group -> groupMetaContains(group, search))
.filter(group -> group.toString().toLowerCase().contains(search.toLowerCase()))
.collect(Collectors.toList());
}
private static boolean groupMetaContains(Group group, String string) {
String meta = group.getTitle().toLowerCase() + " " + group.getDescription().toLowerCase();
String pattern = string.toLowerCase();
/**
* Sortiert die übergebene Liste an Gruppen, sodass Veranstaltungen am Anfang der Liste sind.
*
* @param groups Die Liste von Gruppen die sortiert werden soll
*/
private static void sortByGroupType(List<Group> groups) {
groups.sort((Group g1, Group g2) -> {
if (g1.getType() == GroupType.LECTURE) {
return -1;
}
if (g2.getType() == GroupType.LECTURE) {
return 0;
}
return meta.contains(pattern);
return 1;
});
}
}

View File

@ -2,6 +2,7 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org"
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}"
xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="utf-8">
<title>Gruppenerstellung</title>
@ -14,7 +15,9 @@
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</th:block>
</head>
<body>
<header>
<nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation" th:switch="${account.getRoles().contains('orga')}">
<ul>
@ -33,65 +36,58 @@
</ul>
</nav>
</header>
<main th:fragment="bodycontent">
<div class="container-fluid">
<div class="row">
<div class="col-10">
<h1>Gruppenerstellung</h1>
<form enctype="multipart/form-data" method="post"
th:action="@{/gruppen2/createOrga}">
<div class="shadow-sm p-2"
style=" border: 10px solid aliceblue; background: aliceblue">
<form enctype="multipart/form-data" method="post" th:action="@{/gruppen2/createOrga}">
<div class="shadow-sm p-2" style=" border: 10px solid aliceblue; background: aliceblue">
<div class="form-group">
<label for="titel">Titel</label>
<input class="form-control" id="titel" required th:name="title"
type="text">
<input class="form-control" id="titel" required th:name="title" type="text">
</div>
<div class="form-group">
<label for="description">Beschreibung</label>
<textarea class="form-control" id="description" required
rows="3" th:name="description"></textarea>
<textarea class="form-control" id="description" required rows="3" th:name="description"></textarea>
</div>
<div class="custom-control custom-checkbox">
<input class="custom-control-input" id="maxInfiniteUsers" th:name="maxInfiniteUsers"
type="checkbox">
<!--DO NOT WRAP-->
<input type="hidden" name="maxInfiniteUsers" value="0"/><input class="custom-control-input" type="checkbox" id="maxInfiniteUsers" onclick="this.previousSibling.value=1-this.previousSibling.value"/>
<label class="custom-control-label" for="maxInfiniteUsers">Anzahl
unbegrenzt</label>
</div>
<div class="form-group mt-3" id="userMaximum">
<label for="userMaximum">Teilnehmeranzahl</label>
<input class="form-control" th:name="userMaximum"
type="number" min="1" max="10000">
<input class="form-control" th:name="userMaximum" type="number" min="1" max="100000" value="1">
</div>
<div class="custom-control custom-checkbox" id="privateCheckbox">
<input class="custom-control-input" id="visibility" th:name="visibility"
type="checkbox">
<label class="custom-control-label" for="visibility">Private
Gruppe</label>
<!--DO NOT WRAP-->
<input type="hidden" name="visibility" value="0"/><input class="custom-control-input" type="checkbox" id="visibility" onclick="this.previousSibling.value=1-this.previousSibling.value"/>
<label class="custom-control-label" for="visibility">Privat</label>
</div>
<div class="custom-control custom-checkbox" id="lectureCheckbox">
<input class="custom-control-input" id="lecture" th:name="lecture"
type="checkbox">
<!--DO NOT WRAP-->
<input type="hidden" name="lecture" value="0"/><input class="custom-control-input" type="checkbox" id="lecture" onclick="this.previousSibling.value=1-this.previousSibling.value"/>
<label class="custom-control-label" for="lecture">Veranstaltung</label>
</div>
<div class="form-group" id="lectureParent">
<label for="parent"></label>
<select class="form-control" id="parent" name="parent">
<option disabled selected="true">--Bitte Veranstaltung auswählen--
</option>
<option th:each="lecture : ${lectures}" th:name="parent" th:value="${lecture.getId()}" th:text="${lecture.getTitle()}">
<select class="form-control" id="parent" th:name="parent">
<option disabled selected>--Bitte Veranstaltung auswählen--
</option>
<option th:each="lecture : ${lectures}" th:name="parent" th:value="${lecture.getId()}" th:text="${lecture.getTitle()}"></option>
</select>
</div>
<div class="form-group pt-4">
<div class="row">
<div class="col">
<div class="custom-file">
<input class="custom-file-input" id="file" th:name="file"
type="file">
<label class="custom-file-label" for="file">CSV Datei von
Mitgliedern
hochladen</label>
<input class="custom-file-input" id="file" th:name="file" type="file">
</div>
</div>
</div>
@ -107,38 +103,46 @@
</div>
</div>
</div>
<script>
//TODO: Hab ich kaputt gemacht
// Add the following code if you want the name of the file appear on select
$(".custom-file-input").on("change", function () {
var fileName = $(this).val().split("\\").pop();
const fileName = $(this).val().split("\\").pop();
$(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
// Collapsing lectureParent if lecture is checked
// Collapse lectureParent if lecture
$(document).ready(function () {
$('#lecture').change(function () {
$('#lectureParent').fadeToggle();
$('#lectureParent').prop('disabled', function (i, v) { return !v; });
});
});
// Collapse provateCheckbox if lecture
$(document).ready(function () {
$('#lecture').change(function () {
$('#privateCheckbox').fadeToggle();
$('#privateCheckbox').prop('disabled', function (i, v) { return !v; });
});
});
// Collapse lectureCheckbox if private
$(document).ready(function () {
$('#visibility').change(function () {
$('#lectureCheckbox').fadeToggle();
$('#lectureCheckbox').prop('disabled', function (i, v) { return !v; });
});
});
// Collapse userMaximum if infinite
$(document).ready(function () {
$('#maxInfiniteUsers').change(function () {
$('#userMaximum').fadeToggle();
$('#userMaximum').prop('disabled', function (i, v) { return !v; });
});
});
</script>
</main>
</body>
</html>

View File

@ -43,17 +43,15 @@
<div class="form-group">
<label for="titel">Titel</label>
<input class="form-control" id="titel" required th:name="title"
type="text">
<input class="form-control" id="titel" required th:name="title" type="text">
</div>
<div class="form-group">
<label for="description">Beschreibung</label>
<textarea class="form-control" id="description" required
rows="3" th:name="description"></textarea>
<textarea class="form-control" id="description" required rows="3" th:name="description"></textarea>
</div>
<div class="custom-control custom-checkbox">
<input class="custom-control-input" id="maxInfiniteUsers" th:name="maxInfiniteUsers"
type="checkbox">
<!--DO NOT WRAP-->
<input type="hidden" name="maxInfiniteUsers" value="0"/><input class="custom-control-input" type="checkbox" id="maxInfiniteUsers" onclick="this.previousSibling.value=1-this.previousSibling.value"/>
<label class="custom-control-label" for="maxInfiniteUsers">Anzahl
unbegrenzt</label>
</div>
@ -63,15 +61,14 @@
type="number" min="1" max="10000">
</div>
<div class="custom-control custom-checkbox">
<input class="custom-control-input" id="visibility" th:name="visibility"
type="checkbox">
<label class="custom-control-label" for="visibility">Private
Gruppe</label>
<!--DO NOT WRAP-->
<input type="hidden" name="visibility" value="0"/><input class="custom-control-input" type="checkbox" id="visibility" onclick="this.previousSibling.value=1-this.previousSibling.value"/>
<label class="custom-control-label" for="visibility">Privat</label>
</div>
<div class="form-group" id="lectureParent">
<label for="parent"></label>
<select class="form-control" id="parent" name="parent">
<option disabled selected="true">--Bitte Veranstaltung auswählen--
<option disabled selected>--Bitte Veranstaltung auswählen--
</option>
<option th:each="lecture : ${lectures}" th:name="parent" th:value="${lecture.getId()}" th:text="${lecture.getTitle()}">
</option>

View File

@ -2,16 +2,7 @@ package mops.gruppen2.service;
import mops.gruppen2.Gruppen2Application;
import mops.gruppen2.domain.Account;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.exception.UserNotFoundException;
import mops.gruppen2.repository.EventRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -19,16 +10,6 @@ import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
//TODO: Alles in die entsprechenden Klassen sortieren :((((
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class)
@ -56,6 +37,7 @@ class ControllerServiceTest {
@Autowired
ProjectionService projectionService;
/*
@BeforeEach
void setUp() {
Set<String> roles = new HashSet<>();
@ -68,7 +50,7 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithNoParentAndLimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", null, null, null, 20L, null);
groupService.createGroup(account, "test", "hi", null, null, null, 20L, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -78,7 +60,7 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithNoParentAndUnlimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -88,7 +70,7 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithNoParentAndUnlimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", true, null, true, null, null);
groupService.createGroup(account, "test", "hi", true, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
@ -98,7 +80,7 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithNoParentAndLimitedNumberTest() {
controllerService.createGroup(account, "test", "hi", true, null, null, 20L, null);
groupService.createGroup(account, "test", "hi", true, null, null, 20L, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
@ -108,9 +90,9 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, true, true, null, null, null);
groupService.createGroupAsOrga(account2, "test", "hi", false, true, true, 1L, null, null);
List<Group> groups1 = projectionService.projectUserGroups(account2.getName());
controllerService.createGroup(account, "test", "hi", true, null, null, 20L, groups1.get(0).getId());
groupService.createGroup(account, "test", "hi", true, null, null, 20L, groups1.get(0).getId());
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
@ -120,9 +102,9 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null);
groupService.createGroupAsOrga(account2, "test", "hi", false, false, true, 1L, null, null);
List<Group> groups1 = projectionService.projectUserGroups(account2.getName());
controllerService.createGroup(account, "test", "hi", null, null, null, 20L, groups1.get(0).getId());
groupService.createGroup(account, "test", "hi", null, null, null, 20L, groups1.get(0).getId());
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -132,9 +114,9 @@ class ControllerServiceTest {
@Test
void createPublicGroupWithParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null);
groupService.createGroupAsOrga(account2, "test", "hi", false, false, true, 1L, null, null);
List<Group> groups1 = projectionService.projectUserGroups(account2.getName());
controllerService.createGroup(account, "test", "hi", null, true, true, null, groups1.get(0).getId());
groupService.createGroup(account, "test", "hi", null, true, true, null, groups1.get(0).getId());
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PUBLIC, groups.get(0).getVisibility());
@ -144,9 +126,9 @@ class ControllerServiceTest {
@Test
void createPrivateGroupWithParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account2, "test", "hi", null, null, true, null, null, null);
groupService.createGroupAsOrga(account2, "test", "hi", false, false, true, 1L, null, null);
List<Group> groups1 = projectionService.projectUserGroups(account2.getName());
controllerService.createGroup(account, "test", "hi", true, true, true, null, groups1.get(0).getId());
groupService.createGroup(account, "test", "hi", true, true, true, null, groups1.get(0).getId());
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(Visibility.PRIVATE, groups.get(0).getVisibility());
@ -156,7 +138,7 @@ class ControllerServiceTest {
@Test
void createPublicOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, null, null, 20L, null, null);
groupService.createGroupAsOrga(account, "test", "hi", false, false, false, 20L, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType());
@ -167,7 +149,7 @@ class ControllerServiceTest {
@Test
void createPublicOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, null, true, null, null, null);
groupService.createGroupAsOrga(account, "test", "hi", false, false, true, 1L, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType());
@ -178,7 +160,7 @@ class ControllerServiceTest {
@Test
void createPrivateOrgaGroupWithNoParentAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", true, null, null, 20L, null, null);
groupService.createGroupAsOrga(account, "test", "hi", true, false, false, 20L, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType());
@ -189,7 +171,7 @@ class ControllerServiceTest {
@Test
void createPrivateOrgaGroupWithNoParentAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", true, null, true, null, null, null);
groupService.createGroupAsOrga(account, "test", "hi", true, false, true, 1L, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.SIMPLE, groups.get(0).getType());
@ -200,7 +182,7 @@ class ControllerServiceTest {
@Test
void createOrgaLectureGroupAndLimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, true, null, 20L, null, null);
groupService.createGroupAsOrga(account, "test", "hi", false, true, false, 20L, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.LECTURE, groups.get(0).getType());
@ -211,7 +193,7 @@ class ControllerServiceTest {
@Test
void createOrgaLectureGroupAndUnlimitedNumberTest() throws IOException {
controllerService.createGroupAsOrga(account, "test", "hi", null, true, true, null, null, null);
groupService.createGroupAsOrga(account, "test", "hi", false, true, true, 1L, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
testTitleAndDescription(groups.get(0).getTitle(), groups.get(0).getDescription());
assertEquals(GroupType.LECTURE, groups.get(0).getType());
@ -224,7 +206,7 @@ class ControllerServiceTest {
@Disabled
@Test
public void deleteUserTest() {
controllerService.createGroup(account, "test", "hi", true, true, true, null, null);
groupService.createGroup(account, "test", "hi", true, true, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
groupService.addUser(account2, groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
@ -236,7 +218,7 @@ class ControllerServiceTest {
@Disabled
@Test
public void updateRoleAdminTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
groupService.addUser(account2, groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
@ -249,7 +231,7 @@ class ControllerServiceTest {
@Disabled
@Test
public void updateRoleMemberTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
groupService.addUser(account2, groups.get(0).getId());
User user = new User(account2.getName(), "", "", "");
@ -261,7 +243,7 @@ class ControllerServiceTest {
//TODO: GroupServiceTest
@Test
public void updateRoleNonUserTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
User user = new User(account2.getName(), "", "", "");
Throwable exception = assertThrows(UserNotFoundException.class, () -> groupService.updateRole(user, groups.get(0).getId()));
@ -271,7 +253,7 @@ class ControllerServiceTest {
//TODO: GroupServiceTest
@Test
public void deleteNonUserTest() {
controllerService.createGroup(account, "test", "hi", true, null, true, null, null);
groupService.createGroup(account, "test", "hi", true, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
User user = new User(account2.getName(), "", "", "");
Throwable exception = assertThrows(UserNotFoundException.class, () -> groupService.deleteUser(account, user, groups.get(0)));
@ -287,7 +269,7 @@ class ControllerServiceTest {
@Disabled
@Test
void passIfLastAdminTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
groupService.addUser(account2, groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
@ -301,7 +283,7 @@ class ControllerServiceTest {
@Disabled
@Test
void dontPassIfNotLastAdminTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
groupService.addUser(account2, groups.get(0).getId());
User user2 = new User(account2.getName(), "", "", "");
@ -318,7 +300,7 @@ class ControllerServiceTest {
@Disabled
@Test
void getVeteranMemberTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
groupService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = projectionService.projectUserGroups(account.getName());
groupService.addUser(account2, groups.get(0).getId());
groupService.addUser(account3, groups.get(0).getId());
@ -328,5 +310,5 @@ class ControllerServiceTest {
groups = projectionService.projectUserGroups(account2.getName());
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));
}
}*/
}