refactored controllers code
This commit is contained in:
parent
eef0887da1
commit
c4f295d7d9
2 changed files with 63 additions and 82 deletions
|
@ -6,9 +6,9 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException;
|
||||||
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 java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
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.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
@ -28,10 +28,30 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
public class RegularLightController {
|
public class RegularLightController {
|
||||||
|
|
||||||
@Autowired private UserRepository userRepository;
|
@Autowired private UserRepository userRepository;
|
||||||
@Autowired private RoomRepository roomRepository;
|
|
||||||
@Autowired private RegularLightRepository regularLightService;
|
@Autowired private RegularLightRepository regularLightService;
|
||||||
@Autowired private SceneRepository sceneRepository;
|
@Autowired private SceneRepository sceneRepository;
|
||||||
@Autowired private StateRepository<State<?>> stateRepository;
|
@Autowired private StateRepository<State<?>> stateRepository;
|
||||||
|
@Autowired private DeviceService deviceService;
|
||||||
|
|
||||||
|
private RegularLight fetchIfOwnerOrGuest(final Principal principal, Long id, Long hostId)
|
||||||
|
throws NotFoundException {
|
||||||
|
if (hostId == null) {
|
||||||
|
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
||||||
|
} else {
|
||||||
|
RegularLight rl =
|
||||||
|
regularLightService
|
||||||
|
.findByIdAndUserId(id, hostId)
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
|
User host = userRepository.findById(hostId).orElseThrow(IllegalStateException::new);
|
||||||
|
User guest = userRepository.findByUsername(principal.getName());
|
||||||
|
rl.setFromHost(true);
|
||||||
|
if (!host.getGuests().contains(guest)) {
|
||||||
|
throw new NotFoundException();
|
||||||
|
} else {
|
||||||
|
return rl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<RegularLight> findAll() {
|
public List<RegularLight> findAll() {
|
||||||
|
@ -43,46 +63,25 @@ public class RegularLightController {
|
||||||
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private RegularLight save(RegularLight newRL, SwitchableSaveRequest rl) {
|
private RegularLight save(RegularLight initial, SwitchableSaveRequest rl, String username) {
|
||||||
newRL.setName(rl.getName());
|
initial.setName(rl.getName());
|
||||||
newRL.setRoomId(rl.getRoomId());
|
initial.setRoomId(rl.getRoomId());
|
||||||
newRL.setOn(rl.isOn());
|
initial.setOn(rl.isOn());
|
||||||
|
|
||||||
return regularLightService.save(newRL);
|
return deviceService.saveAsOwner(initial, username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public RegularLight create(@Valid @RequestBody SwitchableSaveRequest rl) {
|
public RegularLight create(
|
||||||
return save(new RegularLight(), rl);
|
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal) {
|
||||||
|
return save(new RegularLight(), rl, principal.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public RegularLight update(
|
public RegularLight update(
|
||||||
@Valid @RequestBody SwitchableSaveRequest rl,
|
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal, Long hostId)
|
||||||
final Principal principal,
|
|
||||||
Optional<Long> guestId)
|
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
return save(fetchIfOwnerOrGuest(principal, rl.getId(), hostId), rl, principal.getName());
|
||||||
/** Extremely verbose check for guest/user authorization */
|
|
||||||
if (guestId.isPresent()
|
|
||||||
&& userRepository
|
|
||||||
.findById(
|
|
||||||
roomRepository
|
|
||||||
.findById(rl.getRoomId())
|
|
||||||
.get()
|
|
||||||
.getUserId()
|
|
||||||
.longValue())
|
|
||||||
.get()
|
|
||||||
.getGuests()
|
|
||||||
.contains(userRepository.findById(guestId.get().longValue()))) {
|
|
||||||
return save(
|
|
||||||
regularLightService
|
|
||||||
.findByIdAndUsername(rl.getId(), principal.getName())
|
|
||||||
.orElseThrow(NotFoundException::new),
|
|
||||||
rl);
|
|
||||||
} else {
|
|
||||||
throw new Error("401: Unauthorized user. Not a guest.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -32,39 +32,39 @@ public class RoomController {
|
||||||
|
|
||||||
@Autowired private ThermostatService thermostatService;
|
@Autowired private ThermostatService thermostatService;
|
||||||
|
|
||||||
|
private <T> List<T> fetchOwnerOrGuest(
|
||||||
|
final List<T> list, Long hostId, final Principal principal) throws NotFoundException {
|
||||||
|
if (hostId == null) {
|
||||||
|
return list;
|
||||||
|
} else {
|
||||||
|
User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new);
|
||||||
|
User guest = userRepository.findByUsername(principal.getName());
|
||||||
|
if (!host.getGuests().contains(guest)) {
|
||||||
|
throw new NotFoundException();
|
||||||
|
} else {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Room> findAll(Optional<Long> guestId) {
|
public List<Room> findAll(Long hostId, final Principal principal) throws NotFoundException {
|
||||||
|
|
||||||
List<Room> rooms = toList(roomRepository.findAll());
|
List<Room> rooms = toList(roomRepository.findAll());
|
||||||
|
return fetchOwnerOrGuest(rooms, hostId, principal);
|
||||||
if (guestId.isPresent()
|
|
||||||
&& !rooms.isEmpty()
|
|
||||||
&& userRepository
|
|
||||||
.findById(rooms.get(0).getUserId())
|
|
||||||
.get()
|
|
||||||
.getGuests()
|
|
||||||
.contains(userRepository.findById(guestId.get().longValue()))) {
|
|
||||||
return rooms;
|
|
||||||
} else {
|
|
||||||
throw new Error("401: Unauthorized user. Not a guest.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public @ResponseBody Room findById(@PathVariable("id") long id, Optional<Long> guestId)
|
public @ResponseBody Room findById(
|
||||||
|
@PathVariable("id") long id, final Principal principal, 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,
|
||||||
if (guestId.isPresent()
|
* we can return the room safely. I pass null as I do not return a list in this case.
|
||||||
&& userRepository
|
* Refer to fetchOwnerOrGuest for further information.
|
||||||
.findById(room.getUserId().longValue())
|
*/
|
||||||
.get()
|
fetchOwnerOrGuest(null, hostId, principal);
|
||||||
.getGuests()
|
|
||||||
.contains(userRepository.findById(guestId.get()))) {
|
|
||||||
return room;
|
return room;
|
||||||
} else {
|
|
||||||
throw new Error("401: Unauthorized user. Not a guest.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
|
@ -126,34 +126,16 @@ public class RoomController {
|
||||||
* id).
|
* id).
|
||||||
*/
|
*/
|
||||||
@GetMapping(path = "/{roomId}/devices")
|
@GetMapping(path = "/{roomId}/devices")
|
||||||
public List<Device> getDevices(@PathVariable("roomId") long roomid, Optional<Long> guestId) {
|
public List<Device> getDevices(
|
||||||
|
@PathVariable("roomId") long roomid, final Principal principal, Long hostId)
|
||||||
|
throws NotFoundException {
|
||||||
Iterable<Device> devices = deviceRepository.findByRoomId(roomid);
|
Iterable<Device> devices = deviceRepository.findByRoomId(roomid);
|
||||||
for (Device d : devices) {
|
for (Device d : devices) {
|
||||||
if (d instanceof Thermostat) {
|
if (d instanceof Thermostat) {
|
||||||
thermostatService.populateMeasuredTemperature((Thermostat) d);
|
thermostatService.populateMeasuredTemperature((Thermostat) d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
List<Device> dl = toList(devices);
|
List<Device> deviceList = toList(devices);
|
||||||
|
return fetchOwnerOrGuest(deviceList, hostId, principal);
|
||||||
/**
|
|
||||||
* Extremely verbose method calls to find the current user and check if the optional user is
|
|
||||||
* one of their guests
|
|
||||||
*/
|
|
||||||
if (guestId.isPresent()
|
|
||||||
&& !dl.isEmpty()
|
|
||||||
&& userRepository
|
|
||||||
.findById(
|
|
||||||
roomRepository
|
|
||||||
.findById(dl.get(0).getRoomId().longValue())
|
|
||||||
.get()
|
|
||||||
.getUserId()
|
|
||||||
.longValue())
|
|
||||||
.get()
|
|
||||||
.getGuests()
|
|
||||||
.contains(userRepository.findById(guestId.get().longValue()))) {
|
|
||||||
return dl;
|
|
||||||
} else {
|
|
||||||
throw new Error("401: Unauthorized user. Not a guest.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue