Merge remote-tracking branch 'origin/master' into updateGroupsAPI
# Conflicts: # src/main/java/mops/gruppen2/repository/EventRepository.java
This commit is contained in:
@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,12 @@ 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 > :status")
|
||||
public Iterable<EventDTO> findNewEventSinceStatus(@Param("status") Long status);
|
||||
|
||||
|
||||
29
src/main/java/mops/gruppen2/service/ControllerService.java
Normal file
29
src/main/java/mops/gruppen2/service/ControllerService.java
Normal 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);
|
||||
}
|
||||
}
|
||||
@ -20,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());
|
||||
@ -39,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();
|
||||
@ -53,7 +64,11 @@ public class EventService {
|
||||
return tmpId;
|
||||
}
|
||||
|
||||
|
||||
/** Findet alle Events welche ab dem neuen Status hinzugekommen sind
|
||||
*
|
||||
* @param status
|
||||
* @return Liste von Events
|
||||
*/
|
||||
public List<Event> getNewEvents(Long status){
|
||||
Iterable<EventDTO> newEventDTOS = eventStore.findNewEventSinceStatus(status);
|
||||
List<Long> groupIdsThatChanged = this.getAllGroupIds(newEventDTOS);
|
||||
@ -62,7 +77,12 @@ public class EventService {
|
||||
return translateEventDTOs(groupEventDTOS);
|
||||
}
|
||||
|
||||
private List<Event> translateEventDTOs(Iterable<EventDTO> 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<>();
|
||||
|
||||
for (EventDTO eventDTO : eventDTOS) {
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
package mops.gruppen2.service;
|
||||
|
||||
import mops.gruppen2.domain.EventDTO;
|
||||
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.ArrayList;
|
||||
@ -14,11 +16,23 @@ 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<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<>();
|
||||
|
||||
27
src/main/java/mops/gruppen2/service/UserService.java
Normal file
27
src/main/java/mops/gruppen2/service/UserService.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user