From 1bde4344bea93a088241556ba525d5663a2b2496 Mon Sep 17 00:00:00 2001 From: omenem Date: Thu, 7 May 2020 18:12:55 +0200 Subject: [PATCH] Added controllers repos and dtos --- .../BooleanConditionController.java | 61 ++++++++++++++++++ .../controller/RangeConditionController.java | 62 +++++++++++++++++++ .../ThermostatConditionController.java | 62 +++++++++++++++++++ .../dto/BooleanConditionSaveRequest.java | 42 +++++++++++++ .../dto/RangeConditionSaveRequest.java | 54 ++++++++++++++++ .../dto/ThermostatConditionSaveRequest.java | 56 +++++++++++++++++ .../models/BooleanConditionRepository.java | 10 +++ .../smarthut/models/Condition.java | 12 ---- .../smarthut/models/ConditionRepository.java | 10 +++ .../smarthut/models/RangeCondition.java | 10 +-- .../models/RangeConditionRepository.java | 10 +++ .../smarthut/models/ThermostatCondition.java | 10 +-- .../models/ThermostatConditionRepository.java | 9 +++ 13 files changed, 386 insertions(+), 22 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanConditionSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeConditionSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatConditionRepository.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java new file mode 100644 index 0000000..fac99b5 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/BooleanConditionController.java @@ -0,0 +1,61 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository; +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("/booleanCondition") +public class BooleanConditionController { + + @Autowired BooleanConditionRepository booleanConditionRepository; + + @GetMapping("/{automationId}") + public List> getAll(@PathVariable long automationId) { + return booleanConditionRepository.findAllByAutomationId(automationId); + } + + private BooleanCondition save(BooleanCondition newRL, BooleanConditionSaveRequest s) { + newRL.setDeviceId(s.getDeviceId()); + newRL.setAutomationId(s.getAutomationId()); + newRL.setOn(s.isOn()); + + return booleanConditionRepository.save(newRL); + } + + @PostMapping + public BooleanCondition create( + @Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) { + return save(new BooleanCondition<>(), booleanTriggerSaveRequest); + } + + @PutMapping + public BooleanCondition update( + @Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) + throws NotFoundException { + return save( + booleanConditionRepository + .findById(booleanTriggerSaveRequest.getId()) + .orElseThrow(NotFoundException::new), + booleanTriggerSaveRequest); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + booleanConditionRepository.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java new file mode 100644 index 0000000..25491b0 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RangeConditionController.java @@ -0,0 +1,62 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeConditionRepository; +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("/rangeCondition") +public class RangeConditionController { + + @Autowired RangeConditionRepository rangeConditionRepository; + + @GetMapping("/{automationId}") + public List> getAll(@PathVariable long automationId) { + return rangeConditionRepository.findAllByAutomationId(automationId); + } + + private RangeCondition save(RangeCondition newRL, RangeConditionSaveRequest s) { + newRL.setDeviceId(s.getDeviceId()); + newRL.setAutomationId(s.getAutomationId()); + newRL.setOperator(s.getOperator()); + newRL.setRange(s.getRange()); + + return rangeConditionRepository.save(newRL); + } + + @PostMapping + public RangeCondition create( + @Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) { + return save(new RangeCondition<>(), booleanTriggerSaveRequest); + } + + @PutMapping + public RangeCondition update( + @Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) + throws NotFoundException { + return save( + rangeConditionRepository + .findById(booleanTriggerSaveRequest.getId()) + .orElseThrow(NotFoundException::new), + booleanTriggerSaveRequest); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + rangeConditionRepository.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionController.java new file mode 100644 index 0000000..a6d118a --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatConditionController.java @@ -0,0 +1,62 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatConditionSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatConditionRepository; +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("/thermostatCondition") +public class ThermostatConditionController { + + @Autowired ThermostatConditionRepository thermostatConditionRepository; + + @GetMapping("/{automationId}") + public List getAll(@PathVariable long automationId) { + return thermostatConditionRepository.findAllByAutomationId(automationId); + } + + private ThermostatCondition save(ThermostatCondition newRL, ThermostatConditionSaveRequest s) { + newRL.setDeviceId(s.getDeviceId()); + newRL.setAutomationId(s.getAutomationId()); + newRL.setOperator(s.getOperator()); + newRL.setMode(s.getMode()); + + return thermostatConditionRepository.save(newRL); + } + + @PostMapping + public ThermostatCondition create( + @Valid @RequestBody ThermostatConditionSaveRequest booleanTriggerSaveRequest) { + return save(new ThermostatCondition(), booleanTriggerSaveRequest); + } + + @PutMapping + public ThermostatCondition update( + @Valid @RequestBody ThermostatConditionSaveRequest booleanTriggerSaveRequest) + throws NotFoundException { + return save( + thermostatConditionRepository + .findById(booleanTriggerSaveRequest.getId()) + .orElseThrow(NotFoundException::new), + booleanTriggerSaveRequest); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + thermostatConditionRepository.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanConditionSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanConditionSaveRequest.java new file mode 100644 index 0000000..2abf6e0 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/BooleanConditionSaveRequest.java @@ -0,0 +1,42 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.NotNull; + +public class BooleanConditionSaveRequest { + + @NotNull private long id; + + @NotNull private Long deviceId; + + @NotNull private Long automationId; + + @NotNull 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/RangeConditionSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeConditionSaveRequest.java new file mode 100644 index 0000000..8cf4bf6 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RangeConditionSaveRequest.java @@ -0,0 +1,54 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition.Operator; +import javax.validation.constraints.NotNull; + +public class RangeConditionSaveRequest { + + @NotNull private long id; + + @NotNull private Long deviceId; + + @NotNull private Long automationId; + + @NotNull private RangeCondition.Operator operator; + + @NotNull private double range; + + 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 Operator getOperator() { + return operator; + } + + public void setOperator(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/dto/ThermostatConditionSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java new file mode 100644 index 0000000..8c56f2d --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatConditionSaveRequest.java @@ -0,0 +1,56 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat.Mode; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition.Operator; +import javax.validation.constraints.NotNull; + +public class ThermostatConditionSaveRequest { + + @NotNull private long id; + + @NotNull private Long deviceId; + + @NotNull private Long automationId; + + @NotNull private ThermostatCondition.Operator operator; + + @NotNull private Thermostat.Mode mode; + + 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 Operator getOperator() { + return operator; + } + + public void setOperator(Operator operator) { + this.operator = operator; + } + + public Mode getMode() { + return mode; + } + + public void setMode(Mode mode) { + this.mode = mode; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java new file mode 100644 index 0000000..c564c41 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/BooleanConditionRepository.java @@ -0,0 +1,10 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.List; +import org.springframework.data.repository.query.Param; + +public interface BooleanConditionRepository + extends ConditionRepository> { + + List> findAllByAutomationId(@Param("automationId") long automationId); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java index e643f24..be917b8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Condition.java @@ -41,10 +41,6 @@ public abstract class Condition { @GsonExclude private Long userId; - @NotNull - @Column(nullable = false) - private String name; - @ManyToOne(targetEntity = Device.class) @JoinColumn(name = "device_id", updatable = false, insertable = false) @GsonExclude @@ -93,14 +89,6 @@ public abstract class Condition { this.userId = userId; } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - public D getDevice() { return device; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java new file mode 100644 index 0000000..346dfd5 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConditionRepository.java @@ -0,0 +1,10 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.List; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; + +public interface ConditionRepository> extends CrudRepository { + + List findAllByDeviceId(@Param("deviceId") long deviceId); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java index 6047235..7ad1a8d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeCondition.java @@ -9,8 +9,8 @@ import javax.validation.constraints.NotNull; @Entity public class RangeCondition extends Condition { - protected RangeCondition(String kind) { - super(kind); + public RangeCondition() { + super("booleanCondition"); } public enum Operator { @@ -28,17 +28,17 @@ public class RangeCondition extends Conditi @NotNull @Column(nullable = false) - private RangeTrigger.Operator operator; + private RangeCondition.Operator operator; @NotNull @Column(nullable = false) private double range; - public RangeTrigger.Operator getOperator() { + public RangeCondition.Operator getOperator() { return operator; } - public void setOperator(RangeTrigger.Operator operator) { + public void setOperator(RangeCondition.Operator operator) { this.operator = operator; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java new file mode 100644 index 0000000..1ed6aa3 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeConditionRepository.java @@ -0,0 +1,10 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.List; +import org.springframework.data.repository.query.Param; + +public interface RangeConditionRepository + extends ConditionRepository> { + + List> findAllByAutomationId(@Param("automationId") long automationId); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java index 439f32d..2bb65d1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatCondition.java @@ -7,8 +7,8 @@ import javax.persistence.Entity; @Entity public class ThermostatCondition extends Condition { - protected ThermostatCondition(String kind) { - super(kind); + public ThermostatCondition() { + super("thermostatCondition"); } public enum Operator { @@ -32,12 +32,12 @@ public class ThermostatCondition extends Condition { this.operator = operator; } - public Thermostat.Mode getState() { + public Thermostat.Mode getMode() { return mode; } - public void setState(Thermostat.Mode state) { - this.mode = state; + public void setMode(Thermostat.Mode mode) { + this.mode = mode; } @Override diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatConditionRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatConditionRepository.java new file mode 100644 index 0000000..4b529fb --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatConditionRepository.java @@ -0,0 +1,9 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.List; +import org.springframework.data.repository.query.Param; + +public interface ThermostatConditionRepository extends ConditionRepository { + + List findAllByAutomationId(@Param("automationId") long automationId); +}