Controllers for automation
This commit is contained in:
parent
fc25177a69
commit
509d890c7e
4 changed files with 123 additions and 0 deletions
|
@ -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<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,
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
public class ScenePrioritySaveRequest {}
|
Loading…
Reference in a new issue