From 6317ac99e43abfa0ef59416a6266a9aec08b592d Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Tue, 21 Apr 2020 14:25:42 +0200 Subject: [PATCH] Code review --- .../smarthut/controller/GuestController.java | 36 +++------ .../smarthut/controller/SceneController.java | 2 + .../smarthut/dto/GuestPermissionsRequest.java | 14 ++++ .../smarthut/dto/GuestUserResponse.java | 80 ------------------- 4 files changed, 28 insertions(+), 104 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestPermissionsRequest.java delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestUserResponse.java 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 a986fe9..e0fc3e9 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 @@ -2,7 +2,7 @@ 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.GuestUserResponse; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GuestPermissionsRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; @@ -27,34 +27,21 @@ public class GuestController { User host = userRepository.findByUsername(principal.getName()); host.addGuest(guest); - /** Not sure if this is useful. userRepository.save(guest); */ + guest.addHost(host); + userRepository.save(guest); + /* 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); + public User save(GuestPermissionsRequest g, User currentUser) { + currentUser.setCameraEnabled(g.isCameraEnabled()); + return userRepository.save(currentUser); } @PutMapping - public User updatePermissions(@Valid @RequestBody GuestUserResponse g) - throws NotFoundException { - return this.save(g, userRepository.findById(g.getId()).orElseThrow(NotFoundException::new)); + public User updatePermissions( + @Valid @RequestBody GuestPermissionsRequest g, final Principal principal) { + return this.save(g, userRepository.findByUsername(principal.getName())); } @DeleteMapping @@ -63,7 +50,8 @@ public class GuestController { User host = userRepository.findByUsername(principal.getName()); host.removeGuest(guest); - userRepository.deleteById(id); + guest.getHosts().remove(host); userRepository.save(host); + userRepository.save(guest); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java index a4da945..f5dd1a2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java @@ -56,6 +56,8 @@ public class SceneController { newScene.setUserId(userId); newScene.setName(s.getName()); + newScene.setGuestAccessEnabled(s.isGuestAccessEnabled()); + return sceneService.save(newScene); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestPermissionsRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestPermissionsRequest.java new file mode 100644 index 0000000..781a10e --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestPermissionsRequest.java @@ -0,0 +1,14 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + + +public class GuestPermissionsRequest { + private boolean cameraEnabled; + + public boolean isCameraEnabled() { + return cameraEnabled; + } + + public void setCameraEnabled(boolean cameraEnabled) { + this.cameraEnabled = cameraEnabled; + } +} 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 deleted file mode 100644 index 19984a6..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GuestUserResponse.java +++ /dev/null @@ -1,80 +0,0 @@ -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; - } - - public void setCameraEnabled(boolean cameraEnabled) { - this.cameraEnabled = cameraEnabled; - } -}