WIP
This commit is contained in:
parent
92e93c80e8
commit
6263bd2d35
2 changed files with 30 additions and 6 deletions
|
@ -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<User> findAll() {
|
||||
return toList(userRepository.findAll());
|
||||
public List<UserResponse> findAll() {
|
||||
return StreamSupport.stream(userRepository.findAll().spliterator(), false)
|
||||
.map(UserResponse::fromUser)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@GetMapping("/hosts")
|
||||
public List<User> findHosts(final Principal principal) {
|
||||
public List<UserResponse> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue