From 389af7c04d675a6eac0a9355dc31de1f6f978145 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Wed, 22 Apr 2020 18:13:06 +0200 Subject: [PATCH] all device controllers have been updated with device service --- .../controller/ButtonDimmerController.java | 35 ++++++++----------- .../controller/CurtainsController.java | 34 ++++++++---------- .../smarthut/controller/DeviceController.java | 4 ++- .../controller/DimmableLightController.java | 17 ++------- .../controller/KnobDimmerController.java | 32 ++++++----------- .../controller/MotionSensorController.java | 28 ++++++--------- .../controller/RegularLightController.java | 5 +-- .../controller/SecurityCameraController.java | 33 +++++++---------- .../smarthut/controller/SensorController.java | 33 ++++++----------- .../controller/SmartPlugController.java | 31 +++++++--------- .../smarthut/controller/SwitchController.java | 30 +++++++--------- .../controller/ThermostatController.java | 35 +++++++------------ .../sanmarinoes/smarthut/models/Device.java | 14 ++++++++ .../smarthut/service/DeviceService.java | 24 +++++++++++-- 14 files changed, 152 insertions(+), 203 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 1870abc..01f453c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -1,13 +1,12 @@ 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.ButtonDimmerDimRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; -import java.util.List; import java.util.Set; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -19,37 +18,30 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/buttonDimmer") public class ButtonDimmerController extends InputDeviceConnectionController { + + private DeviceService deviceService; private ButtonDimmerRepository buttonDimmerRepository; private DimmableRepository dimmableRepository; @Autowired protected ButtonDimmerController( - ButtonDimmerRepository inputRepository, DimmableRepository outputRepository) { - super( - inputRepository, - outputRepository, - DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR); + ButtonDimmerRepository inputRepository, + DimmableRepository outputRepository, + DeviceService deviceService) { + super(inputRepository, outputRepository, DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR); + this.deviceService = deviceService; this.buttonDimmerRepository = inputRepository; this.dimmableRepository = outputRepository; } - @GetMapping - public List findAll() { - return toList(buttonDimmerRepository.findAll()); - } - - @GetMapping("/{id}") - public ButtonDimmer findById(@PathVariable("id") long id) throws NotFoundException { - return buttonDimmerRepository.findById(id).orElseThrow(NotFoundException::new); - } - @PostMapping - public ButtonDimmer create(@Valid @RequestBody final GenericDeviceSaveReguest bd) { + public ButtonDimmer create( + @Valid @RequestBody final GenericDeviceSaveReguest bd, final Principal principal) { ButtonDimmer newBD = new ButtonDimmer(); newBD.setName(bd.getName()); newBD.setRoomId(bd.getRoomId()); - return buttonDimmerRepository.save(newBD); + return deviceService.saveAsOwner(newBD, principal.getName()); } @PutMapping("/dim") @@ -76,7 +68,8 @@ public class ButtonDimmerController } @DeleteMapping("/{id}") - public void delete(@PathVariable("id") long id) { - buttonDimmerRepository.deleteById(id); + public void delete(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java index 5eb00be..3e1872a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java @@ -1,13 +1,12 @@ 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.DimmableSaveRequest; 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.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; -import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -17,44 +16,39 @@ import org.springframework.web.bind.annotation.*; @EnableAutoConfiguration @RequestMapping("/curtains") public class CurtainsController { + @Autowired private DeviceService deviceService; @Autowired private CurtainsRepository curtainsService; @Autowired private SceneRepository sceneRepository; @Autowired private StateRepository> stateRepository; - @GetMapping - public List findAll() { - return toList(curtainsService.findAll()); - } - - @GetMapping("/{id}") - public Curtains findById(@PathVariable("id") long id) throws NotFoundException { - return curtainsService.findById(id).orElseThrow(NotFoundException::new); - } - - private Curtains save(Curtains newRL, DimmableSaveRequest s) { + private Curtains save(Curtains newRL, DimmableSaveRequest s, final Principal principal) { newRL.setName(s.getName()); newRL.setRoomId(s.getRoomId()); newRL.setIntensity(s.getIntensity()); - return curtainsService.save(newRL); + return deviceService.saveAsOwner(newRL, principal.getName()); } @PostMapping - public Curtains create(@Valid @RequestBody DimmableSaveRequest curtain) { - return save(new Curtains(), curtain); + public Curtains create( + @Valid @RequestBody DimmableSaveRequest curtain, final Principal principal) { + return save(new Curtains(), curtain, principal); } @PutMapping - public Curtains update(@Valid @RequestBody DimmableSaveRequest curtain) + public Curtains update( + @Valid @RequestBody DimmableSaveRequest curtain, final Principal principal) throws NotFoundException { return save( curtainsService.findById(curtain.getId()).orElseThrow(NotFoundException::new), - curtain); + curtain, + principal); } @DeleteMapping("/{id}") - public void delete(@PathVariable("id") long id) { - curtainsService.deleteById(id); + public void delete(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java index 17bdec7..ac1acd4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java @@ -6,6 +6,7 @@ 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.DeviceRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; import java.util.List; import javax.validation.Valid; @@ -18,6 +19,7 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/device") public class DeviceController { + @Autowired private DeviceService deviceService; @Autowired private DeviceRepository deviceRepository; @Autowired private RoomRepository roomRepository; @@ -43,7 +45,7 @@ public class DeviceController { d.setRoomId(deviceSaveRequest.getRoomId()); d.setName(deviceSaveRequest.getName()); - deviceRepository.save(d); + deviceService.saveAsOwner(d, principal.getName()); return d; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 3a714e8..3c80b39 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -1,6 +1,5 @@ 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.DimmableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException; @@ -8,7 +7,6 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; -import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -38,16 +36,6 @@ public class DimmableLightController extends GuestEnabledController findAll() { - return toList(dimmableLightRepository.findAll()); - } - - @GetMapping("/{id}") - public DimmableLight findById(@PathVariable("id") long id) throws NotFoundException { - return dimmableLightRepository.findById(id).orElseThrow(NotFoundException::new); - } - private DimmableLight save( DimmableLight initial, DimmableSaveRequest dl, String username, Long hostId) throws NotFoundException { @@ -89,8 +77,9 @@ public class DimmableLightController extends GuestEnabledController { +public class KnobDimmerController extends InputDeviceConnectionController { + @Autowired private DeviceService deviceService; @Autowired private KnobDimmerRepository knobDimmerRepository; @Autowired private DimmableRepository dimmableRepository; @Autowired protected KnobDimmerController( KnobDimmerRepository inputRepository, DimmableRepository outputRepository) { - super( - inputRepository, - outputRepository, - Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR); + super(inputRepository, outputRepository, Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR); this.knobDimmerRepository = inputRepository; this.dimmableRepository = outputRepository; } - @GetMapping - public List findAll() { - return toList(knobDimmerRepository.findAll()); - } - - @GetMapping("/{id}") - public KnobDimmer findById(@PathVariable("id") long id) throws NotFoundException { - return knobDimmerRepository.findById(id).orElseThrow(NotFoundException::new); - } - @PostMapping - public KnobDimmer create(@Valid @RequestBody GenericDeviceSaveReguest kd) { + public KnobDimmer create( + @Valid @RequestBody GenericDeviceSaveReguest kd, final Principal principal) { KnobDimmer newKD = new KnobDimmer(); newKD.setName(kd.getName()); newKD.setRoomId(kd.getRoomId()); - return knobDimmerRepository.save(newKD); + return deviceService.saveAsOwner(newKD, principal.getName()); } @PutMapping("/dimTo") @@ -69,7 +56,8 @@ public class KnobDimmerController } @DeleteMapping("/{id}") - public void delete(@PathVariable("id") long id) { - knobDimmerRepository.deleteById(id); + public void delete(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java index c349aa2..1d0c70e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java @@ -1,14 +1,13 @@ 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.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; import java.security.Principal; -import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -19,27 +18,20 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/motionSensor") public class MotionSensorController { + @Autowired private DeviceService deviceService; + @Autowired private MotionSensorRepository motionSensorService; @Autowired private SensorSocketEndpoint sensorSocketEndpoint; - @GetMapping - public List findAll() { - return toList(motionSensorService.findAll()); - } - - @GetMapping("/{id}") - public MotionSensor findById(@PathVariable("id") long id) throws NotFoundException { - return motionSensorService.findById(id).orElseThrow(NotFoundException::new); - } - @PostMapping - public MotionSensor create(@Valid @RequestBody GenericDeviceSaveReguest ms) { + public MotionSensor create( + @Valid @RequestBody GenericDeviceSaveReguest ms, final Principal principal) { MotionSensor newMS = new MotionSensor(); newMS.setName(ms.getName()); newMS.setRoomId(ms.getRoomId()); - return motionSensorService.save(newMS); + return deviceService.saveAsOwner(newMS, principal.getName()); } /** @@ -53,7 +45,8 @@ public class MotionSensorController { sensor.setDetected(detected); final MotionSensor toReturn = motionSensorService.save(sensor); - sensorSocketEndpoint.queueDeviceUpdate(sensor, motionSensorService.findUser(sensor.getId())); + sensorSocketEndpoint.queueDeviceUpdate( + sensor, motionSensorService.findUser(sensor.getId())); return toReturn; } @@ -73,7 +66,8 @@ public class MotionSensorController { } @DeleteMapping("/{id}") - public void delete(@PathVariable("id") long id) { - motionSensorService.deleteById(id); + public void delete(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index 308b8ee..47d98d0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -89,8 +89,9 @@ public class RegularLightController extends GuestEnabledController } @DeleteMapping("/{id}") - public void delete(@PathVariable("id") long id) { - regularLightRepository.deleteById(id); + public void delete(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } // the full url should be: "/regularLight/{id}/state?sceneId={sceneId} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java index d7fdc8e..c03c430 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java @@ -1,18 +1,16 @@ 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.SwitchableSaveRequest; 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.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; -import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -26,31 +24,24 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("/securityCamera") public class SecurityCameraController { + @Autowired DeviceService deviceService; @Autowired SecurityCameraRepository securityCameraService; @Autowired private SceneRepository sceneRepository; @Autowired private StateRepository> stateRepository; - @GetMapping - public List findAll() { - return toList(securityCameraService.findAll()); - } - - @GetMapping("/{id}") - public SecurityCamera findById(@PathVariable("id") long id) throws NotFoundException { - return securityCameraService.findById(id).orElseThrow(NotFoundException::new); - } - - private SecurityCamera save(SecurityCamera newSC, SwitchableSaveRequest sc) { + private SecurityCamera save( + SecurityCamera newSC, SwitchableSaveRequest sc, final Principal principal) { newSC.setName(sc.getName()); newSC.setRoomId(sc.getRoomId()); newSC.setOn(sc.isOn()); - return securityCameraService.save(newSC); + return deviceService.saveAsOwner(newSC, principal.getName()); } @PostMapping - public SecurityCamera create(@Valid @RequestBody SwitchableSaveRequest sc) { - return save(new SecurityCamera(), sc); + public SecurityCamera create( + @Valid @RequestBody SwitchableSaveRequest sc, final Principal principal) { + return save(new SecurityCamera(), sc, principal); } @PutMapping @@ -61,12 +52,14 @@ public class SecurityCameraController { securityCameraService .findByIdAndUsername(sc.getId(), principal.getName()) .orElseThrow(NotFoundException::new), - sc); + sc, + principal); } @DeleteMapping("/{id}") - public void delete(@PathVariable("id") long id) { - securityCameraService.deleteById(id); + public void delete(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index b56fd60..5c14f07 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -1,19 +1,16 @@ 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.SensorSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; 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.SensorService; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; - import java.math.BigDecimal; import java.security.Principal; import java.util.*; -import java.util.List; import javax.validation.Valid; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @@ -23,34 +20,23 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/sensor") public class SensorController { - @Autowired - private SensorRepository sensorRepository; + @Autowired private DeviceService deviceService; - @Autowired - private SensorSocketEndpoint sensorSocketEndpoint; + @Autowired private SensorRepository sensorRepository; - @Autowired - private SensorService sensorService; + @Autowired private SensorSocketEndpoint sensorSocketEndpoint; - @GetMapping - public List findAll() { - return toList(sensorRepository.findAll()); - } - - @GetMapping("/{id}") - public Sensor findById(@PathVariable("id") long id) throws NotFoundException { - return sensorRepository.findById(id).orElseThrow(NotFoundException::new); - } + @Autowired private SensorService sensorService; @PostMapping - public Sensor create(@Valid @RequestBody SensorSaveRequest s) { + public Sensor create(@Valid @RequestBody SensorSaveRequest s, final Principal principal) { Sensor newSensor = new Sensor(); newSensor.setSensor(s.getSensor()); newSensor.setName(s.getName()); newSensor.setRoomId(s.getRoomId()); newSensor.setValue(s.getValue()); - return sensorRepository.save(newSensor); + return deviceService.saveAsOwner(newSensor, principal.getName()); } @PutMapping("/{id}/value") @@ -67,7 +53,8 @@ public class SensorController { } @DeleteMapping("/{id}") - public void deleteById(@PathVariable("id") long id) { - sensorRepository.deleteById(id); + public void deleteById(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index d90c9ac..32fb300 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -1,13 +1,12 @@ 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.SwitchableSaveRequest; 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.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; -import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -18,32 +17,24 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/smartPlug") public class SmartPlugController { + @Autowired private DeviceService deviceService; @Autowired private SmartPlugRepository smartPlugRepository; @Autowired private SceneRepository sceneRepository; @Autowired private StateRepository> stateRepository; - @GetMapping - public List findAll() { - return toList(smartPlugRepository.findAll()); - } - - @GetMapping("/{id}") - public SmartPlug findById(@PathVariable("id") long id) throws NotFoundException { - return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new); - } - - private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp) { + private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) { newSP.setOn(sp.isOn()); newSP.setId(sp.getId()); newSP.setName(sp.getName()); newSP.setRoomId(sp.getRoomId()); - return smartPlugRepository.save(newSP); + return deviceService.saveAsOwner(newSP, principal.getName()); } @PostMapping - public SmartPlug create(@Valid @RequestBody SwitchableSaveRequest sp) { - return save(new SmartPlug(), sp); + public SmartPlug create( + @Valid @RequestBody SwitchableSaveRequest sp, final Principal principal) { + return save(new SmartPlug(), sp, principal); } @PutMapping @@ -53,7 +44,8 @@ public class SmartPlugController { smartPlugRepository .findByIdAndUsername(sp.getId(), principal.getName()) .orElseThrow(NotFoundException::new), - sp); + sp, + principal); } @DeleteMapping("/{id}/meter") @@ -69,8 +61,9 @@ public class SmartPlugController { } @DeleteMapping("/{id}") - public void deleteById(@PathVariable("id") long id) { - smartPlugRepository.deleteById(id); + public void deleteById(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index 3a87b18..eecd86e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -1,15 +1,13 @@ 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.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchOperationRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; import java.util.*; -import java.util.List; -import java.util.stream.Collectors; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -22,6 +20,7 @@ public class SwitchController extends InputDeviceConnectionController switchableRepository; + private DeviceService deviceService; /** * Contstructs the controller by requiring essential object for the controller implementation @@ -31,29 +30,23 @@ public class SwitchController extends InputDeviceConnectionController outputRepository) { + SwitchRepository inputRepository, + SwitchableRepository outputRepository, + DeviceService deviceService) { super(inputRepository, outputRepository, Switchable.SWITCH_SWITCHABLE_CONNECTOR); + this.deviceService = deviceService; this.switchRepository = inputRepository; this.switchableRepository = outputRepository; } - @GetMapping - public List findAll() { - return toList(switchRepository.findAll()); - } - - @GetMapping("/{id}") - public Switch findById(@PathVariable("id") long id) throws NotFoundException { - return switchRepository.findById(id).orElseThrow(NotFoundException::new); - } - @PostMapping - public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s) { + public Switch create( + @Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal) { Switch newSwitch = new Switch(); newSwitch.setName(s.getName()); newSwitch.setRoomId(s.getRoomId()); - return switchRepository.save(newSwitch); + return deviceService.saveAsOwner(newSwitch, principal.getName()); } @PutMapping("/operate") @@ -83,7 +76,8 @@ public class SwitchController extends InputDeviceConnectionController> stateRepository; - @GetMapping - public List findAll(Principal user) { - return thermostatService.findAll(user.getName()); - } - - @GetMapping("/{id}") - public Thermostat findById(@PathVariable("id") long id, Principal principal) - throws NotFoundException { - return thermostatService - .findById(id, principal.getName()) - .orElseThrow(NotFoundException::new); - } - - private Thermostat save(Thermostat newT, ThermostatSaveRequest t) { + private Thermostat save(Thermostat newT, ThermostatSaveRequest t, final Principal principal) { newT.setTargetTemperature(t.getTargetTemperature()); newT.setId(t.getId()); newT.setName(t.getName()); @@ -45,14 +31,15 @@ public class ThermostatController { newT.setUseExternalSensors(t.isUseExternalSensors()); newT.setOn(t.isTurnOn()); - newT = thermostatRepository.save(newT); + newT = deviceService.saveAsOwner(newT, principal.getName()); thermostatService.populateMeasuredTemperature(newT); return newT; } @PostMapping - public Thermostat create(@Valid @RequestBody ThermostatSaveRequest t) { - return save(new Thermostat(), t); + public Thermostat create( + @Valid @RequestBody ThermostatSaveRequest t, final Principal principal) { + return save(new Thermostat(), t, principal); } @PutMapping @@ -62,12 +49,14 @@ public class ThermostatController { thermostatRepository .findByIdAndUsername(t.getId(), principal.getName()) .orElseThrow(NotFoundException::new), - t); + t, + principal); } @DeleteMapping("/{id}") - public void deleteById(@PathVariable("id") long id) { - thermostatRepository.deleteById(id); + public void deleteById(@PathVariable("id") long id, final Principal principal) + throws NotFoundException { + deviceService.delete(id, principal.getName()); } @PostMapping("/{id}/state") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index 3b681c5..dfc852d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -70,6 +70,20 @@ public abstract class Device { @Transient @GsonExclude private boolean fromGuest = false; + @Transient @GsonExclude private boolean deleted = false; + + public boolean isFromHost() { + return fromHost; + } + + public boolean isDeleted() { + return deleted; + } + + public void setDeleted(boolean deleted) { + this.deleted = deleted; + } + public boolean isFromGuest() { return fromGuest; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index 4bf6cca..1ed0ea4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -44,7 +44,7 @@ public class DeviceService { // enqueue all device updates for all other guests endpoint.queueDeviceUpdate(device, guest); } - // broadcast device updates for all other guests + return device; } @@ -60,8 +60,26 @@ public class DeviceService { // broadcast to endpoint the object device, with receiving user set to guest endpoint.queueDeviceUpdate(device, guest); } - // after queueing the device update for each user, flush them all in a single message - // can be moved inside the foreach loop to send a single message for each update enqueued + return device; } + + public void delete(Long id, String username) throws NotFoundException { + Device device = + deviceRepository + .findByIdAndUsername(id, username) + .orElseThrow(NotFoundException::new); + deviceRepository.delete(device); + + final User user = userRepository.findByUsername(username); + final Set guests = user.getGuests(); + + device.setFromHost(true); + device.setFromGuest(false); + device.setDeleted(true); + for (final User guest : guests) { + // broadcast to endpoint the object device, with receiving user set to guest + endpoint.queueDeviceUpdate(device, guest); + } + } }