1

Merge pull request #128 from hhu-propra2/refactor-controller

Refactor controller
This commit is contained in:
Christoph
2020-03-25 13:08:01 +01:00
committed by GitHub
8 changed files with 74 additions and 68 deletions

View File

@ -1,16 +1,18 @@
package mops.gruppen2.controller;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import mops.gruppen2.config.Gruppen2Config;
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.domain.exception.*;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.CsvService;
@ -18,8 +20,6 @@ import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@ -41,22 +41,18 @@ import java.util.UUID;
@Controller
@SessionScope
@RequestMapping("/gruppen2")
public class Gruppen2Controller {
public class WebController {
private final KeyCloakService keyCloakService;
private final GroupService groupService;
private final UserService userService;
private final ControllerService controllerService;
private final Gruppen2Config gruppen2Config;
private final Logger logger = LoggerFactory.getLogger("Gruppen2ControllerLogger");
;
public Gruppen2Controller(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService, Gruppen2Config gruppen2Config) {
public WebController(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService) {
this.keyCloakService = keyCloakService;
this.groupService = groupService;
this.userService = userService;
this.controllerService = controllerService;
this.gruppen2Config = gruppen2Config;
}
/**
@ -99,29 +95,8 @@ public class Gruppen2Controller {
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
if (userMaximum == null) {
userMaximum = 100000L;
}
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if (userList.size() > userMaximum) {
userMaximum = (long) userList.size() + userMaximum;
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
logger.warn("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
visibility = visibility == null;
lecture = lecture != null;
maxInfiniteUsers = maxInfiniteUsers != null;
UUID parentUUID = controllerService.getUUID(parent);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, userList);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
return "redirect:/gruppen2/";
}
@ -173,7 +148,6 @@ public class Gruppen2Controller {
}
UUID groupUUID = controllerService.getUUID(groupId);
controllerService.addUserList(userList, groupUUID);
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -229,7 +203,7 @@ public class Gruppen2Controller {
if (search != null) {
groupse = groupService.findGroupWith(search, account);
}
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("account", account);
model.addAttribute("gruppen", groupse);
return "search";
}
@ -238,6 +212,7 @@ public class Gruppen2Controller {
@GetMapping("/details/{id}")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, HttpServletRequest request, @PathVariable("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(groupId));
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
@ -283,7 +258,6 @@ public class Gruppen2Controller {
public String joinGroup(KeycloakAuthenticationToken token,
Model model, @RequestParam("id") String 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(UUID.fromString(groupId));
@ -302,9 +276,9 @@ public class Gruppen2Controller {
@GetMapping("/detailsSearch")
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token,
Model model,
@RequestParam("id") String id) throws EventException {
@RequestParam("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(id));
Group group = userService.getGroupById(UUID.fromString(groupId));
UUID parentId = group.getParent();
Group parent = new Group();
@ -393,8 +367,6 @@ public class Gruppen2Controller {
public String changeRole(KeycloakAuthenticationToken token,
@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))) {

View File

@ -3,6 +3,7 @@ package mops.gruppen2.domain.exception;
import org.springframework.http.HttpStatus;
public class NoAccessException extends EventException {
public NoAccessException(String info) {
super(HttpStatus.FORBIDDEN, "Hier hast du leider keinen Zugriff!", info);
}

View File

@ -3,6 +3,7 @@ package mops.gruppen2.domain.exception;
import org.springframework.http.HttpStatus;
public class PageNotFoundException extends EventException {
public PageNotFoundException(String info) {
super(HttpStatus.NOT_FOUND, "Die Seite wurde nicht gefunden!", info);
}

View File

@ -1,5 +1,6 @@
package mops.gruppen2.service;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
@ -15,9 +16,13 @@ import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.domain.event.UpdateUserMaxEvent;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.UserNotFoundException;
import mops.gruppen2.domain.exception.WrongFileException;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.CharConversionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -71,10 +76,27 @@ public class ControllerService {
updateRole(account.getName(), groupId);
}
public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, UUID parent, List<User> users) throws EventException {
public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, UUID parent, MultipartFile file) throws EventException, IOException {
List<User> userList = new ArrayList<>();
if (userMaximum == null) {
userMaximum = 100000L;
}
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if (userList.size() > userMaximum) {
userMaximum = (long) userList.size() + userMaximum;
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
logger.warning("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
visibility = visibility == null;
lecture = lecture != null;
maxInfiniteUsers = maxInfiniteUsers != null;
Visibility visibility1;
UUID groupId = eventService.checkGroup();
if (visibility) {
visibility1 = Visibility.PUBLIC;
} else {
@ -100,7 +122,7 @@ public class ControllerService {
updateTitle(account, groupId, title);
updateDescription(account, groupId, description);
updateRole(account.getName(), groupId);
addUserList(users, groupId);
addUserList(userList, groupId);
}

View File

@ -5,7 +5,6 @@ import mops.gruppen2.domain.User;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -16,12 +15,10 @@ import java.util.UUID;
@Service
public class UserService {
private final EventRepository eventRepository;
private final GroupService groupService;
private final EventService eventService;
public UserService(EventRepository eventRepository, GroupService groupService, EventService eventService) {
this.eventRepository = eventRepository;
public UserService(GroupService groupService, EventService eventService) {
this.groupService = groupService;
this.eventService = eventService;
}

View File

@ -1,7 +1,8 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
<html lang="en"
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}"
xmlns="http://www.w3.org/1999/html">
xmlns="http://www.w3.org/1999/html"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Gruppenerstellung</title>
@ -16,10 +17,11 @@
</head>
<body>
<header>
<nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation" th:switch="${account.getRoles().contains('orga')}">
<nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation"
th:switch="${account.getRoles().contains('orga')}">
<ul>
<li class="active">
<a th:href="@{/gruppen2}" href="/">Gruppen</a>
<a href="/" th:href="@{/gruppen2}">Gruppen</a>
</li>
<li th:case="${true}">
<a href="/createOrga" th:href="@{/gruppen2/createOrga}">Erstellen</a>
@ -28,7 +30,7 @@
<a href="/createStudent" th:href="@{/gruppen2/createStudent}">Erstellen</a>
</li>
<li>
<a th:href="@{/gruppen2/findGroup}" href="/findGroup">Suche</a>
<a href="/findGroup" th:href="@{/gruppen2/findGroup}">Suche</a>
</li>
</ul>
</nav>
@ -38,13 +40,13 @@
<div class="row">
<div class="col-10">
<h1>Metadaten ändern</h1>
<form method="post" action="/gruppen2/details/changeMetadata">
<form action="/gruppen2/details/changeMetadata" method="post">
<div class="shadow-sm p-2"
style=" border: 10px solid aliceblue; background: aliceblue">
<div class="form-group">
<label for="title">Titel</label>
<input class="form-control" id="title" required
type="text" th:name="title" th:value="${title}">
th:name="title" th:value="${title}" type="text">
</div>
<div class="form-group">
<label for="description">Beschreibung</label>
@ -55,9 +57,9 @@
<div class="form-group pt-4">
<button class="btn btn-primary"
style="background: #52a1eb; border-style: none"
th:if="${roles.get(user.getId()) == admin}"
th:name="groupId"
th:value="${groupId}"
th:if="${roles.get(user.getId()) == admin}"
type="submit">Speichern
</button>
</div>

View File

@ -6,7 +6,8 @@
<meta charset="utf-8">
<title>Gruppendetails</title>
<th:block th:fragment="headcontent">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet">
</th:block>
</head>
<body>
@ -31,11 +32,14 @@
<main th:fragment="bodycontent">
<div class="container-fluid">
<div>
<div class="shadow-sm p-4 col-8" style="border: 10px solid aliceblue; display: inline-block; border-radius: 5px; background: aliceblue">
<div class="shadow-sm p-4 col-8"
style="border: 10px solid aliceblue; display: inline-block; border-radius: 5px; background: aliceblue">
<div class="row">
<h1 style="color: black; font-weight: bold; font-optical-sizing: auto; overflow-wrap: break-word; width: 95%" th:text="${group.getTitle()}"></h1>
<a th:href="@{/gruppen2/details/changeMetadata/{id}(id=${group.getId()})}"
class="fa fa-pencil" style="font-size:30px; width: 5%"
<h1 style="color: black; font-weight: bold; font-optical-sizing: auto; overflow-wrap: break-word; width: 95%"
th:text="${group.getTitle()}"></h1>
<a class="fa fa-pencil"
style="font-size:30px; width: 5%"
th:href="@{/gruppen2/details/changeMetadata/{id}(id=${group.getId()})}"
th:if="${roles.get(user.getId()) == admin}"></a>
</div>
<h3>
@ -48,14 +52,20 @@
<span class="badge badge-pill badge-info" style="background: mediumorchid"
th:text="${parent.getTitle()}">Parent</span>
<div class="input-group mb-3" style="margin-top: 10px" th:if="${group.getVisibility() == group.getVisibility().PRIVATE}">
<div class="input-group mb-3" style="margin-top: 10px"
th:if="${group.getVisibility() == group.getVisibility().PRIVATE}">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default" style="background: #52a1eb">Einladungslink:</span>
<span class="input-group-text" id="inputGroup-sizing-default"
style="background: #52a1eb">Einladungslink:</span>
</div>
<input type="text" class="form-control" th:value="${link}"
aria-label="Recipient's username" aria-describedby="basic-addon2" id="groupLink" style="background: white" readonly>
<input aria-describedby="basic-addon2" aria-label="Recipient's username"
class="form-control"
id="groupLink" readonly style="background: white" th:value="${link}"
type="text">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" onclick="copyLink()">Link kopieren</button>
<button class="btn btn-outline-secondary" onclick="copyLink()"
type="button">Link kopieren
</button>
</div>
</div>
</h3>

View File

@ -29,7 +29,8 @@
<button class="btn btn-primary"
style="background: #52a1eb; border-style: none;">
<a style="color: white" href="#" onclick="window.history.back(-1);return false;" role="button">Zurück</a>
<a href="#" onclick="window.history.back(-1);return false;" role="button"
style="color: white">Zurück</a>
</button>
</div>
</div>