Code review
This commit is contained in:
parent
389af7c04d
commit
3894fa14a9
6 changed files with 95 additions and 41 deletions
|
@ -81,6 +81,7 @@ public class SpringFoxConfig {
|
||||||
.or(PathSelectors.regex("/switch.*")::apply)
|
.or(PathSelectors.regex("/switch.*")::apply)
|
||||||
.or(PathSelectors.regex("/motionSensor.*")::apply)
|
.or(PathSelectors.regex("/motionSensor.*")::apply)
|
||||||
.or(PathSelectors.regex("/curtains.*")::apply)
|
.or(PathSelectors.regex("/curtains.*")::apply)
|
||||||
|
.or(PathSelectors.regex("/user.*")::apply)
|
||||||
.or(PathSelectors.regex("/auth/profile.*")::apply);
|
.or(PathSelectors.regex("/auth/profile.*")::apply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,8 +24,10 @@ public class DeviceController {
|
||||||
@Autowired private RoomRepository roomRepository;
|
@Autowired private RoomRepository roomRepository;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Device> getAll(final Principal user) {
|
public List<Device> getAll(
|
||||||
return deviceRepository.findAllByUsername(user.getName());
|
@RequestParam(value = "hostId", required = false) Long hostId, final Principal user)
|
||||||
|
throws NotFoundException {
|
||||||
|
return deviceService.findAll(hostId, user.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
|
|
|
@ -10,8 +10,12 @@ import java.security.Principal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@RequestMapping("/user")
|
||||||
public class GuestController {
|
public class GuestController {
|
||||||
|
|
||||||
@Autowired private UserRepository userRepository;
|
@Autowired private UserRepository userRepository;
|
||||||
|
@ -21,31 +25,29 @@ public class GuestController {
|
||||||
return toList(userRepository.findAll());
|
return toList(userRepository.findAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping("/guest")
|
||||||
public User addUserAsGuest(long id, final Principal principal) throws NotFoundException {
|
public User addUserAsGuest(@RequestParam("userId") 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);
|
||||||
guest.addHost(host);
|
guest.addHost(host);
|
||||||
userRepository.save(guest);
|
userRepository.save(guest);
|
||||||
/* Not sure if this is useful. userRepository.save(guest); */
|
|
||||||
return userRepository.save(host);
|
return userRepository.save(host);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User save(GuestPermissionsRequest g, User currentUser) {
|
@PutMapping("/permissions")
|
||||||
|
public User updatePermissions(
|
||||||
|
@Valid @RequestBody GuestPermissionsRequest g, final Principal principal) {
|
||||||
|
final User currentUser = userRepository.findByUsername(principal.getName());
|
||||||
currentUser.setCameraEnabled(g.isCameraEnabled());
|
currentUser.setCameraEnabled(g.isCameraEnabled());
|
||||||
return userRepository.save(currentUser);
|
return userRepository.save(currentUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@DeleteMapping("/guest")
|
||||||
public User updatePermissions(
|
public void removeUserAsGuest(@RequestParam("userId") long id, final Principal principal)
|
||||||
@Valid @RequestBody GuestPermissionsRequest g, final Principal principal) {
|
throws NotFoundException {
|
||||||
return this.save(g, userRepository.findByUsername(principal.getName()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping
|
|
||||||
public void removeUserAsGuest(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());
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
|
||||||
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.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
|
@ -23,7 +24,7 @@ public class RoomController {
|
||||||
|
|
||||||
@Autowired private UserRepository userRepository;
|
@Autowired private UserRepository userRepository;
|
||||||
|
|
||||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
@Autowired private DeviceService deviceService;
|
||||||
|
|
||||||
@Autowired private SwitchRepository switchRepository;
|
@Autowired private SwitchRepository switchRepository;
|
||||||
|
|
||||||
|
@ -43,7 +44,10 @@ public class RoomController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Room> findAll(Long hostId, final Principal principal) throws NotFoundException {
|
public List<Room> findAll(
|
||||||
|
@RequestParam(value = "hostId", required = false) Long hostId,
|
||||||
|
final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
|
||||||
List<Room> rooms = toList(roomRepository.findAll());
|
List<Room> rooms = toList(roomRepository.findAll());
|
||||||
return fetchOwnerOrGuest(rooms, hostId, principal);
|
return fetchOwnerOrGuest(rooms, hostId, principal);
|
||||||
|
@ -51,7 +55,9 @@ public class RoomController {
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public @ResponseBody Room findById(
|
public @ResponseBody Room findById(
|
||||||
@PathVariable("id") long id, final Principal principal, Long hostId)
|
@PathVariable("id") long id,
|
||||||
|
final Principal principal,
|
||||||
|
@RequestParam(value = "hostId", required = false) Long hostId)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
Room room = roomRepository.findById(id).orElseThrow(NotFoundException::new);
|
Room room = roomRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||||
/* Very ugly way of avoiding code duplication. If this method call throws no exception,
|
/* Very ugly way of avoiding code duplication. If this method call throws no exception,
|
||||||
|
@ -122,15 +128,10 @@ public class RoomController {
|
||||||
*/
|
*/
|
||||||
@GetMapping(path = "/{roomId}/devices")
|
@GetMapping(path = "/{roomId}/devices")
|
||||||
public List<Device> getDevices(
|
public List<Device> getDevices(
|
||||||
@PathVariable("roomId") long roomid, final Principal principal, Long hostId)
|
@PathVariable("roomId") long roomId,
|
||||||
|
final Principal principal,
|
||||||
|
@RequestParam(value = "hostId", required = false) Long hostId)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
Iterable<Device> devices = deviceRepository.findByRoomId(roomid);
|
return deviceService.findAll(roomId, hostId, principal.getName());
|
||||||
for (Device d : devices) {
|
|
||||||
if (d instanceof Thermostat) {
|
|
||||||
thermostatService.populateMeasuredTemperature((Thermostat) d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
List<Device> deviceList = toList(devices);
|
|
||||||
return fetchOwnerOrGuest(deviceList, hostId, principal);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,11 +38,7 @@ public class User {
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
/** Guests invited by this user */
|
/** Guests invited by this user */
|
||||||
@ManyToMany(cascade = CascadeType.DETACH)
|
@ManyToMany(mappedBy = "hosts", cascade = CascadeType.DETACH)
|
||||||
@JoinTable(
|
|
||||||
name = "invited",
|
|
||||||
joinColumns = @JoinColumn(name = "host_id"),
|
|
||||||
inverseJoinColumns = @JoinColumn(name = "guest_id"))
|
|
||||||
@GsonExclude
|
@GsonExclude
|
||||||
private Set<User> guests = new HashSet<>();
|
private Set<User> guests = new HashSet<>();
|
||||||
|
|
||||||
|
@ -55,7 +51,8 @@ public class User {
|
||||||
private Set<User> hosts = new HashSet<>();
|
private Set<User> hosts = new HashSet<>();
|
||||||
|
|
||||||
/** Determines whether a guest can access security cameras */
|
/** Determines whether a guest can access security cameras */
|
||||||
@Column private boolean cameraEnabled;
|
@Column(nullable = false)
|
||||||
|
private boolean cameraEnabled;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@GsonExclude
|
@GsonExclude
|
||||||
|
@ -130,9 +127,7 @@ public class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeGuest(User guest) {
|
public void removeGuest(User guest) {
|
||||||
if (this.guests.contains(guest)) {
|
this.guests.remove(guest);
|
||||||
this.guests.remove(guest);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCameraEnabled(boolean cameraEnabled) {
|
public void setCameraEnabled(boolean cameraEnabled) {
|
||||||
|
@ -141,8 +136,7 @@ public class User {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "User{"
|
return "User{id="
|
||||||
+ "id="
|
|
||||||
+ id
|
+ id
|
||||||
+ ", name='"
|
+ ", name='"
|
||||||
+ name
|
+ name
|
||||||
|
@ -156,6 +150,8 @@ public class User {
|
||||||
+ ", email='"
|
+ ", email='"
|
||||||
+ email
|
+ email
|
||||||
+ '\''
|
+ '\''
|
||||||
|
+ ", cameraEnabled="
|
||||||
|
+ cameraEnabled
|
||||||
+ ", isEnabled="
|
+ ", isEnabled="
|
||||||
+ isEnabled
|
+ isEnabled
|
||||||
+ '}';
|
+ '}';
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||||
|
|
||||||
|
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||||
|
|
||||||
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.Device;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
|
||||||
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.socket.SensorSocketEndpoint;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
@ -15,8 +15,60 @@ public class DeviceService {
|
||||||
// FIXME: TO BE MERGED WITH USER STORY 5 (MATTEO'S STUFF)
|
// FIXME: TO BE MERGED WITH USER STORY 5 (MATTEO'S STUFF)
|
||||||
|
|
||||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||||
|
@Autowired private RoomRepository roomRepository;
|
||||||
@Autowired private UserRepository userRepository;
|
@Autowired private UserRepository userRepository;
|
||||||
@Autowired private SensorSocketEndpoint endpoint;
|
@Autowired private SensorSocketEndpoint endpoint;
|
||||||
|
@Autowired private ThermostatService thermostatService;
|
||||||
|
|
||||||
|
public List<Device> findAll(Long hostId, String username) throws NotFoundException {
|
||||||
|
return findAll(null, hostId, username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Device> findAll(Long roomId, Long hostId, String username)
|
||||||
|
throws NotFoundException {
|
||||||
|
try {
|
||||||
|
Iterable<Device> devices;
|
||||||
|
if (hostId == null) {
|
||||||
|
if (roomId != null) {
|
||||||
|
roomRepository
|
||||||
|
.findByIdAndUsername(roomId, username)
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
|
devices = deviceRepository.findByRoomId(roomId);
|
||||||
|
} else {
|
||||||
|
devices = deviceRepository.findAllByUsername(username);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
final User guest = userRepository.findByUsername(username);
|
||||||
|
final User host =
|
||||||
|
userRepository.findById(hostId).orElseThrow(NotFoundException::new);
|
||||||
|
|
||||||
|
if (!guest.getHosts().contains(host)) {
|
||||||
|
throw new NotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (roomId != null) {
|
||||||
|
Room r = roomRepository.findById(roomId).orElseThrow(NotFoundException::new);
|
||||||
|
if (!r.getUserId().equals(hostId)) {
|
||||||
|
throw new NotFoundException();
|
||||||
|
}
|
||||||
|
devices = deviceRepository.findByRoomId(roomId);
|
||||||
|
} else {
|
||||||
|
devices = deviceRepository.findAllByUsername(host.getUsername());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Device d : devices) {
|
||||||
|
if (d instanceof Thermostat) {
|
||||||
|
thermostatService.populateMeasuredTemperature((Thermostat) d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toList(devices);
|
||||||
|
} catch (NotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
|
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
|
Loading…
Reference in a new issue