1

Merge branch 'master' into group-create

This commit is contained in:
AndiBuls
2020-03-13 16:59:00 +01:00
committed by GitHub
21 changed files with 303 additions and 230 deletions

View File

@ -32,7 +32,7 @@ public class Gruppen2Application {
public Docket productAPI() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(PathSelectors.ant("/products/**"))
.paths(PathSelectors.ant("/gruppen2/api/**"))
.apis(RequestHandlerSelectors.basePackage("mops.gruppen2"))
.build()
.apiInfo(apiMetadata());

View File

@ -1,26 +1,25 @@
package mops.gruppen2.controller;
import com.github.javafaker.Faker;
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.ProductSwaggerExample;
import mops.gruppen2.domain.apiWrapper.UpdatedGroupRequestMapper;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.service.APIFormatterService;
import mops.gruppen2.service.EventService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.SerializationService;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/**
* Ein Beispiel für eine API mit Swagger.
*/
@RestController
@RequestMapping("/gruppen2")
@RequestMapping("/gruppen2/api")
public class APIController {
private final SerializationService serializationService;
@ -33,12 +32,27 @@ public class APIController {
this.groupService = groupService;
}
@GetMapping("/updatedGroups/{status}")
@GetMapping("/updateGroups/{status}")
@ApiOperation(value = "Gibt alle Gruppen zurück in denen sich etwas geändert hat")
public List<Group> updateGroup(@ApiParam("Status des Anfragestellers") @PathVariable Long status) throws EventException {
public UpdatedGroupRequestMapper updateGroup(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long status) throws EventException {
List<Event> events = eventService.getNewEvents(status);
return groupService.projectEventList(events);
UpdatedGroupRequestMapper updatedGroupRequestMapper = APIFormatterService.wrapp(eventService.getMaxEvent_id(), groupService.projectEventList(events));
return updatedGroupRequestMapper;
}
@GetMapping("/getGroupIdsOfUser/{teilnehmer}")
@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 {
return eventService.getGroupsOfUser(teilnehmer);
}
@GetMapping("/getGroup/{groupId}")
@ApiOperation(value = "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

@ -2,22 +2,21 @@ package mops.gruppen2.controller;
import mops.gruppen2.config.Gruppen2Config;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Group;
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.UpdateGroupDescriptionEvent;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.*;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import javax.annotation.security.RolesAllowed;
import java.util.Set;
@Controller
@RequestMapping("/gruppen2")
@ -83,4 +82,20 @@ public class Gruppen2Controller {
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @RequestParam (value="id") Long id) throws EventException, ResponseStatusException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(id);
Account account = keyCloakService.createAccountFromPrincipal (token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Role role = group.getRoles().get(user);
if(group!= null) {
model.addAttribute("group", group);
model.addAttribute("role",role);
return "detailsMember";
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Group not found");
}
}

View File

@ -3,6 +3,8 @@ package mops.gruppen2.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class MopsController {
@ -10,4 +12,10 @@ public class MopsController {
public String redirect(){
return "redirect:/gruppen2/";
}
@GetMapping("/logout")
public String logout(HttpServletRequest request) throws Exception {
request.logout();
return "redirect:/gruppen2/";
}
}

View File

@ -17,7 +17,7 @@ public class Group extends Aggregate {
private String title;
private String description;
private final List<User> members;
private final Map<User, Role> roles;
private final Map<String, Role> roles;
private GroupType type;
private Visibility visibility;
@ -51,7 +51,7 @@ public class Group extends Aggregate {
if (roles.containsKey(user) && event.getNewRole() == Role.MEMBER) {
roles.remove(user);
} else {
roles.put(user, event.getNewRole());
roles.put(user.getUser_id(), event.getNewRole());
}
}

View File

@ -0,0 +1,16 @@
package mops.gruppen2.domain.apiWrapper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mops.gruppen2.domain.Group;
import java.util.List;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UpdatedGroupRequestMapper {
private Long status;
private List<Group> groupList;
}

View File

@ -15,7 +15,12 @@ public class UpdateRoleEvent extends Event {
public UpdateRoleEvent(Long event_id, Long group_id, String user_id, Role newRole) {
super(event_id, group_id, user_id);
this.newRole = newRole;
}
public UpdateRoleEvent(Long group_id, String user_id, Role newRole) {
super(group_id, user_id);
this.newRole = newRole;
}
}

View File

@ -1,7 +1,6 @@
package mops.gruppen2.repository;
import mops.gruppen2.domain.EventDTO;
import mops.gruppen2.domain.event.Event;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
@ -16,6 +15,13 @@ public interface EventRepository extends CrudRepository<EventDTO, Long> {
@Query("select * from event where group_id =:id")
List<EventDTO> findEventDTOByGroup_id(@Param("id") Long group_id);
@Query("SELECT * FROM event WHERE event_id > ?#{[0]}")
public Iterable<EventDTO> findNewEventSinceStatus(@Param("status") Long status);
@Query("SELECT DISTINCT group_id FROM event WHERE event_id > :status")
public List<Long> findNewEventSinceStatus(@Param("status") Long status);
@Query("SELECT * FROM event WHERE group_id IN (:groupIds) ")
public List<EventDTO> findAllEventsOfGroups(@Param("groupIds") List<Long> groupIds);
@Query("SELECT MAX(event_id) FROM event")
public Long getHighesEvent_ID();
}

View File

@ -0,0 +1,14 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.apiWrapper.UpdatedGroupRequestMapper;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class APIFormatterService {
static public UpdatedGroupRequestMapper wrapp(Long status, List<Group> groupList){
return new UpdatedGroupRequestMapper(status, groupList);
}
}

View File

@ -1,10 +1,12 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.*;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class ControllerService {
@ -15,15 +17,25 @@ public class ControllerService {
this.eventService = eventService;
}
public void createGroup(Account account, String title, String beschreibung) {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(eventService.checkGroup(), account.getName(), null , GroupType.LECTURE, Visibility.PUBLIC);
AddUserEvent addUserEvent = new AddUserEvent(eventService.checkGroup(), account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail());
UpdateGroupTitleEvent updateGroupTitleEvent = new UpdateGroupTitleEvent(eventService.checkGroup(), account.getName(), title);
UpdateGroupDescriptionEvent updateGroupDescriptionEvent = new UpdateGroupDescriptionEvent(eventService.checkGroup(), account.getName(), beschreibung);
/**
* 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 account Keycloak-Account
* @param title Gruppentitel
* @param description Gruppenbeschreibung
*/
public void createGroup(Account account, String title, String description) {
eventService.saveEvent(createGroupEvent);
eventService.saveEvent(addUserEvent);
eventService.saveEvent(updateGroupTitleEvent);
eventService.saveEvent(updateGroupDescriptionEvent);
List<Event> eventList = new ArrayList<>();
Collections.addAll(eventList, new CreateGroupEvent(eventService.checkGroup(), account.getName(), null , GroupType.LECTURE, Visibility.PUBLIC),
new AddUserEvent(eventService.checkGroup(), account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()),
new UpdateRoleEvent(eventService.checkGroup(), account.getName(), Role.ADMIN),
new UpdateGroupTitleEvent(eventService.checkGroup(), account.getName(), title),
new UpdateGroupDescriptionEvent(eventService.checkGroup(), account.getName(), description),
new UpdateRoleEvent(eventService.checkGroup(),account.getName(), Role.ADMIN));
eventService.saveEventList(eventList);
}
}

View File

@ -70,9 +70,10 @@ public class EventService {
* @return Liste von Events
*/
public List<Event> getNewEvents(Long status){
Iterable<EventDTO> eventDTOS = eventStore.findNewEventSinceStatus(status);
List<Long> groupIdsThatChanged = eventStore.findNewEventSinceStatus(status);
return translateEventDTOs(eventDTOS);
List<EventDTO> groupEventDTOS = eventStore.findAllEventsOfGroups(groupIdsThatChanged);
return translateEventDTOs(groupEventDTOS);
}
/** Erzeugt aus der Datenbank eine Liste von Events
@ -94,4 +95,28 @@ public class EventService {
return events;
}
/**
* Sichert eine Liste von Event Objekten mithilfe der Methode saveEvent(Event event)
*
* @param createGroupEvents Liste von Event Objekten
*/
public void saveEventList(List<Event> createGroupEvents) {
for(Event event : createGroupEvents) {
saveEvent(event);
}
}
public Long getMaxEvent_id(){
return eventStore.getHighesEvent_ID();
}
public List<Long> getGroupsOfUser(String userID) {
return eventStore.findGroup_idsWhereUser_id(userID);
}
public List<Event> getEventsOfGroup(Long groupId) {
List<EventDTO> eventDTOList = eventStore.findEventDTOByGroup_id(groupId);
return translateEventDTOs(eventDTOList);
}
}

View File

@ -22,11 +22,25 @@ public class SerializationService {
this.eventStore = eventStore;
}
/**
* Übersetzt mithilfe der Jackson-Library eine Java-Event-Repräsentation zu einem JSON-Event-Payload.
*
* @param event Java-Event-Repräsentation
* @return JSON-Event-Payload als String
* @throws JsonProcessingException
*/
public String serializeEvent(Event event) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(event);
}
/**
* Übersetzt mithilfe der Jackson-Library einen JSON-Event-Payload zu einer Java-Event-Repräsentation.
*
* @param json JSON-Event-Payload als String
* @return Java-Event-Repräsentation
* @throws JsonProcessingException
*/
public Event deserializeEvent(String json) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, Event.class);

View File

@ -6,6 +6,7 @@ import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@ -26,4 +27,11 @@ public class UserService {
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events);
}
public Group getGroupById(Long group_id) throws EventException {
List<Long> group_ids = new ArrayList<>();
group_ids.add(group_id);
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events).get(0);
}
}

View File

@ -4,7 +4,7 @@
xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="utf-8">
<title>Name des Subsystems</title>
<title>Gruppenerstellung</title>
<th:block th:fragment="headcontent">
<!-- Links, Skripts, Styles hier einfügen! -->
</th:block>

View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}">
<head>
<meta charset="utf-8">
<title>Gruppendetails</title>
<th:block th:fragment="headcontent">
<!-- Links, Skripts, Styles hier einfügen! -->
</th:block>
</head>
<body>
<header>
<nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation">
<ul>
<li class="active">
<a th:href="@{/gruppen2}" href="/">Gruppen</a>
</li>
<li>
<a th:href="@{/gruppen2/createGroup}" href="/createGroup">Erstellen</a>
</li>
<li>
<a th:href="@{/gruppen2/findGroup}" href="/findGroup">Suche</a>
</li>
</ul>
</nav>
</header>
<main th:fragment="bodycontent">
<div class="container-fluid">
<form action="/" method="get">
<div style="border: 10px solid aliceblue; background: aliceblue">
<h1 style="color: dodgerblue; font-weight: bold" th:text="${group.getTitle()}"></h1>
<p style="font-weight: bold">
<a th:if="${group.getVisibility() == group.getVisibility().PUBLIC }">Private Gruppe</a>
<a th:if="${group.getVisibility() == group.getVisibility().PRIVATE}">Öffentliche Gruppe</a>
</p>
<p th:text="${group.getDescription()}"></p>
<div class="form-group">
<div >
<div th:if="${role == role.ADMIN}">
<button class="btn btn-primary" type="warning">Gruppenmitglieder bearbeiten</button>
<button class="btn btn-primary" type="danger" style="background: #52a1eb; border-style: none">Gruppe verlassen</button>
</div>
<div th:if="${role == role.MEMBER}">
<button class="btn btn-primary" type="danger" style="background: #52a1eb; border-style: none">Gruppe verlassen</button>
</div>
</div>
</div>
</div>
</form>
</div>
</main>
</body>
</html>

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}">
<head>
<meta charset="utf-8">
<title>Gruppendetails</title>
<th:block th:fragment="headcontent">
<!-- Links, Skripts, Styles hier einfügen! -->
</th:block>
</head>
<body>
<header>
<nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation">
<ul>
<li class="active">
<a th:href="@{/gruppen2}" href="/">Gruppen</a>
</li>
<li>
<a th:href="@{/gruppen2/createGroup}" href="/createGroup">Erstellen</a>
</li>
<li>
<a th:href="@{/gruppen2/findGroup}" href="/findGroup">Suche</a>
</li>
</ul>
</nav>
</header>
<main th:fragment="bodycontent">
</main>
</body>
</html>

