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
|
@PostMapping
|
||||||
public Automation create(
|
public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) {
|
||||||
@Valid @RequestBody AutomationSaveRequest automationSaveRequest,
|
|
||||||
final Principal principal) {
|
|
||||||
return save(new Automation(), automationSaveRequest);
|
return save(new Automation(), automationSaveRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public Automation update(
|
public Automation update(@Valid @RequestBody AutomationSaveRequest automation)
|
||||||
@Valid @RequestBody AutomationSaveRequest automation, final Principal principal)
|
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
return save(
|
return save(
|
||||||
automationRepository
|
automationRepository
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
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.ScenePriority;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
|
||||||
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
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.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@ -17,7 +24,37 @@ public class ScenePriorityController {
|
||||||
@Autowired ScenePriorityRepository scenePriorityRepository;
|
@Autowired ScenePriorityRepository scenePriorityRepository;
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public ScenePriority get(@PathVariable long id) {
|
public ScenePriority get(@PathVariable long id) throws NotFoundException {
|
||||||
return new ScenePriority();
|
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;
|
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> {
|
public interface ScenePriorityRepository extends CrudRepository<ScenePriority, Long> {
|
||||||
|
|
||||||
List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId);
|
List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId);
|
||||||
|
|
||||||
|
void deleteBySceneId(@Param("sceneId") long sceneId);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue