1

Merge branch 'master' into group-details

This commit is contained in:
kasch309
2020-03-13 16:36:13 +01:00
committed by GitHub
13 changed files with 163 additions and 225 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

@ -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

@ -10,7 +10,7 @@ import java.util.List;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UpdatedGroupRequestMapper {
public class UpdatedGroupRequestMapper {
private Long status;
private List<Group> groupList;
}
}

View File

@ -14,8 +14,12 @@ public class UpdateRoleEvent extends Event {
Role newRole;
public UpdateRoleEvent(Long event_id, Long group_id, String user_id, Role newRole) {
super(event_id,+ group_id, user_id);
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;
}
public UpdateRoleEvent(Long group_id, String user_id, Role 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

@ -6,6 +6,7 @@ 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 {
@ -16,17 +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);
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(eventService.checkGroup(),account.getName(), Role.ADMIN);
/**
* 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);
eventService.saveEvent(updateRoleEvent);
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

@ -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;