1

add leaving group

Co-Authored-By: xxnitram <xxnitram@users.noreply.github.com>
This commit is contained in:
killerber4t
2020-03-17 14:36:55 +01:00
parent 1acfcc449f
commit 61e007e4d8
5 changed files with 46 additions and 20 deletions

View File

@ -15,13 +15,16 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.web.server.ResponseStatusException;
import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.List;
@Controller
@SessionScope
@RequestMapping("/gruppen2")
public class Gruppen2Controller {
@ -56,7 +59,7 @@ public class Gruppen2Controller {
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()));
model.addAttribute("gruppen", userService.getUserGroups(user));
model.addAttribute("user",user);
return "index";
}
@ -125,4 +128,12 @@ public class Gruppen2Controller {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Group not found");
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/leaveGroup")
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam (value="group_id") Long id) {
Account account = keyCloakService.createAccountFromPrincipal(token);
controllerService.deleteUser(account, id);
System.out.println(id);
return "redirect:/gruppen2/";
}
}

View File

@ -9,6 +9,7 @@ import mops.gruppen2.domain.Group;
* Entfernt ein einzelnes Mitglied einer Gruppe.
*/
@Getter
@NoArgsConstructor
public class DeleteUserEvent extends Event {
public DeleteUserEvent(Long group_id, String user_id) {
super(group_id, user_id);

View File

@ -65,4 +65,10 @@ public class ControllerService {
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(group_id,account.getName(),Role.ADMIN);
eventService.saveEvent(updateRoleEvent);
}
public void deleteUser(Account account, Long group_id){
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(group_id,account.getName());
System.out.println(deleteUserEvent.getGroup_id() + " " + deleteUserEvent.getUser_id());
eventService.saveEvent(deleteUserEvent);
}
}

View File

@ -2,13 +2,14 @@ package mops.gruppen2.service;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
//Hallo
@Service
public class UserService {
@ -22,10 +23,17 @@ public class UserService {
//Test nötig??
public List<Group> getUserGroups(String user_id) throws EventException {
List<Long> group_ids = eventRepository.findGroup_idsWhereUser_id(user_id);
public List<Group> getUserGroups(User user) throws EventException {
List<Long> group_ids = eventRepository.findGroup_idsWhereUser_id(user.getUser_id());
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events);
List<Group> groups = groupService.projectEventList(events);
List<Group> newGroups = new ArrayList<>();
for (Group group: groups) {
if(group.getMembers().contains(user)){
newGroups.add(group);
}
}
return newGroups;
}
public Group getGroupById(Long group_id) throws EventException {

View File

@ -28,20 +28,20 @@
<div class="container-fluid">
<div class="row">
<div class="shadow-sm p-4" style="border: 10px solid aliceblue; background: aliceblue">
<form action="/" method="get">
<h1 style="color: dodgerblue; font-weight: bold" th:text="${group.getTitle()}"></h1>
<p style="font-weight: bold">
<span class="badge badge-pill badge-dark" style="background: darkslategray" th:if="${group.getVisibility() == group.getVisibility().PRIVATE }">Private Gruppe</span>
<span class="badge badge-pill badge-primary" th:if="${group.getVisibility() == group.getVisibility().PUBLIC}">Öffentliche Gruppe</span>
<span class="badge badge-pill badge-success" style="background: lightseagreen" th:if="${group.getType() == group.getType().LECTURE}"> Veranstaltung</span>
</p>
<p th:text="${group.getDescription()}"></p>
<div class="form-group">
<div class="text-right">
<button class="btn btn-danger" type="danger" style="border-style: none;">Gruppe verlassen</button>
</div>
<h1 style="color: dodgerblue; font-weight: bold" th:text="${group.getTitle()}"></h1>
<p style="font-weight: bold">
<span class="badge badge-pill badge-dark" style="background: darkslategray" th:if="${group.getVisibility() == group.getVisibility().PRIVATE }">Private Gruppe</span>
<span class="badge badge-pill badge-primary" th:if="${group.getVisibility() == group.getVisibility().PUBLIC}">Öffentliche Gruppe</span>
<span class="badge badge-pill badge-success" style="background: lightseagreen" th:if="${group.getType() == group.getType().LECTURE}"> Veranstaltung</span>
</p>
<p th:text="${group.getDescription()}"></p>
<div class="form-group">
<div class="text-right">
<form method="post" action="/gruppen2/leaveGroup">
<button th:value="${group.getId()}" th:name="group_id" class="btn btn-danger" type="submit" style="border-style: none;">Gruppe verlassen</button>
</form>
</div>
</form>
</div>
</div>
<div class="col-3" style="white-space: nowrap">
@ -53,8 +53,8 @@
<div>
<ul th:each="member : ${group.getMembers()}" class="list-group-flush" style="background: slategrey">
<li class="list-group-item" style="background: aliceblue">
<span th:text="${member.getUser_id()}"></span>
<span th:if="${role == role.ADMIN}" class="badge badge-success">admin</span>
<span th:text="${member.getUser_id()}"></span>
<span th:if="${role == role.ADMIN}" class="badge badge-success">admin</span>
</li>
</ul>
</div>