1

Merge branch 'refactoring' into error-handling

# Conflicts:
#	src/main/java/mops/gruppen2/controller/Gruppen2Controller.java
#	src/main/java/mops/gruppen2/domain/Exceptions/UserAlreadyExistsException.java
#	src/main/java/mops/gruppen2/domain/Exceptions/UserNotFoundException.java
#	src/main/java/mops/gruppen2/domain/Group.java
#	src/main/java/mops/gruppen2/domain/event/AddUserEvent.java
#	src/main/java/mops/gruppen2/domain/event/CreateGroupEvent.java
#	src/main/java/mops/gruppen2/domain/event/DeleteUserEvent.java
#	src/main/java/mops/gruppen2/domain/event/Event.java
#	src/main/java/mops/gruppen2/domain/event/UpdateGroupDescriptionEvent.java
#	src/main/java/mops/gruppen2/domain/event/UpdateGroupTitleEvent.java
#	src/main/java/mops/gruppen2/domain/event/UpdateRoleEvent.java
#	src/main/java/mops/gruppen2/domain/exception/EventException.java
#	src/main/java/mops/gruppen2/domain/exception/GroupIdMismatchException.java
#	src/main/java/mops/gruppen2/domain/exception/GroupNotFoundException.java
#	src/main/java/mops/gruppen2/domain/exception/NoValueException.java
#	src/main/java/mops/gruppen2/service/ControllerService.java
#	src/main/java/mops/gruppen2/service/GroupService.java
#	src/main/java/mops/gruppen2/service/UserService.java
#	src/main/resources/templates/errorRenameLater.html
This commit is contained in:
Christoph
2020-03-18 23:40:39 +01:00
57 changed files with 741 additions and 631 deletions

View File

