1

Merge pull request #61 from hhu-propra2/groups-for-user

Groups for user
This commit is contained in:
Talha Caliskan
2020-03-12 16:25:36 +01:00
committed by GitHub
15 changed files with 299 additions and 139 deletions

View File

@ -17,10 +17,4 @@ public class Gruppen2Config {
GroupService groupService;
@Autowired
EventService eventService;
@Bean
public List<Group> groups() throws EventException {
return groupService.projectEventList(eventService.findAllEvents());
}
}

View File

@ -0,0 +1,44 @@
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.event.Event;
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")
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;
this.eventService = eventService;
this.groupService = groupService;
}
@GetMapping("/updatedGroups/{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 {
List<Event> events = eventService.getNewEvents(status);
return groupService.projectEventList(events);
}
}

View File

@ -1,14 +1,16 @@
package mops.gruppen2.controller;
import mops.gruppen2.config.Gruppen2Config;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.GroupType;
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.EventService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.*;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -27,11 +29,15 @@ public class Gruppen2Controller {
private final KeyCloakService keyCloakService;
private final EventService eventService;
private final GroupService groupService;
private final UserService userService;
private final ControllerService controllerService;
public Gruppen2Controller(KeyCloakService keyCloakService, EventService eventService, GroupService groupService) {
public Gruppen2Controller(KeyCloakService keyCloakService, EventService eventService, GroupService groupService, UserService userService, ControllerService controllerService) {
this.keyCloakService = keyCloakService;
this.eventService = eventService;
this.groupService = groupService;
this.userService = userService;
this.controllerService = controllerService;
}
/**
@ -43,8 +49,12 @@ public class Gruppen2Controller {
*/
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("")
public String index(KeycloakAuthenticationToken token, Model model) {
public String index(KeycloakAuthenticationToken token, Model model) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("gruppen", userService.getUserGroups(user.getUser_id()));
return "index";
}
@ -67,15 +77,10 @@ public class Gruppen2Controller {
@RequestParam(value = "title") String title,
@RequestParam(value = "beschreibung") String beschreibung) {
//Refoctor
Account account = keyCloakService.createAccountFromPrincipal(token);
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());
eventService.saveEvent(createGroupEvent);
eventService.saveEvent(addUserEvent);
controllerService.createGroup(account, title, beschreibung);
return "redirect:/";
return "redirect:/gruppen2";
}
}

View File

@ -1,54 +0,0 @@
package mops.gruppen2.controller;
import com.github.javafaker.Faker;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mops.gruppen2.domain.ProductSwaggerExample;
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("/products")
public class SwaggerAPIController {
private final Faker faker = new Faker();
private final List<ProductSwaggerExample> products = new ArrayList<>();
private final SerializationService serializationService;
public SwaggerAPIController(SerializationService serializationService) {
this.serializationService = serializationService;
}
@GetMapping("/get/all")
@ApiOperation(value = "Erzeugt eine Liste mit allen gespeicherten Produkten")
public List<ProductSwaggerExample> getProducts() {
return products;
}
@GetMapping("/get/{index}")
public ProductSwaggerExample getProduct(@ApiParam("Produkt Index") @PathVariable int index) {
return products.get(index);
}
@PostMapping("/save")
public String saveProduct(@RequestBody ProductSwaggerExample product) {
products.add(product);
return "Product saved successfully";
}
@PostMapping("/random")
public String saveRandomProduct() {
products.add(new ProductSwaggerExample(faker.food().ingredient(), "Empty"));
return "Product saved successfully";
}
}

View File

@ -9,9 +9,7 @@ import lombok.Value;
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"givenname", "familyname", "email"})
public class User {
String user_id;
String givenname;
String familyname;
String email;

View File

@ -17,4 +17,9 @@ public class UpdateGroupDescriptionEvent extends Event {
super(event_id, group_id, user_id);
this.newGroupDescription = newGroupDescription;
}
public UpdateGroupDescriptionEvent(Long group_id, String user_id, String newGroupDescription) {
super(group_id, user_id);
this.newGroupDescription = newGroupDescription;
}
}

