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;
|
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 mops.gruppen2.service.KeyCloakService;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
import javax.annotation.security.RolesAllowed;
|
import javax.annotation.security.RolesAllowed;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/gruppen2")
|
@RequestMapping("/gruppen2")
|
||||||
public class Gruppen2Controller {
|
public class Gruppen2Controller {
|
||||||
|
|
||||||
private final KeyCloakService keyCloakService;
|
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.keyCloakService = keyCloakService;
|
||||||
|
this.eventService = eventService;
|
||||||
|
this.groupService = groupService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Zeigt die index.html an.
|
/**Zeigt die index.html an.
|
||||||
@ -46,5 +53,17 @@ public class Gruppen2Controller {
|
|||||||
return "search";
|
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;
|
package mops.gruppen2.domain;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Value;
|
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.relational.core.mapping.Table;
|
import org.springframework.data.relational.core.mapping.Table;
|
||||||
|
|
||||||
@ -9,8 +8,8 @@ import org.springframework.data.relational.core.mapping.Table;
|
|||||||
@Data
|
@Data
|
||||||
public class EventDTO {
|
public class EventDTO {
|
||||||
@Id
|
@Id
|
||||||
long event_id;
|
Long event_id;
|
||||||
long group_id;
|
Long group_id;
|
||||||
String user_id;
|
String user_id;
|
||||||
String event_payload;
|
String event_payload;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,14 +16,14 @@ public class AddUserEvent extends Event {
|
|||||||
String familyname;
|
String familyname;
|
||||||
String email;
|
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);
|
super(event_id, group_id, user_id);
|
||||||
this.givenname = givenname;
|
this.givenname = givenname;
|
||||||
this.familyname = familyname;
|
this.familyname = familyname;
|
||||||
this.email = email;
|
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());
|
super(event_id, group_id, user.getUser_id());
|
||||||
this.givenname = user.getGivenname();
|
this.givenname = user.getGivenname();
|
||||||
this.familyname = user.getFamilyname();
|
this.familyname = user.getFamilyname();
|
||||||
|
|||||||
@ -11,9 +11,14 @@ public class CreateGroupEvent extends Event {
|
|||||||
String groupTitle;
|
String groupTitle;
|
||||||
String groupDescription;
|
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);
|
super(event_id, group_id, user_id);
|
||||||
this.groupTitle = groupTitle;
|
this.groupTitle = groupTitle;
|
||||||
this.groupDescription = groupDescription;
|
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.
|
* Entfernt ein einzelnes Mitglied einer Gruppe.
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class DeleteUserEvent extends Event {
|
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);
|
super(event_id, group_id, user_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,7 +24,12 @@ import lombok.NoArgsConstructor;
|
|||||||
@JsonSubTypes.Type(value = AddUserEvent.class, name = "UpdateRoleEvent"),
|
@JsonSubTypes.Type(value = AddUserEvent.class, name = "UpdateRoleEvent"),
|
||||||
})
|
})
|
||||||
public class Event {
|
public class Event {
|
||||||
long event_id;
|
Long event_id;
|
||||||
long group_id;
|
Long group_id;
|
||||||
String user_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 {
|
public class UpdateGroupDescriptionEvent extends Event {
|
||||||
String newGroupDescription;
|
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);
|
super(event_id, group_id, user_id);
|
||||||
this.newGroupDescription = newGroupDescription;
|
this.newGroupDescription = newGroupDescription;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
|
|||||||
public class UpdateGroupTitleEvent extends Event {
|
public class UpdateGroupTitleEvent extends Event {
|
||||||
String newGroupTitle;
|
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);
|
super(event_id, group_id, user_id);
|
||||||
this.newGroupTitle = newGroupTitle;
|
this.newGroupTitle = newGroupTitle;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ public class UpdateRoleEvent extends Event {
|
|||||||
|
|
||||||
Role newRole;
|
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);
|
super(event_id, group_id, user_id);
|
||||||
|
|
||||||
this.newRole = newRole;
|
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 mops.gruppen2.domain.event.Event;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -15,9 +16,17 @@ public class GroupService {
|
|||||||
* @param eventList Die Events für diese Gruppe
|
* @param eventList Die Events für diese Gruppe
|
||||||
* @return Gruppe auf aktuellem Stand
|
* @return Gruppe auf aktuellem Stand
|
||||||
*/
|
*/
|
||||||
Group buildGroupFromEvents(List<Event> eventList) {
|
public Group buildGroupFromEvents(List<Event> eventList) {
|
||||||
Group newGroup = new Group();
|
Group newGroup = new Group();
|
||||||
newGroup.apply(eventList);
|
newGroup.apply(eventList);
|
||||||
return newGroup;
|
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;
|
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">
|
<main th:fragment="bodycontent">
|
||||||
<h1>Gruppenerstellung</h1>
|
<h1>Gruppenerstellung</h1>
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<form method="post" action="/">
|
<form method="post" action="/gruppen2/createGroup">
|
||||||
<div style="border: 10px solid aliceblue; background: aliceblue">
|
<div style="border: 10px solid aliceblue; background: aliceblue">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="titel">Name der Gruppe</label>
|
<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>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="beschreibung">Beschreibung der Gruppe</label>
|
<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>
|
</textarea>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<input type="submit">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@ -23,25 +23,25 @@ class GroupTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createSingleGroup() {
|
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 group = new Group();
|
||||||
group.apply(createGroupEvent);
|
group.apply(createGroupEvent);
|
||||||
|
|
||||||
assertThat(group.getDescription()).isEqualTo("foo");
|
assertThat(group.getDescription()).isEqualTo("foo");
|
||||||
assertThat(group.getTitle()).isEqualTo("hello");
|
assertThat(group.getTitle()).isEqualTo("hello");
|
||||||
assertThat(group.getId()).isEqualTo(2);
|
assertThat(group.getId()).isEqualTo(2L);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verwendet CreateGroupEvent
|
// Verwendet CreateGroupEvent
|
||||||
@Test
|
@Test
|
||||||
void addSingleUser() {
|
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 group = new Group();
|
||||||
group.apply(createGroupEvent);
|
group.apply(createGroupEvent);
|
||||||
|
|
||||||
User user = new User("prof", "jens", "bendi", "hi@gmail.com");
|
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);
|
group.apply(addUserEvent);
|
||||||
|
|
||||||
assertThat(group.getMembers().get(0)).isEqualTo(user);
|
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
|
@Test
|
||||||
void serializeEventTest() {
|
void serializeEventTest() {
|
||||||
event = new Event(1,1,"1");
|
event = new Event(1L,1L,"1");
|
||||||
SerializationService serializationService = new SerializationService(eventRepository);
|
SerializationService serializationService = new SerializationService(eventRepository);
|
||||||
try {
|
try {
|
||||||
assertThat(serializationService.serializeEvent(event)).isEqualTo("{\"Event\":{\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}}");
|
assertThat(serializationService.serializeEvent(event)).isEqualTo("{\"Event\":{\"event_id\":1,\"group_id\":1,\"user_id\":\"1\"}}");
|
||||||
|
|||||||
Reference in New Issue
Block a user