From 509d890c7ee2cf7a8485379bd22e87ee9f3abf6c Mon Sep 17 00:00:00 2001 From: omenem Date: Sat, 25 Apr 2020 14:56:07 +0200 Subject: [PATCH 1/7] Controllers for automation --- .../controller/AutomationController.java | 82 +++++++++++++++++++ .../controller/ScenePriorityController.java | 15 ++++ .../smarthut/dto/AutomationSaveRequest.java | 23 ++++++ .../dto/ScenePrioritySaveRequest.java | 3 + 4 files changed, 123 insertions(+) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java new file mode 100644 index 0000000..62d27b6 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -0,0 +1,82 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository; +import java.security.Principal; +import java.util.Collection; +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.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.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/automation") +public class AutomationController { + + @Autowired private AutomationRepository automationRepository; + @Autowired private SceneRepository sceneRepository; + @Autowired private ScenePriorityRepository scenePriorityRepository; + + @GetMapping + public List getAll( + @RequestParam(value = "hostId", required = false) Long hostId, final Principal user) + throws NotFoundException { + return sceneRepository + .findByUsername(user.getName()) + .stream() + .map(s -> scenePriorityRepository.findAllBySceneId(s.getId())) + .flatMap(Collection::stream) + .distinct() + .map( + t -> + automationRepository + .findById(t.getAutomationId()) + .orElseThrow(IllegalStateException::new)) + .distinct() + .collect(Collectors.toList()); + } + + private Automation save(Automation newRL, AutomationSaveRequest s) { + newRL.setName(s.getName()); + + return automationRepository.save(newRL); + } + + @PostMapping + public Automation create( + @Valid @RequestBody AutomationSaveRequest automationSaveRequest, + final Principal principal) { + return save(new Automation(), automationSaveRequest); + } + + @PutMapping + public Automation update( + @Valid @RequestBody AutomationSaveRequest automation, final Principal principal) + throws NotFoundException { + return save( + automationRepository + .findById(automation.getId()) + .orElseThrow(NotFoundException::new), + automation); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + automationRepository.deleteById(id); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java new file mode 100644 index 0000000..9093a71 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java @@ -0,0 +1,15 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableAutoConfiguration +@RequestMapping("/scenePriority") +public class ScenePriorityController { + + @Autowired ScenePriorityRepository scenePriorityRepository; +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java new file mode 100644 index 0000000..bcd1f8b --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/AutomationSaveRequest.java @@ -0,0 +1,23 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +public class AutomationSaveRequest { + + private long id; + + @NotNull @NotEmpty private String name; + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java new file mode 100644 index 0000000..12dc23b --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java @@ -0,0 +1,3 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +public class ScenePrioritySaveRequest {} From 5eadb2314c2163124de79a5ac628e890eaae4341 Mon Sep 17 00:00:00 2001 From: omenem Date: Sat, 25 Apr 2020 15:00:22 +0200 Subject: [PATCH 2/7] wip --- .../smarthut/controller/ScenePriorityController.java | 8 ++++++++ .../sa4/sanmarinoes/smarthut/models/Automation.java | 12 ++++++++++++ .../smarthut/models/ScenePriorityRepository.java | 7 ++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java index 9093a71..fc1a45f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java @@ -1,8 +1,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -12,4 +15,9 @@ import org.springframework.web.bind.annotation.RestController; public class ScenePriorityController { @Autowired ScenePriorityRepository scenePriorityRepository; + + @GetMapping("/{id}") + public ScenePriority get(@PathVariable long id) { + return new ScenePriority(); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java index fb5d7d2..27bda84 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java @@ -5,6 +5,8 @@ import io.swagger.annotations.ApiModelProperty; import java.util.HashSet; import java.util.Set; import javax.persistence.*; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; @Entity public class Automation { @@ -23,6 +25,8 @@ public class Automation { @GsonExclude private Set scenes = new HashSet<>(); + @NotNull @NotEmpty private String name; + public long getId() { return id; } @@ -38,4 +42,12 @@ public class Automation { public Set getScenes() { return scenes; } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java index 2dc81c4..30ee4e5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java @@ -1,5 +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 ScenePriorityRepository extends CrudRepository {} +public interface ScenePriorityRepository extends CrudRepository { + + List findAllBySceneId(@Param("sceneId") long sceneId); +} From db35205ed8c517d9f534fbf2cdc567cac1458dbd Mon Sep 17 00:00:00 2001 From: omenem Date: Sat, 25 Apr 2020 15:20:00 +0200 Subject: [PATCH 3/7] controller for scenePriority --- .../controller/AutomationController.java | 7 +--- .../controller/ScenePriorityController.java | 41 ++++++++++++++++++- .../dto/ScenePrioritySaveRequest.java | 37 ++++++++++++++++- .../models/ScenePriorityRepository.java | 2 + 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java index 62d27b6..f888481 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -58,15 +58,12 @@ public class AutomationController { } @PostMapping - public Automation create( - @Valid @RequestBody AutomationSaveRequest automationSaveRequest, - final Principal principal) { + public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) { return save(new Automation(), automationSaveRequest); } @PutMapping - public Automation update( - @Valid @RequestBody AutomationSaveRequest automation, final Principal principal) + public Automation update(@Valid @RequestBody AutomationSaveRequest automation) throws NotFoundException { return save( automationRepository diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java index fc1a45f..d0bbb5a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ScenePriorityController.java @@ -1,11 +1,18 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ScenePrioritySaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; +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; @@ -17,7 +24,37 @@ public class ScenePriorityController { @Autowired ScenePriorityRepository scenePriorityRepository; @GetMapping("/{id}") - public ScenePriority get(@PathVariable long id) { - return new ScenePriority(); + public ScenePriority get(@PathVariable long id) throws NotFoundException { + return scenePriorityRepository.findById(id).orElseThrow(NotFoundException::new); + } + + private ScenePriority save(ScenePriority newRL, ScenePrioritySaveRequest s) { + newRL.setPriority(s.getPriority()); + newRL.setAutomationId(s.getAutomationId()); + newRL.setSceneId(s.getSceneId()); + + return scenePriorityRepository.save(newRL); + } + + @PostMapping + public ScenePriority create( + @Valid @RequestBody ScenePrioritySaveRequest scenePrioritySaveRequest) { + return save(new ScenePriority(), scenePrioritySaveRequest); + } + + @PutMapping + public ScenePriority update( + @Valid @RequestBody ScenePrioritySaveRequest scenePrioritySaveRequest) + throws NotFoundException { + return save( + scenePriorityRepository + .findById(scenePrioritySaveRequest.getSceneId()) + .orElseThrow(NotFoundException::new), + scenePrioritySaveRequest); + } + + @DeleteMapping("/{id}") + public void delete(@PathVariable long id) { + scenePriorityRepository.deleteBySceneId(id); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java index 12dc23b..14a1873 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ScenePrioritySaveRequest.java @@ -1,3 +1,38 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; -public class ScenePrioritySaveRequest {} +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +public class ScenePrioritySaveRequest { + + @NotNull private Long automationId; + + @Min(0) + private Integer priority; + + @NotNull private Long sceneId; + + public Long getAutomationId() { + return automationId; + } + + public void setAutomationId(Long automationId) { + this.automationId = automationId; + } + + public Integer getPriority() { + return priority; + } + + public void setPriority(Integer priority) { + this.priority = priority; + } + + public Long getSceneId() { + return sceneId; + } + + public void setSceneId(Long sceneId) { + this.sceneId = sceneId; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java index 30ee4e5..020a369 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ScenePriorityRepository.java @@ -7,4 +7,6 @@ import org.springframework.data.repository.query.Param; public interface ScenePriorityRepository extends CrudRepository { List findAllBySceneId(@Param("sceneId") long sceneId); + + void deleteBySceneId(@Param("sceneId") long sceneId); } From d61c7bbc0b01a55b6ff6b354a5ffe8f13b277e2f Mon Sep 17 00:00:00 2001 From: omenem Date: Sat, 25 Apr 2020 15:59:06 +0200 Subject: [PATCH 4/7] 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); +} From c5aab751df0ea1adba7a1eeab3a5605b2a051c61 Mon Sep 17 00:00:00 2001 From: Nicola Brunner Date: Sat, 25 Apr 2020 16:33:00 +0200 Subject: [PATCH 5/7] fix update sensor --- .../java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java | 2 +- src/main/resources/application-dev.properties | 2 +- src/main/resources/application-prod.properties | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 a84e705..397bb1e 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 @@ -38,7 +38,7 @@ public abstract class Device { @OneToMany(mappedBy = "device", orphanRemoval = true) @GsonExclude - // @SocketGsonExclude + @SocketGsonExclude private Set> triggers = new HashSet<>(); /** diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 3c77362..8720442 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -27,7 +27,7 @@ spring.mail.properties.mail.smtp.writetimeout=5000 email.registrationSubject=Complete your SmartHut.sm registration email.registration=To confirm your registration, please click here: email.registrationPath=http://localhost:8080/register/confirm-account?token= -email.registrationRedirect=http://localhost:3000 +email.registrationRedirect=http://localhost:3000/login email.resetpasswordSubject=SmartHut.sm password reset email.resetpassword=To reset your password, please click here: diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 5888106..1727673 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -32,7 +32,7 @@ spring.mail.properties.mail.smtp.writetimeout=5000 email.registrationSubject=Complete your SmartHut.sm registration email.registration=To confirm your registration, please click here: email.registrationPath=${BACKEND_URL}/register/confirm-account?token= -email.registrationRedirect=${FRONTEND_URL} +email.registrationRedirect=${FRONTEND_URL}/login # Password reset email properties From be7c9ce99f3074396a5745c963bf5cb0a736013c Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 25 Apr 2020 22:51:30 +0200 Subject: [PATCH 6/7] fix duplicate device names --- .../controller/BooleanTriggerController.java | 4 ++-- .../smarthut/models/DeviceRepository.java | 4 ++++ .../smarthut/service/DeviceService.java | 14 +++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) 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 index 927b66c..9331b56 100644 --- 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 @@ -29,7 +29,7 @@ public class BooleanTriggerController { return booleanTriggerRepository.findAllByAutomationId(automationId); } - private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { + private BooleanTrigger save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { newRL.setDeviceId(s.getDeviceId()); newRL.setAutomationId(s.getAutomationId()); newRL.setOn(s.isOn()); @@ -40,7 +40,7 @@ public class BooleanTriggerController { @PostMapping public BooleanTrigger create( @Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) { - return save(new BooleanTrigger(), booleanTriggerSaveRequest); + return save(new BooleanTrigger<>(), booleanTriggerSaveRequest); } @PutMapping diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java index cf5c004..d1f828d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DeviceRepository.java @@ -14,6 +14,10 @@ import org.springframework.data.repository.query.Param; public interface DeviceRepository extends CrudRepository { List findByRoomId(@Param("roomId") long roomId); + @Query( + "SELECT COUNT(d) FROM Device d JOIN d.room r JOIN r.user u WHERE d.name = ?1 AND u.username = ?2") + Integer findDuplicates(String name, String username); + /** * Finds devices by their id and a username * 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 87df762..0bc2a6e 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 @@ -29,7 +29,15 @@ public class DeviceService { roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new); } - public void triggerTriggers(Device device) { + private void renameIfDuplicate(Device toCreate, String username) { + while (deviceRepository.findDuplicates(toCreate.getName(), username) + - (toCreate.getId() <= 0 ? 0 : 1) + > 0) { + toCreate.setName(toCreate.getName() + " (new)"); + } + } + + private void triggerTriggers(Device device) { final long deviceId = device.getId(); @@ -107,6 +115,8 @@ public class DeviceService { final User currentUser = userRepository.findByUsername(guestUsername); final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); if (!host.getGuests().contains(currentUser)) throw new NotFoundException(); + renameIfDuplicate(device, host.getUsername()); + device = deviceRepository.save(device); final Set guests = Set.copyOf(host.getGuests()); // We're telling the host that a guest has modified a device. Therefore, fromGuest becomes @@ -145,6 +155,7 @@ public class DeviceService { public List saveAllAsOwner( Iterable devices, String username, boolean fromScene) { + devices.forEach(d -> renameIfDuplicate(d, username)); devices = deviceRepository.saveAll(devices); devices.forEach((d) -> propagateUpdateAsOwner(d, username)); @@ -156,6 +167,7 @@ public class DeviceService { } public T saveAsOwner(T device, String username, boolean fromScene) { + renameIfDuplicate(device, username); device = deviceRepository.save(device); propagateUpdateAsOwner(device, username); From 10dc0c97415f94de4c488c5544c2f4c30f698a58 Mon Sep 17 00:00:00 2001 From: omenem Date: Sun, 26 Apr 2020 12:13:46 +0200 Subject: [PATCH 7/7] new route fix --- .../smarthut/controller/AutomationController.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java index f888481..445bb83 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/AutomationController.java @@ -51,6 +51,11 @@ public class AutomationController { .collect(Collectors.toList()); } + @GetMapping("/{id}") + public Automation get(@PathVariable long id) throws NotFoundException { + return automationRepository.findById(id).orElseThrow(NotFoundException::new); + } + private Automation save(Automation newRL, AutomationSaveRequest s) { newRL.setName(s.getName());