Merge branch '60-we-forgot-the-controllers-for-automations-and-scenepriority-as-well-s-the-dtos' into 'dev'

Resolve "We forgot the controllers for Automations and ScenePriority as well s the DTOs"

Closes #60

See merge request sa4-2020/the-sanmarinoes/backend!85
This commit is contained in:
Matteo Omenetti 2020-04-25 16:23:58 +02:00
commit ca05c0a596
13 changed files with 458 additions and 5 deletions

View file

@ -0,0 +1,79 @@
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<Automation> 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) {
return save(new Automation(), automationSaveRequest);
}
@PutMapping
public Automation update(@Valid @RequestBody AutomationSaveRequest automation)
throws NotFoundException {
return save(
automationRepository
.findById(automation.getId())
.orElseThrow(NotFoundException::new),
automation);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable long id) {
automationRepository.deleteById(id);
}
}

View file

@ -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<BooleanTrigger<?>> 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);
}
}

View file

@ -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<RangeTrigger<?>> 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);
}
}

View file

@ -0,0 +1,60 @@
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;
@RestController
@EnableAutoConfiguration
@RequestMapping("/scenePriority")
public class ScenePriorityController {
@Autowired ScenePriorityRepository scenePriorityRepository;
@GetMapping("/{id}")
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);
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,38 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
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;
}
}

View file

@ -5,6 +5,8 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Entity @Entity
public class Automation { public class Automation {
@ -23,6 +25,8 @@ public class Automation {
@GsonExclude @GsonExclude
private Set<ScenePriority> scenes = new HashSet<>(); private Set<ScenePriority> scenes = new HashSet<>();
@NotNull @NotEmpty private String name;
public long getId() { public long getId() {
return id; return id;
} }
@ -38,4 +42,12 @@ public class Automation {
public Set<ScenePriority> getScenes() { public Set<ScenePriority> getScenes() {
return scenes; return scenes;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }

View file

@ -1,4 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List;
import org.springframework.data.repository.query.Param;
public interface BooleanTriggerRepository public interface BooleanTriggerRepository
extends TriggerRepository<BooleanTrigger<? extends Device>> {} extends TriggerRepository<BooleanTrigger<? extends Device>> {
List<BooleanTrigger<?>> findAllByAutomationId(@Param("automationId") long automationId);
}

View file

@ -29,7 +29,7 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
throw new IllegalStateException(); throw new IllegalStateException();
} }
enum Operator { public enum Operator {
@SerializedName("EQUAL") @SerializedName("EQUAL")
EQUAL, EQUAL,
@SerializedName("LESS") @SerializedName("LESS")
@ -62,7 +62,7 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
return range; return range;
} }
public void setRange(Float range) { public void setRange(Double range) {
this.range = range; this.range = range;
} }
} }

View file

@ -1,3 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface RangeTriggerRepository extends TriggerRepository<RangeTrigger<? extends Device>> {} import java.util.List;
import org.springframework.data.repository.query.Param;
public interface RangeTriggerRepository extends TriggerRepository<RangeTrigger<? extends Device>> {
List<RangeTrigger<?>> findAllByAutomationId(@Param("automationId") long automationId);
}

View file

@ -1,5 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface ScenePriorityRepository extends CrudRepository<ScenePriority, Long> {} public interface ScenePriorityRepository extends CrudRepository<ScenePriority, Long> {
List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId);
void deleteBySceneId(@Param("sceneId") long sceneId);
}