From 25eb9555c0b92204c5bd0b8685bec925226e16c8 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 2 May 2020 22:37:54 +0200 Subject: [PATCH] Fixed for frontend to guest controllers --- .../smarthut/controller/GuestController.java | 47 +++++++++++-------- .../smarthut/dto/GuestsUpdateRequest.java | 8 ++++ 2 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestsUpdateRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java index 8e2274f..f010b52 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestController.java @@ -1,13 +1,16 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestPermissionsRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestsUpdateRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.EagerUserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import java.security.Principal; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; import javax.validation.Valid; @@ -35,16 +38,33 @@ public class GuestController { return u.getHosts().stream().map(UserResponse::fromUser).collect(Collectors.toList()); } - @PostMapping("/guest") - public User addUserAsGuest(@RequestParam("userId") long id, final Principal principal) + @GetMapping("/guests") + public List findGuests(final Principal principal) { + final User u = userRepository.findByUsername(principal.getName()); + return u.getGuests().stream().map(UserResponse::fromUser).collect(Collectors.toList()); + } + + @PutMapping("/guests") + public List setGuests( + @RequestBody @Valid GuestsUpdateRequest g, final Principal principal) throws NotFoundException { - User guest = userRepository.findById(id).orElseThrow(NotFoundException::new); + Iterable guests = userRepository.findAllById(g.ids); User host = userRepository.findByUsername(principal.getName()); - host.addGuest(guest); - guest.addHost(host); - userRepository.save(guest); - return userRepository.save(host); + for (final User oldGuest : host.getGuests()) { + oldGuest.getHosts().remove(host); + } + + final Set oldGuests = Set.copyOf(host.getGuests()); + + for (final User guest : guests) { + host.addGuest(guest); + guest.addHost(host); + } + + userRepository.saveAll(oldGuests); + userRepository.save(host); + return toList(userRepository.saveAll(guests)); } @PutMapping("/permissions") @@ -54,17 +74,4 @@ public class GuestController { currentUser.setCameraEnabled(g.isCameraEnabled()); return userRepository.save(currentUser); } - - @DeleteMapping("/guest") - public User removeUserAsGuest(@RequestParam("userId") long id, final Principal principal) - throws NotFoundException { - User guest = userRepository.findById(id).orElseThrow(NotFoundException::new); - User host = userRepository.findByUsername(principal.getName()); - - host.removeGuest(guest); - guest.getHosts().remove(host); - userRepository.save(host); - userRepository.save(guest); - return host; - } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestsUpdateRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestsUpdateRequest.java new file mode 100644 index 0000000..6e98937 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestsUpdateRequest.java @@ -0,0 +1,8 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import java.util.List; +import javax.validation.constraints.NotNull; + +public class GuestsUpdateRequest { + @NotNull public List ids; +}