controller for scenePriority
This commit is contained in:
parent
5eadb2314c
commit
db35205ed8
4 changed files with 79 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,4 +7,6 @@ import org.springframework.data.repository.query.Param;
|
|||
public interface ScenePriorityRepository extends CrudRepository<ScenePriority, Long> {
|
||||
|
||||
List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId);
|
||||
|
||||
void deleteBySceneId(@Param("sceneId") long sceneId);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue