From d61c7bbc0b01a55b6ff6b354a5ffe8f13b277e2f Mon Sep 17 00:00:00 2001 From: omenem Date: Sat, 25 Apr 2020 15:59:06 +0200 Subject: [PATCH] controller for triggers --- .../controller/BooleanTriggerController.java | 61 ++++++++++++++++++ .../controller/RangeTriggerController.java | 62 +++++++++++++++++++ .../dto/BooleanTriggerSaveRequest.java | 42 +++++++++++++ .../smarthut/dto/RangeTriggerSaveRequest.java | 57 +++++++++++++++++ .../models/BooleanTriggerRepository.java | 8 ++- .../smarthut/models/RangeTrigger.java | 4 +- .../models/RangeTriggerRepository.java | 8 ++- 7 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanTriggerSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeTriggerSaveRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java new file mode 100644 index 0000000..927b66c --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanTriggerController.java @@ -0,0 +1,61 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanTriggerSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository; +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; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/booleanTrigger") +public class BooleanTriggerController { + + @Autowired BooleanTriggerRepository booleanTriggerRepository; + + @GetMapping("/{automationId}") + public List> getAll(@PathVariable long automationId) { + return booleanTriggerRepository.findAllByAutomationId(automationId); + } + + private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { + newRL.setDeviceId(s.getDeviceId()); + newRL.setAutomationId(s.getAutomationId()); + newRL.setOn(s.isOn()); + + return booleanTriggerRepository.save(newRL); + } + + @PostMapping + public BooleanTrigger create( + @Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) { + return save(new BooleanTrigger(), booleanTriggerSaveRequest); + } + + @PutMapping + public BooleanTrigger update( + @Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) + throws NotFoundException { + return save( + booleanTriggerRepository + .findById(booleanTriggerSaveRequest.getId()) + .orElseThrow(NotFoundException::new), + booleanTriggerSaveRequest); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + booleanTriggerRepository.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java new file mode 100644 index 0000000..7043566 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeTriggerController.java @@ -0,0 +1,62 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeTriggerSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTriggerRepository; +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; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/rangeTrigger") +public class RangeTriggerController { + + @Autowired RangeTriggerRepository rangeTriggerRepository; + + @GetMapping("/{automationId}") + public List> getAll(@PathVariable long automationId) { + return rangeTriggerRepository.findAllByAutomationId(automationId); + } + + private RangeTrigger save(RangeTrigger newRL, RangeTriggerSaveRequest s) { + newRL.setDeviceId(s.getDeviceId()); + newRL.setAutomationId(s.getAutomationId()); + newRL.setOperator(s.getOperator()); + newRL.setRange(s.getRange()); + + return rangeTriggerRepository.save(newRL); + } + + @PostMapping + public RangeTrigger create( + @Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) { + return save(new RangeTrigger(), booleanTriggerSaveRequest); + } + + @PutMapping + public RangeTrigger update( + @Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) + throws NotFoundException { + return save( + rangeTriggerRepository + .findById(booleanTriggerSaveRequest.getId()) + .orElseThrow(NotFoundException::new), + booleanTriggerSaveRequest); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + rangeTriggerRepository.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanTriggerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanTriggerSaveRequest.java new file mode 100644 index 0000000..2c08bc0 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanTriggerSaveRequest.java @@ -0,0 +1,42 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.NotNull; + +public class BooleanTriggerSaveRequest { + + private long id; + + @NotNull private Long deviceId; + + @NotNull private Long automationId; + + private boolean on; + + public long getId() { + return id; + } + + public Long getDeviceId() { + return deviceId; + } + + public void setDeviceId(Long deviceId) { + this.deviceId = deviceId; + } + + public Long getAutomationId() { + return automationId; + } + + public void setAutomationId(Long automationId) { + this.automationId = automationId; + } + + public boolean isOn() { + return on; + } + + public void setOn(boolean on) { + this.on = on; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeTriggerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeTriggerSaveRequest.java new file mode 100644 index 0000000..567c035 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeTriggerSaveRequest.java @@ -0,0 +1,57 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger; +import javax.validation.constraints.NotNull; + +public class RangeTriggerSaveRequest { + + private long id; + + @NotNull private Long deviceId; + + @NotNull private Long automationId; + + @NotNull private RangeTrigger.Operator operator; + + @NotNull private double range; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Long getDeviceId() { + return deviceId; + } + + public void setDeviceId(Long deviceId) { + this.deviceId = deviceId; + } + + public Long getAutomationId() { + return automationId; + } + + public void setAutomationId(Long automationId) { + this.automationId = automationId; + } + + public RangeTrigger.Operator getOperator() { + return operator; + } + + public void setOperator(RangeTrigger.Operator operator) { + this.operator = operator; + } + + public double getRange() { + return range; + } + + public void setRange(Double range) { + this.range = range; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerRepository.java index 932f4c8..08b8898 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanTriggerRepository.java @@ -1,4 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.List; +import org.springframework.data.repository.query.Param; + public interface BooleanTriggerRepository - extends TriggerRepository> {} + extends TriggerRepository> { + + List> findAllByAutomationId(@Param("automationId") long automationId); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java index 73c86eb..a03781d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java @@ -29,7 +29,7 @@ public class RangeTrigger extends Trigger extends Trigger> {} +import java.util.List; +import org.springframework.data.repository.query.Param; + +public interface RangeTriggerRepository extends TriggerRepository> { + + List> findAllByAutomationId(@Param("automationId") long automationId); +}