guest controller should be finished, must be reviewed

This commit is contained in:
Tommaso Rodolfo Masera 2020-04-19 17:10:42 +02:00
parent 36a7a649d5
commit 7b80b52f9e
3 changed files with 111 additions and 5 deletions

View File

@ -22,18 +22,48 @@ public class GuestController {
} }
@PostMapping @PostMapping
public void addUserAsGuest(long id, final Principal principal) throws NotFoundException { public User addUserAsGuest(long id, final Principal principal) 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());
host.addGuest(guest); host.addGuest(guest);
userRepository.save(guest); /** Not sure if this is useful. userRepository.save(guest); */
userRepository.save(host); return userRepository.save(host);
}
public User save(GuestUserResponse g, User newGuest) {
newGuest.setCameraEnabled(g.isCameraEnabled());
newGuest.setEmail(g.getEmail());
newGuest.setId(g.getId());
newGuest.setName(g.getName());
newGuest.setUsername(g.getUsername());
newGuest.setPassword(g.getPassword());
newGuest.setEnabled(g.getEnabled());
for (User guest : g.getGuests()) {
newGuest.addGuest(guest);
}
for (User host : g.getHosts()) {
newGuest.addHost(host);
}
return userRepository.save(newGuest);
} }
@PutMapping @PutMapping
public void updatePermissions(@Valid @RequestBody GuestUserResponse g, Principal principal) {} public User updatePermissions(@Valid @RequestBody GuestUserResponse g)
throws NotFoundException {
return this.save(g, userRepository.findById(g.getId()).orElseThrow(NotFoundException::new));
}
@DeleteMapping @DeleteMapping
public void removeUserAsGuest(long id) {} public void removeUserAsGuest(long id, final Principal principal) throws NotFoundException {
User guest = userRepository.findById(id).orElseThrow(NotFoundException::new);
User host = userRepository.findByUsername(principal.getName());
host.removeGuest(guest);
userRepository.deleteById(id);
userRepository.save(host);
}
} }

View File

@ -1,8 +1,74 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import java.util.Set;
public class GuestUserResponse { public class GuestUserResponse {
private boolean cameraEnabled; private boolean cameraEnabled;
private long id;
private String name;
private String username;
private String password;
private String email;
private Set<User> hosts;
private Set<User> guests;
private Boolean isEnabled = false;
public Boolean getEnabled() {
return isEnabled;
}
public void setEnabled(Boolean enabled) {
isEnabled = enabled;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Set<User> getHosts() {
return hosts;
}
public Set<User> getGuests() {
return guests;
}
public boolean isCameraEnabled() { public boolean isCameraEnabled() {
return cameraEnabled; return cameraEnabled;

View File

@ -125,6 +125,16 @@ public class User {
this.guests.add(guest); this.guests.add(guest);
} }
public void addHost(User host) {
this.hosts.add(host);
}
public void removeGuest(User guest) {
if (this.guests.contains(guest)) {
this.guests.remove(guest);
}
}
public void setCameraEnabled(boolean cameraEnabled) { public void setCameraEnabled(boolean cameraEnabled) {
this.cameraEnabled = cameraEnabled; this.cameraEnabled = cameraEnabled;
} }