View File

@ -17,4 +17,9 @@ public class UpdateGroupTitleEvent extends Event {
super(event_id, group_id, user_id);
this.newGroupTitle = newGroupTitle;
}
public UpdateGroupTitleEvent(Long group_id, String user_id, String newGroupTitle) {
super(group_id, user_id);
this.newGroupTitle = newGroupTitle;
}
}

View File

@ -1,9 +1,21 @@
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;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface EventRepository extends CrudRepository<EventDTO, Long> {
@Query("select distinct group_id from event where user_id =:id")
List<Long> findGroup_idsWhereUser_id(@Param("id") String user_id);
@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);
}

View File

@ -0,0 +1,29 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.*;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
@Service
public class ControllerService {
private final EventService eventService;
public ControllerService(EventService eventService) {
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);
eventService.saveEvent(createGroupEvent);
eventService.saveEvent(addUserEvent);
eventService.saveEvent(updateGroupTitleEvent);
eventService.saveEvent(updateGroupDescriptionEvent);
}
}

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import mops.gruppen2.domain.EventDTO;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -19,13 +20,20 @@ public class EventService {
this.eventStore = eventStore;
}
/** sichert ein Event Objekt indem es ein EventDTO Objekt erzeugt
*
* @param event
*/
public void saveEvent(Event event){
EventDTO eventDTO = getDTO(event);
eventStore.save(eventDTO);
}
/** Erzeugt aus einem Event Objekt ein EventDTO Objekt
*
* @param event
* @return EventDTO
*/
public EventDTO getDTO(Event event){
EventDTO eventDTO = new EventDTO();
eventDTO.setGroup_id(event.getGroup_id());
@ -38,6 +46,10 @@ public class EventService {
return eventDTO;
}
/** Sorgt dafür die Group_id immer um 1 zu erhöhen
*
* @return Gibt Long zurück
*/
public Long checkGroup() {
Long tmpId = 1L;
Iterable<EventDTO> eventDTOS = eventStore.findAll();
@ -52,16 +64,33 @@ public class EventService {
return tmpId;
}
public List<Event> findAllEvents() {
Iterable<EventDTO> eventDTOS = eventStore.findAll();
/** Findet alle Events welche ab dem neuen Status hinzugekommen sind
*
* @param status
* @return Liste von Events
*/
public List<Event> getNewEvents(Long status){
Iterable<EventDTO> eventDTOS = eventStore.findNewEventSinceStatus(status);
return translateEventDTOs(eventDTOS);
}
/** Erzeugt aus der Datenbank eine Liste von Events
*
* @param eventDTOS
* @return Liste von Events
*/
public List<Event> translateEventDTOs(Iterable<EventDTO> eventDTOS){
List<Event> events = new ArrayList<>();
eventDTOS.forEach(eventDTO -> {
for (EventDTO eventDTO : eventDTOS) {
try {
events.add(serializationService.deserializeEvent(eventDTO.getEvent_payload()));
}catch (JsonProcessingException e) {
e.printStackTrace();
}
});
}
return events;
}

View File

@ -1,12 +1,10 @@
package mops.gruppen2.service;
import lombok.EqualsAndHashCode;
import mops.gruppen2.domain.EventDTO;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -18,41 +16,40 @@ import java.util.Map;
public class GroupService {
private final EventService eventService;
private final EventRepository eventRepository;
public GroupService(EventService eventService) {
public GroupService(EventService eventService, EventRepository eventRepository) {
this.eventService = eventService;
this.eventRepository = eventRepository;
}
public List<Group> projectEventList(Map<Long, Group> groupMap, List<Event> events) throws EventException {
public List<Event> getGroupEvents(List<Long> group_ids) {
List<EventDTO> eventDTOS = new ArrayList<>();
List<Event> events = new ArrayList<>();
for (Long group_id: group_ids) {
eventDTOS.addAll(eventRepository.findEventDTOByGroup_id(group_id));
}
return events = eventService.translateEventDTOs(eventDTOS);
}
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<Long, Group> groupMap = new HashMap<>();
for (Event event : events) {
if (event instanceof CreateGroupEvent) {
groupMap.put(event.getGroup_id(), new Group());
}
if (event instanceof DeleteGroupEvent) {
groupMap.remove(event.getGroup_id());
} else {
try {
Group group = groupMap.get(event.getGroup_id());
if (group == null) {
throw new GroupDoesNotExistException("Gruppe " + event.getGroup_id() + " existiert nicht");
}
group.applyEvent(event);
} catch (EventException e) {
if (e instanceof GroupDoesNotExistException) {
throw e;
}
e.printStackTrace();
}
}
getOrCreateGroup(groupMap, event.getGroup_id()).applyEvent(event);
}
return new ArrayList<>(groupMap.values());
}
public List<Group> projectEventList(List<Event> events) throws EventException {
return projectEventList(new HashMap<>(), events);
//
private Group getOrCreateGroup(Map<Long, Group> groups, long group_id) {
if (!groups.containsKey(group_id)) {
groups.put(group_id, new Group());
}
return groups.get(group_id);
}
}

View File

@ -0,0 +1,27 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
final EventRepository eventRepository;
final GroupService groupService;
public UserService(EventRepository eventRepository, GroupService groupService) {
this.eventRepository = eventRepository;
this.groupService = groupService;
}
public List<Group> getUserGroups(String user_id) throws EventException {
List<Long> group_ids = eventRepository.findGroup_idsWhereUser_id(user_id);
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events);
}
}

View File

@ -28,9 +28,11 @@
<h1>Meine Gruppen</h1>
<div class="container-fluid">
<form action="/" method="get">
<div th:each="gruppe: ${gruppen}">
<div style="border: 10px solid aliceblue; background: aliceblue">
<h4 style="color: dodgerblue; font-weight: bold">Titel der Gruppe</h4>
<p>Beschreibung der Gruppe ...</p>
<h4 th:href="url" style="color: dodgerblue; font-weight: bold" th:text="${gruppe.getTitle()}"></h4>
<p th:text="${gruppe.getDescription()}"></p>
</div>
</div>
</form>
</div>

View File

@ -1,7 +1,7 @@
package mops.gruppen2.builder;
import com.github.javafaker.Faker;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.*;
import mops.gruppen2.domain.event.*;
import java.util.ArrayList;
@ -9,19 +9,64 @@ import java.util.List;
public class EventBuilder {
public static CreateGroupEvent randomCreateGroupEvent() {
Faker faker = new Faker();
/**
* Generiert ein EventLog mit mehreren Gruppen nud Usern
*
* @param count Gruppenanzahl
* @param membercount Gesamte Mitgliederanzahl
* @return
*/
public static List<Event> completeGroups(int count, int membercount) {
List<Event> eventList = new ArrayList<>();
return null;/*new CreateGroupEvent(
faker.random().nextLong(),
faker.random().nextLong(),
faker.random().hex(),
faker.leagueOfLegends().champion(),
faker.leagueOfLegends().quote()
);*/
for (int i = 1; i <= count; i++) {
eventList.addAll(EventBuilder.completeGroup(i, membercount / count));
}
public static AddUserEvent randomAddUserEvent(long group_id) {
return eventList;
}
public static List<Event> completeGroup(long group_id, int membercount) {
List<Event> eventList = new ArrayList<>();
eventList.add(EventBuilder.createGroupEvent(group_id));
eventList.add(EventBuilder.updateGroupTitleEvent(group_id));
eventList.add(EventBuilder.updateGroupDescriptionEvent(group_id));
eventList.addAll(EventBuilder.addUserEvents(membercount, group_id));
return eventList;
}
public static CreateGroupEvent createGroupEvent(long group_id) {
Faker faker = new Faker();
return new CreateGroupEvent(
group_id,
faker.random().hex(),
null,
GroupType.SIMPLE,
Visibility.PRIVATE
);
}
/**
* Generiert mehrere CreateGroupEvents, 1 <= group_id <= count
*
* @param count Anzahl der verschiedenen Gruppen.
* @return
*/
public static List<CreateGroupEvent> createGroupEvents(int count) {
List<CreateGroupEvent> eventList = new ArrayList<>();
for (int i = 1; i <= count; i++) {
eventList.add(createGroupEvent(i));
}
return eventList;
}
public static AddUserEvent addUserEvent(long group_id, String user_id) {
Faker faker = new Faker();
String firstname = faker.name().firstName();
@ -30,24 +75,31 @@ public class EventBuilder {
return new AddUserEvent(
faker.random().nextLong(),
group_id,
faker.random().hex(),
user_id,
firstname,
lastname,
firstname + "." + lastname + "@mail.de"
);
}
public static List<Event> randomAddUserEvents(int count, long group_id) {
/**
* Generiert mehrere AddUserEvents für eine Gruppe, 1 <= user_id <= count
*
* @param count
* @param group_id
* @return
*/
public static List<Event> addUserEvents(int count, long group_id) {
List<Event> eventList = new ArrayList<>();
for (int i = 0; i < count; i++) {
eventList.add(EventBuilder.randomAddUserEvent(group_id));
for (int i = 1; i <= count; i++) {
eventList.add(EventBuilder.addUserEvent(group_id, "" + i));
}
return eventList;
}
public static DeleteUserEvent randomDeleteUserEvent(long group_id, String user_id) {
public static DeleteUserEvent deleteUserEvent(long group_id, String user_id) {
Faker faker = new Faker();
return new DeleteUserEvent(
@ -57,7 +109,23 @@ public class EventBuilder {
);
}
public static UpdateGroupDescriptionEvent randomUpdateGroupDescriptionEvent(long group_id) {
/**
* Erzeugt mehrere DeleteUserEvents, sodass eine Gruppe komplett geleert wird
*
* @param group Gruppe welche geleert wird
* @return
*/
public static List<DeleteUserEvent> deleteUserEvents(Group group) {
List<DeleteUserEvent> eventList = new ArrayList<>();
for (User user : group.getMembers()) {
eventList.add(EventBuilder.deleteUserEvent(group.getId(), user.getUser_id()));
}
return eventList;
}
public static UpdateGroupDescriptionEvent updateGroupDescriptionEvent(long group_id) {
Faker faker = new Faker();
return new UpdateGroupDescriptionEvent(
@ -68,7 +136,7 @@ public class EventBuilder {
);
}
public static UpdateGroupTitleEvent randomUpdateGroupTitleEvent(long group_id) {
public static UpdateGroupTitleEvent updateGroupTitleEvent(long group_id) {
Faker faker = new Faker();
return new UpdateGroupTitleEvent(

View File

@ -1,7 +1,6 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Exceptions.GroupDoesNotExistException;
import mops.gruppen2.domain.Exceptions.UserNotFoundException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Visibility;
@ -9,28 +8,28 @@ import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.configuration.IMockitoConfiguration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
class GroupServiceTest {
GroupService groupService;
EventRepository eventRepository;
@BeforeEach
public void setUp() {
groupService = new GroupService(mock(EventService.class));
groupService = new GroupService(mock(EventService.class), eventRepository);
}
@Disabled
@Test
void applyEventOnGroupThatIsDeleted() throws Exception {
List<Event> eventList = new ArrayList<>();
@ -47,6 +46,7 @@ class GroupServiceTest {
});
}
@Disabled
@Test
void returnDeletedGroup() throws Exception {
List<Event> eventList = new ArrayList<>();
@ -70,5 +70,4 @@ class GroupServiceTest {
assertThat(groupService.projectEventList(eventList).get(0)).isInstanceOf(Group.class);
}
}