added Creating Groups to frontend and saving them in Database. Also prepared some Methods for Snapshot db
This commit is contained in:
@ -1,22 +1,29 @@
|
||||
package mops.gruppen2.controller;
|
||||
|
||||
import mops.gruppen2.domain.event.CreateGroupEvent;
|
||||
import mops.gruppen2.service.EventService;
|
||||
import mops.gruppen2.service.GroupService;
|
||||
import mops.gruppen2.service.KeyCloakService;
|
||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.swing.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/gruppen2")
|
||||
public class Gruppen2Controller {
|
||||
|
||||
private final KeyCloakService keyCloakService;
|
||||
private final EventService eventService;
|
||||
private final GroupService groupService;
|
||||
|
||||
public Gruppen2Controller(KeyCloakService keyCloakService) {
|
||||
public Gruppen2Controller(KeyCloakService keyCloakService, EventService eventService, GroupService groupService) {
|
||||
this.keyCloakService = keyCloakService;
|
||||
this.eventService = eventService;
|
||||
this.groupService = groupService;
|
||||
}
|
||||
|
||||
/**Zeigt die index.html an.
|
||||
@ -46,5 +53,17 @@ public class Gruppen2Controller {
|
||||
return "search";
|
||||
}
|
||||
|
||||
@PostMapping("/createGroup")
|
||||
public String pCreateGroup(KeycloakAuthenticationToken token,
|
||||
@RequestParam(value = "title") String title,
|
||||
@RequestParam(value = "beschreibung") String beschreibung) {
|
||||
|
||||
//Hier muss alles in eine separate Klasse
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(eventService.checkGroup(), "faker", title, beschreibung);
|
||||
eventService.saveEvent(createGroupEvent);
|
||||
groupService.buildGroupFromEvent(createGroupEvent);
|
||||
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package mops.gruppen2.domain;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Value;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
|
||||
@ -9,8 +8,8 @@ import org.springframework.data.relational.core.mapping.Table;
|
||||
@Data
|
||||
public class EventDTO {
|
||||
@Id
|
||||
long event_id;
|
||||
long group_id;
|
||||
Long event_id;
|
||||
Long group_id;
|
||||
String user_id;
|
||||
String event_payload;
|
||||
}
|
||||
|
@ -16,14 +16,14 @@ public class AddUserEvent extends Event {
|
||||
String familyname;
|
||||
String email;
|
||||
|
||||
public AddUserEvent(long event_id, long group_id, String user_id, String givenname, String familyname, String email) {
|
||||
public AddUserEvent(Long event_id, Long group_id, String user_id, String givenname, String familyname, String email) {
|
||||
super(event_id, group_id, user_id);
|
||||
this.givenname = givenname;
|
||||
this.familyname = familyname;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public AddUserEvent(long event_id, long group_id, User user) {
|
||||
public AddUserEvent(Long event_id, Long group_id, User user) {
|
||||
super(event_id, group_id, user.getUser_id());
|
||||
this.givenname = user.getGivenname();
|
||||
this.familyname = user.getFamilyname();
|
||||
|
@ -11,9 +11,14 @@ public class CreateGroupEvent extends Event {
|
||||
String groupTitle;
|
||||
String groupDescription;
|
||||
|
||||
public CreateGroupEvent(long event_id, long group_id, String user_id, String groupTitle, String groupDescription) {
|
||||
public CreateGroupEvent(long event_id, Long group_id, String user_id, String groupTitle, String groupDescription) {
|
||||
super(event_id, group_id, user_id);
|
||||
this.groupTitle = groupTitle;
|
||||
this.groupDescription = groupDescription;
|
||||
}
|
||||
public CreateGroupEvent(Long group_id, String user_id, String groupTitle, String groupDescription) {
|
||||
super(group_id, user_id);
|
||||
this.groupTitle = groupTitle;
|
||||
this.groupDescription = groupDescription;
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,9 @@ import lombok.*;
|
||||
* Entfernt ein einzelnes Mitglied einer Gruppe.
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class DeleteUserEvent extends Event {
|
||||
|
||||
public DeleteUserEvent(long event_id, long group_id, String user_id) {
|
||||
public DeleteUserEvent(Long event_id, Long group_id, String user_id) {
|
||||
super(event_id, group_id, user_id);
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,12 @@ import lombok.NoArgsConstructor;
|
||||
@JsonSubTypes.Type(value = AddUserEvent.class, name = "UpdateRoleEvent"),
|
||||
})
|
||||
public class Event {
|
||||
long event_id;
|
||||
long group_id;
|
||||
Long event_id;
|
||||
Long group_id;
|
||||
String user_id;
|
||||
|
||||
public Event(Long group_id,String user_id){
|
||||
this.group_id = group_id;
|
||||
this.user_id = user_id;
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
|
||||
public class UpdateGroupDescriptionEvent extends Event {
|
||||
String newGroupDescription;
|
||||
|
||||
public UpdateGroupDescriptionEvent(long event_id, long group_id, String user_id, String newGroupDescription) {
|
||||
public UpdateGroupDescriptionEvent(Long event_id, Long group_id, String user_id, String newGroupDescription) {
|
||||
super(event_id, group_id, user_id);
|
||||
this.newGroupDescription = newGroupDescription;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
|
||||
public class UpdateGroupTitleEvent extends Event {
|
||||
String newGroupTitle;
|
||||
|
||||
public UpdateGroupTitleEvent(long event_id, long group_id, String user_id, String newGroupTitle) {
|
||||
public UpdateGroupTitleEvent(Long event_id, Long group_id, String user_id, String newGroupTitle) {
|
||||
super(event_id, group_id, user_id);
|
||||
this.newGroupTitle = newGroupTitle;
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ public class UpdateRoleEvent extends Event {
|
||||
|
||||
Role newRole;
|
||||
|
||||
public UpdateRoleEvent(long event_id, long group_id, String user_id, Role newRole) {
|
||||
public UpdateRoleEvent(Long event_id, Long group_id, String user_id, Role newRole) {
|
||||
super(event_id, group_id, user_id);
|
||||
|
||||
this.newRole = newRole;
|
||||
|
51
src/main/java/mops/gruppen2/service/EventService.java
Normal file
51
src/main/java/mops/gruppen2/service/EventService.java
Normal file
@ -0,0 +1,51 @@
|
||||
package mops.gruppen2.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import mops.gruppen2.domain.EventDTO;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
import mops.gruppen2.repository.EventRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EventService {
|
||||
private final SerializationService serializationService;
|
||||
private final EventRepository eventStore;
|
||||
|
||||
public EventService(SerializationService serializationService, EventRepository eventStore) {
|
||||
this.serializationService = serializationService;
|
||||
this.eventStore = eventStore;
|
||||
}
|
||||
|
||||
|
||||
public void saveEvent(Event event){
|
||||
EventDTO eventDTO = getDTO(event);
|
||||
eventStore.save(eventDTO);
|
||||
|
||||
}
|
||||
|
||||
public EventDTO getDTO(Event event){
|
||||
EventDTO eventDTO = new EventDTO();
|
||||
eventDTO.setGroup_id(event.getGroup_id());
|
||||
eventDTO.setUser_id(event.getUser_id());
|
||||
try {
|
||||
eventDTO.setEvent_payload(serializationService.serializeEvent(event));
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return eventDTO;
|
||||
}
|
||||
|
||||
public Long checkGroup() {
|
||||
Long tmpId = 1L;
|
||||
Iterable<EventDTO> eventDTOS = eventStore.findAll();
|
||||
for (EventDTO event : eventDTOS) {
|
||||
if (event.getGroup_id() == null) {
|
||||
return tmpId;
|
||||
}
|
||||
if (tmpId <= event.getGroup_id()) {
|
||||
tmpId++;
|
||||
}
|
||||
}
|
||||
return tmpId;
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import mops.gruppen2.domain.Group;
|
||||
import mops.gruppen2.domain.event.Event;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@ -15,9 +16,17 @@ public class GroupService {
|
||||
* @param eventList Die Events für diese Gruppe
|
||||
* @return Gruppe auf aktuellem Stand
|
||||
*/
|
||||
Group buildGroupFromEvents(List<Event> eventList) {
|
||||
public Group buildGroupFromEvents(List<Event> eventList) {
|
||||
Group newGroup = new Group();
|
||||
newGroup.apply(eventList);
|
||||
return newGroup;
|
||||
}
|
||||
|
||||
public Group buildGroupFromEvent(Event event){
|
||||
Group newGroup = new Group();
|
||||
List<Event> eventList = new ArrayList<>();
|
||||
eventList.add(event);
|
||||
newGroup.apply(eventList);
|
||||
return newGroup;
|
||||
}
|
||||
}
|
||||
|
@ -29,16 +29,5 @@ public class SerializationService {
|
||||
return json;
|
||||
}
|
||||
|
||||
public void saveEvent(Event event){
|
||||
try {
|
||||
EventDTO eventDTO = new EventDTO();
|
||||
eventDTO.setGroup_id(event.getGroup_id());
|
||||
eventDTO.setUser_id(event.getUser_id());
|
||||
eventDTO.setEvent_payload(serializeEvent(event));
|
||||
eventStore.save(eventDTO);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -27,20 +27,20 @@
|
||||
<main th:fragment="bodycontent">
|
||||
<h1>Gruppenerstellung</h1>
|
||||
<div class="container-fluid">
|
||||
<form method="post" action="/">
|
||||
<form method="post" action="/gruppen2/createGroup">
|
||||
<div style="border: 10px solid aliceblue; background: aliceblue">
|
||||
<div class="form-group">
|
||||
<label for="titel">Name der Gruppe</label>
|
||||
<input type="text" class="form-control" id="titel" th:value="${titel}">
|
||||
<input type="text" class="form-control" id="titel" th:name="title">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="beschreibung">Beschreibung der Gruppe</label>
|
||||
<textarea type="text" class="form-control" id="beschreibung" th:value="${beschreibung}">
|
||||
<textarea type="text" class="form-control" id="beschreibung" th:name="beschreibung">
|
||||
|
||||
</textarea>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
|
||||
<input type="submit">
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
@ -23,25 +23,25 @@ class GroupTest {
|
||||
|
||||
@Test
|
||||
void createSingleGroup() {
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,2, "asd", "hello", "foo");
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,2L, "asd", "hello", "foo");
|
||||
|
||||
Group group = new Group();
|
||||
group.apply(createGroupEvent);
|
||||
|
||||
assertThat(group.getDescription()).isEqualTo("foo");
|
||||
assertThat(group.getTitle()).isEqualTo("hello");
|
||||
assertThat(group.getId()).isEqualTo(2);
|
||||
assertThat(group.getId()).isEqualTo(2L);
|
||||
}
|
||||
|
||||
// Verwendet CreateGroupEvent
|
||||
@Test
|
||||
void addSingleUser() {
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1,1,"prof1", "hi", "foo");
|
||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(1L,1L,"prof1", "hi", "foo");
|
||||
Group group = new Group();
|
||||
group.apply(createGroupEvent);
|
||||
|
||||
User user = new User("prof", "jens", "bendi", "hi@gmail.com");
|
||||
AddUserEvent addUserEvent = new AddUserEvent(1,1, user);
|
||||
AddUserEvent addUserEvent = new AddUserEvent(1L,1L, user);
|
||||
group.apply(addUserEvent);
|
||||
|
||||
assertThat(group.getMembers().get(0)).isEqualTo(user);
|
||||
|
@ -0,0 +1,7 @@
|
||||
package mops.gruppen2.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EventServiceTest {
|
||||
|
||||
}
|
@ -28,7 +28,7 @@ class SerializationServiceTest {
|
||||
|
||||
@Test
|
||||
void serializeEventTest() {
|
||||
event = new Event(1,1,"1");
|
||||
event = new Event(1L,1L,"1");
|
||||
SerializationService serializationService = new SerializationService(eventRepository);
|
||||
try {
|
||||
assertThat(serializationService.serializeEvent(event)).isEqualTo("{\"Event\":{\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}}");
|
||||
|
Reference in New Issue
Block a user