Code review

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-21 14:25:42 +02:00
parent cfe3848d7e
commit 6317ac99e4
4 changed files with 28 additions and 104 deletions

View file

@ -2,7 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; 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.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
@ -27,34 +27,21 @@ public class GuestController {
User host = userRepository.findByUsername(principal.getName()); User host = userRepository.findByUsername(principal.getName());
host.addGuest(guest); 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); return userRepository.save(host);
} }
public User save(GuestUserResponse g, User newGuest) { public User save(GuestPermissionsRequest g, User currentUser) {
newGuest.setCameraEnabled(g.isCameraEnabled()); currentUser.setCameraEnabled(g.isCameraEnabled());
newGuest.setEmail(g.getEmail()); return userRepository.save(currentUser);
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 User updatePermissions(@Valid @RequestBody GuestUserResponse g) public User updatePermissions(
throws NotFoundException { @Valid @RequestBody GuestPermissionsRequest g, final Principal principal) {
return this.save(g, userRepository.findById(g.getId()).orElseThrow(NotFoundException::new)); return this.save(g, userRepository.findByUsername(principal.getName()));
} }
@DeleteMapping @DeleteMapping
@ -63,7 +50,8 @@ public class GuestController {
User host = userRepository.findByUsername(principal.getName()); User host = userRepository.findByUsername(principal.getName());
host.removeGuest(guest); host.removeGuest(guest);
userRepository.deleteById(id); guest.getHosts().remove(host);
userRepository.save(host); userRepository.save(host);
userRepository.save(guest);
} }
} }

View file

@ -56,6 +56,8 @@ public class SceneController {
newScene.setUserId(userId); newScene.setUserId(userId);
newScene.setName(s.getName()); newScene.setName(s.getName());
newScene.setGuestAccessEnabled(s.isGuestAccessEnabled());
return sceneService.save(newScene); return sceneService.save(newScene);
} }

View file

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

View file

@ -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<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() {
return cameraEnabled;
}
public void setCameraEnabled(boolean cameraEnabled) {
this.cameraEnabled = cameraEnabled;
}
}