1

Merge branch 'master' into change_DTO

This commit is contained in:
Talha Caliskan
2020-03-14 09:28:39 +01:00
committed by GitHub
21 changed files with 326 additions and 236 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

@ -4,23 +4,22 @@ import mops.gruppen2.config.Gruppen2Config;
import mops.gruppen2.domain.Exceptions.EventException;
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.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.ArrayList;
import java.util.List;
import java.util.Set;
@Controller
@RequestMapping("/gruppen2")
@ -96,4 +95,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;
@ -17,9 +16,17 @@ 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]}")
Iterable<EventDTO> findNewEventSinceStatus(@Param("status") Long status);
//@Query("SELECT * FROM event WHERE event_id > ?#{[0]}")
//Iterable<EventDTO> findNewEventSinceStatus(@Param("status") Long status);
@Query("select * from event where visibility =:vis")
List<EventDTO> findEventDTOByVisibility(@Param("vis") Boolean visibility);
@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")
}

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

@ -2,10 +2,12 @@ package mops.gruppen2.service;
import mops.gruppen2.domain.Group;
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 {
@ -16,16 +18,26 @@ 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);
}
public void addUser(Account account, Group group){

View File

@ -83,9 +83,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 einer Liste von eventDTOs eine Liste von Events
@ -106,5 +107,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,12 +22,26 @@ 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);
}
}