This commit is contained in:
Claudio Maggioni 2020-05-02 20:50:50 +02:00
parent 92e93c80e8
commit 6263bd2d35
2 changed files with 30 additions and 6 deletions

View file

@ -1,13 +1,15 @@
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.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.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -21,14 +23,16 @@ public class GuestController {
@Autowired private EagerUserRepository userRepository; @Autowired private EagerUserRepository userRepository;
@GetMapping @GetMapping
public List<User> findAll() { public List<UserResponse> findAll() {
return toList(userRepository.findAll()); return StreamSupport.stream(userRepository.findAll().spliterator(), false)
.map(UserResponse::fromUser)
.collect(Collectors.toList());
} }
@GetMapping("/hosts") @GetMapping("/hosts")
public List<User> findHosts(final Principal principal) { public List<UserResponse> findHosts(final Principal principal) {
final User u = userRepository.findByUsername(principal.getName()); final User u = userRepository.findByUsername(principal.getName());
return toList(u.getHosts()); return u.getHosts().stream().map(UserResponse::fromUser).collect(Collectors.toList());
} }
@PostMapping("/guest") @PostMapping("/guest")
@ -52,7 +56,7 @@ public class GuestController {
} }
@DeleteMapping("/guest") @DeleteMapping("/guest")
public void removeUserAsGuest(@RequestParam("userId") long id, final Principal principal) public User removeUserAsGuest(@RequestParam("userId") long id, final Principal principal)
throws NotFoundException { throws NotFoundException {
User guest = userRepository.findById(id).orElseThrow(NotFoundException::new); User guest = userRepository.findById(id).orElseThrow(NotFoundException::new);
User host = userRepository.findByUsername(principal.getName()); User host = userRepository.findByUsername(principal.getName());
@ -61,5 +65,6 @@ public class GuestController {
guest.getHosts().remove(host); guest.getHosts().remove(host);
userRepository.save(host); userRepository.save(host);
userRepository.save(guest); userRepository.save(guest);
return host;
} }
} }

View file

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