Fixed for frontend to guest controllers

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-02 22:37:54 +02:00
parent 6263bd2d35
commit 25eb9555c0
2 changed files with 35 additions and 20 deletions

View file

@ -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<UserResponse> 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<User> setGuests(
@RequestBody @Valid GuestsUpdateRequest g, final Principal principal)
throws NotFoundException {
User guest = userRepository.findById(id).orElseThrow(NotFoundException::new);
Iterable<User> 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<User> 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;
}
}

View file

@ -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<Long> ids;
}