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 cfc0f36..8e2274f 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,15 @@ 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.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.stream.Collectors; +import java.util.stream.StreamSupport; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -21,14 +23,16 @@ public class GuestController { @Autowired private EagerUserRepository userRepository; @GetMapping - public List findAll() { - return toList(userRepository.findAll()); + public List findAll() { + return StreamSupport.stream(userRepository.findAll().spliterator(), false) + .map(UserResponse::fromUser) + .collect(Collectors.toList()); } @GetMapping("/hosts") - public List findHosts(final Principal principal) { + public List findHosts(final Principal principal) { final User u = userRepository.findByUsername(principal.getName()); - return toList(u.getHosts()); + return u.getHosts().stream().map(UserResponse::fromUser).collect(Collectors.toList()); } @PostMapping("/guest") @@ -52,7 +56,7 @@ public class GuestController { } @DeleteMapping("/guest") - public void removeUserAsGuest(@RequestParam("userId") long id, final Principal principal) + 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()); @@ -61,5 +65,6 @@ public class GuestController { 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/UserResponse.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/UserResponse.java new file mode 100644 index 0000000..2ed2c09 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/UserResponse.java @@ -0,0 +1,19 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; + +public class UserResponse { + private Long id; + private String username; + private String name; + + private UserResponse() {} + + public static UserResponse fromUser(User u) { + final UserResponse us = new UserResponse(); + us.name = u.getName(); + us.id = u.getId(); + us.username = u.getUsername(); + return us; + } +}