Fixed for frontend to guest controllers
This commit is contained in:
parent
6263bd2d35
commit
25eb9555c0
2 changed files with 35 additions and 20 deletions
|
@ -1,13 +1,16 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
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.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.dto.UserResponse;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
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.EagerUserRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
@ -35,16 +38,33 @@ public class GuestController {
|
||||||
return u.getHosts().stream().map(UserResponse::fromUser).collect(Collectors.toList());
|
return u.getHosts().stream().map(UserResponse::fromUser).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/guest")
|
@GetMapping("/guests")
|
||||||
public User addUserAsGuest(@RequestParam("userId") long id, final Principal principal)
|
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 {
|
throws NotFoundException {
|
||||||
User guest = userRepository.findById(id).orElseThrow(NotFoundException::new);
|
Iterable<User> guests = userRepository.findAllById(g.ids);
|
||||||
User host = userRepository.findByUsername(principal.getName());
|
User host = userRepository.findByUsername(principal.getName());
|
||||||
|
|
||||||
host.addGuest(guest);
|
for (final User oldGuest : host.getGuests()) {
|
||||||
guest.addHost(host);
|
oldGuest.getHosts().remove(host);
|
||||||
userRepository.save(guest);
|
}
|
||||||
return userRepository.save(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")
|
@PutMapping("/permissions")
|
||||||
|
@ -54,17 +74,4 @@ public class GuestController {
|
||||||
currentUser.setCameraEnabled(g.isCameraEnabled());
|
currentUser.setCameraEnabled(g.isCameraEnabled());
|
||||||
return userRepository.save(currentUser);
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in a new issue