@ -3,17 +3,19 @@ package mops.gruppen2.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.apiWrapper.UpdatedGroupRequestMapper;
import mops.gruppen2.domain.api.GroupRequestWrapper;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.service.*;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.service.APIFormatterService;
import mops.gruppen2.service.EventService;
import mops.gruppen2.service.GroupService;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
@ -23,40 +25,37 @@ import java.util.List;
@RequestMapping("/gruppen2/api")
public class APIController {
private final SerializationService serializationService;
private final EventService eventService;
private final GroupService groupService;
public APIController(SerializationService serializationService, EventService eventService, GroupService groupService) {
this.serializationService = serializationService;
public APIController(EventService eventService, GroupService groupService) {
this.eventService = eventService;
this.groupService = groupService;
}
@GetMapping("/updateGroups/{status}")
@Secured("ROLE_api_user")
@ApiOperation(value = "Gibt alle Gruppen zurück in denen sich etwas geändert hat")
public UpdatedGroupRequestMapper updateGroup(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long status) throws EventException {
@ApiOperation("Gibt alle Gruppen zurück in denen sich etwas geändert hat")
public GroupRequestWrapper updateGroup(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long status) throws EventException {
List<Event> events = eventService.getNewEvents(status);
UpdatedGroupRequestMapper updatedGroupRequestMapper = APIFormatterService.wrapp(eventService.getMaxEvent_id(), groupService.projectEventList(events));
return updatedGroupRequestMapper;
return APIFormatterService.wrap(eventService.getMaxEvent_id(), groupService.projectEventList(events));
}
@GetMapping("/getGroupIdsOfUser/{teilnehmer}")
@Secured("ROLE_api_user")
@ApiOperation(value = "Gibt alle Gruppen zurück in denen sich ein Teilnehmer befindet")
public List<Long> getGroupsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String teilnehmer) throws EventException {
@ApiOperation("Gibt alle Gruppen zurück in denen sich ein Teilnehmer befindet")
public List<Long> getGroupsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String teilnehmer) {
return eventService.getGroupsOfUser(teilnehmer);
}
@GetMapping("/getGroup/{groupId}")
@Secured("ROLE_api_user")
@ApiOperation(value = "Gibt die Gruppe mit der als Parameter mitgegebenden groupId zurück")
@ApiOperation("Gibt die Gruppe mit der als Parameter mitgegebenden groupId zurück")
public Group getGroupFromId(@ApiParam("GruppenId der gefordeten Gruppe") @PathVariable Long groupId) throws EventException {
List<Event> eventList = eventService.getEventsOfGroup(groupId);
List<Group> groups = groupService.projectEventList(eventList);
return groups.get(0);
}

View File

@ -6,13 +6,23 @@ import mops.gruppen2.domain.Exceptions.GroupNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.*;
import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.CsvService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.InviteLinkRepositoryService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
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.context.annotation.SessionScope;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
@ -27,14 +37,13 @@ import java.util.List;
@RequestMapping("/gruppen2")
public class Gruppen2Controller {
@Autowired
Gruppen2Config gruppen2Config;
private final KeyCloakService keyCloakService;
private final GroupService groupService;
private final UserService userService;
private final ControllerService controllerService;
private final InviteLinkRepositoryService inviteLinkRepositoryService;
@Autowired
Gruppen2Config gruppen2Config;
public Gruppen2Controller(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService, InviteLinkRepositoryService inviteLinkRepositoryService) {
this.keyCloakService = keyCloakService;
@ -72,19 +81,34 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_actuator)"})
@PostMapping("/createLecture")
public String pCreateLecture(KeycloakAuthenticationToken token,
@RequestParam(value = "title") String title,
@RequestParam(value = "beschreibung") String beschreibung,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "file") MultipartFile file) throws IOException, EventException {
@RequestParam("title") String title,
@RequestParam("beschreibung") String beschreibung,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = CsvService.read(file.getInputStream());
List<User> userList = new ArrayList<>();
if (!file.isEmpty()) {
userList = CsvService.read(file.getInputStream());
}
visibility = visibility == null;
controllerService.createLecture(account, title, beschreibung, visibility, userList);
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_actuator)"})
@PostMapping("/details/members/addUsersFromCsv")
public String addUsersFromCsv(@RequestParam("group_id") Long groupId,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
List<User> userList = new ArrayList<>();
if (!file.isEmpty()) {
userList = CsvService.read(file.getInputStream());
}
controllerService.addUserList(userList, groupId);
return "redirect:/gruppen2/details/members/" + groupId;
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/createGroup")
public String createGroup(KeycloakAuthenticationToken token, Model model) {
@ -94,11 +118,11 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@GetMapping("/findGroup")
public String findGroup(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "suchbegriff", required = false) String suchbegriff) throws EventException {
public String findGroup(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "suchbegriff", required = false) String search) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<Group> groupse = new ArrayList<>();
if (suchbegriff != null) {
groupse = groupService.findGroupWith(suchbegriff,account);
if (search != null) {
groupse = groupService.findGroupWith(search, account);
}
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("gruppen", groupse);
@ -108,8 +132,8 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/createGroup")
public String pCreateGroup(KeycloakAuthenticationToken token,
@RequestParam(value = "title") String title,
@RequestParam(value = "beschreibung") String beschreibung,
@RequestParam("title") String title,
@RequestParam("beschreibung") String beschreibung,
@RequestParam(value = "visibility", required = false) Boolean visibility) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
@ -121,10 +145,10 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details/{id}")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @PathVariable (value="id") Long id) throws EventException, ResponseStatusException {
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @PathVariable("id") Long groupId) throws EventException, ResponseStatusException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(id);
Group group = userService.getGroupById(groupId);
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
if (group != null) {
@ -140,21 +164,23 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/detailsBeitreten")
public String joinGroup(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "id") Long id) throws EventException {
public String joinGroup(KeycloakAuthenticationToken token, Model model, @RequestParam("id") Long groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Account account = keyCloakService.createAccountFromPrincipal (token);
User user = new User(account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail());
Group group = userService.getGroupById(id);
if(group.getMembers().contains(user)) return "errorRenameLater"; //hier soll eigentlich auf die bereits beigetretene Gruppe weitergeleitet werden
controllerService.addUser(account,id);
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(groupId);
if (group.getMembers().contains(user)) {
return "errorRenameLater"; //hier soll eigentlich auf die bereits beigetretene Gruppe weitergeleitet werden
}
controllerService.addUser(account, groupId);
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@GetMapping("/detailsSearch")
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "id") Long id) throws EventException {
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, Model model, @RequestParam("id") Long groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(id);
Group group = userService.getGroupById(groupId);
if (group != null) {
model.addAttribute("group", group);
return "detailsNoMember";
@ -176,19 +202,20 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/leaveGroup")
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam (value="group_id") Long id) throws EventException {
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
controllerService.deleteUser(user.getUser_id(), id);
controllerService.deleteUser(user.getId(), groupId);
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details/members/{id}")
public String editMembers(Model model, KeycloakAuthenticationToken token, @PathVariable (value="id") Long id) throws EventException {
public String editMembers(Model model, KeycloakAuthenticationToken token, @PathVariable("id") Long groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(id);
if(group.getRoles().get(account.getName()) == Role.ADMIN) {
Group group = userService.getGroupById(groupId);
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);
@ -200,22 +227,17 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/changeRole")
public String changeRole(KeycloakAuthenticationToken token, @RequestParam (value = "group_id") Long group_id,
@RequestParam (value = "user_id") String user_id) throws EventException {
controllerService.updateRole(user_id, group_id);
return "redirect:/gruppen2/details/members/" + group_id;
public String changeRole(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId,
@RequestParam("user_id") String userId) throws EventException {
controllerService.updateRole(userId, groupId);
return "redirect:/gruppen2/details/members/" + groupId;
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/deleteUser")
public String deleteUser(KeycloakAuthenticationToken token,@RequestParam (value = "group_id") Long group_id,
@RequestParam (value = "user_id") String user_id) throws EventException {
controllerService.deleteUser(user_id, group_id);
return "redirect:/gruppen2/details/members/" + group_id;
}
@GetMapping("*")
public String defaultLink() {
return "error";
public String deleteUser(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId,
@RequestParam("user_id") String userId) throws EventException {
controllerService.deleteUser(userId, groupId);
return "redirect:/gruppen2/details/members/" + groupId;
}
}

View File

@ -18,4 +18,9 @@ public class MopsController {
request.logout();
return "redirect:/gruppen2/";
}
@GetMapping("*")
public String defaultLink() {
return "errorRenameLater";
}
}