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