View File

@ -3,7 +3,7 @@
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}">
<head>
<meta charset="utf-8">
<title>Name des Subsystems</title>
<title>Eigene Gruppen</title>
<th:block th:fragment="headcontent">
<!-- Links, Skripts, Styles hier einfügen! -->
</th:block>
@ -31,7 +31,7 @@
<div th:each="gruppe: ${gruppen}">
<div style="border: 10px solid aliceblue; background: aliceblue">
<h3>
<a href="url" style="color: dodgerblue; font-weight: bold" th:text="${gruppe.getTitle()}"></a>
<a th:href="@{/gruppen2/details(id=${gruppe.getId()})}" style="color: dodgerblue; font-weight: bold" th:text="${gruppe.getTitle()}"></a>
</h3>
<p th:text="${gruppe.getDescription()}"></p>
</div>

View File

@ -3,7 +3,7 @@
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}">
<head>
<meta charset="utf-8">
<title>Name des Subsystems</title>
<title>Suche</title>
<th:block th:fragment="headcontent">
<!-- Links, Skripts, Styles hier einfügen! -->
</th:block>

View File

@ -115,7 +115,7 @@ class GroupTest {
// Assert
assertThat(group.getRoles())
.containsOnlyKeys(group.getMembers().get(0))
.containsOnlyKeys(group.getMembers().get(0).getUser_id())
.containsValue(Role.ADMIN);
}

View File

@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;