From 7b80b52f9e5bed582e4c35f7ab1b37cc6b595f9c Mon Sep 17 00:00:00 2001 From: tommi27 Date: Sun, 19 Apr 2020 17:10:42 +0200 Subject: [PATCH] guest controller should be finished, must be reviewed --- .../smarthut/controller/GuestController.java | 40 +++++++++-- .../smarthut/dto/GuestUserResponse.java | 66 +++++++++++++++++++ .../sa4/sanmarinoes/smarthut/models/User.java | 10 +++ 3 files changed, 111 insertions(+), 5 deletions(-) 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 ab47121..a986fe9 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 @@ -22,18 +22,48 @@ public class GuestController { } @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 host = userRepository.findByUsername(principal.getName()); host.addGuest(guest); - userRepository.save(guest); - userRepository.save(host); + /** Not sure if this is useful. userRepository.save(guest); */ + 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 - 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 - 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); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestUserResponse.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestUserResponse.java index 1afec1e..19984a6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestUserResponse.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestUserResponse.java @@ -1,8 +1,74 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import java.util.Set; public class GuestUserResponse { private boolean cameraEnabled; + private long id; + private String name; + private String username; + private String password; + private String email; + private Set hosts; + private Set 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 getHosts() { + return hosts; + } + + public Set getGuests() { + return guests; + } public boolean isCameraEnabled() { return cameraEnabled; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java index 4f98f0f..5880ec6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -125,6 +125,16 @@ public class User { 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) { this.cameraEnabled = cameraEnabled; }