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 {