Code review
This commit is contained in:
parent
570b1e8b29
commit
37ea9591cf
3 changed files with 56 additions and 37 deletions
|
@ -5,8 +5,8 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
|||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SceneService;
|
||||
import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -26,20 +26,21 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RequestMapping("/scene")
|
||||
public class SceneController {
|
||||
|
||||
@Autowired SceneRepository sceneService;
|
||||
@Autowired UserRepository userService;
|
||||
@Autowired StateRepository<State<?>> stateService;
|
||||
@Autowired DeviceRepository<Device> deviceRepository;
|
||||
@Autowired private SceneRepository sceneRepository;
|
||||
@Autowired private SceneService sceneService;
|
||||
@Autowired private UserRepository userService;
|
||||
@Autowired private StateRepository<State<?>> stateService;
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
|
||||
@GetMapping
|
||||
public List<Scene> findAll(Principal principal) {
|
||||
return toList(sceneService.findByUsername(principal.getName()));
|
||||
return toList(sceneRepository.findByUsername(principal.getName()));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public @ResponseBody Scene findById(@PathVariable("id") long id, Principal principal)
|
||||
throws NotFoundException {
|
||||
return sceneService
|
||||
return sceneRepository
|
||||
.findByIdAndUsername(id, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
@ -56,26 +57,18 @@ public class SceneController {
|
|||
newScene.setUserId(userId);
|
||||
newScene.setName(s.getName());
|
||||
|
||||
return sceneService.save(newScene);
|
||||
return sceneRepository.save(newScene);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/apply")
|
||||
public @ResponseBody List<Device> apply(@PathVariable("id") long id, final Principal principal)
|
||||
throws NotFoundException {
|
||||
final Scene newScene =
|
||||
sceneService
|
||||
sceneRepository
|
||||
.findByIdAndUsername(id, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
|
||||
final List<Device> updated = new ArrayList<>();
|
||||
|
||||
for (final State<?> s : newScene.getStates()) {
|
||||
s.apply();
|
||||
updated.add(s.getDevice());
|
||||
}
|
||||
deviceRepository.saveAll(updated);
|
||||
|
||||
return updated;
|
||||
return sceneService.apply(newScene);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
|
@ -83,7 +76,7 @@ public class SceneController {
|
|||
@PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal)
|
||||
throws NotFoundException {
|
||||
final Scene newScene =
|
||||
sceneService
|
||||
sceneRepository
|
||||
.findByIdAndUsername(id, principal.getName())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
|
||||
|
@ -91,13 +84,13 @@ public class SceneController {
|
|||
newScene.setName(s.getName());
|
||||
}
|
||||
|
||||
return sceneService.save(newScene);
|
||||
return sceneRepository.save(newScene);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteById(@PathVariable("id") long id) {
|
||||
stateService.deleteAllBySceneId(id);
|
||||
sceneService.deleteById(id);
|
||||
sceneRepository.deleteById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.TriggerRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
@ -17,11 +11,11 @@ import org.springframework.stereotype.Component;
|
|||
@Component
|
||||
public class DeviceService {
|
||||
|
||||
@Autowired DeviceRepository<Device> deviceRepository;
|
||||
@Autowired AutomationRepository automationRepository;
|
||||
@Autowired SceneRepository sceneRepository;
|
||||
// @Autowired SceneService sceneService;
|
||||
@Autowired TriggerRepository<Trigger<? extends Device>> triggerRepository;
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
@Autowired private AutomationRepository automationRepository;
|
||||
@Autowired private SceneRepository sceneRepository;
|
||||
@Autowired private SceneService sceneService;
|
||||
@Autowired private TriggerRepository<Trigger<? extends Device>> triggerRepository;
|
||||
|
||||
public <T extends Device> List<T> saveAll(Collection<T> devices) {
|
||||
return devices.stream().map(this::save).collect(Collectors.toList());
|
||||
|
@ -36,14 +30,18 @@ public class DeviceService {
|
|||
triggers.stream()
|
||||
.filter(Trigger::triggered)
|
||||
.map(Trigger::getAutomationId)
|
||||
.map(t -> automationRepository.findById(t))
|
||||
.map(t -> automationRepository.findById(t).orElseThrow(IllegalStateException::new))
|
||||
.distinct()
|
||||
.map(t -> t.get().getScenes())
|
||||
.map(Automation::getScenes)
|
||||
.flatMap(Collection::stream)
|
||||
.distinct()
|
||||
.sorted(Comparator.comparing(ScenePriority::getPriority))
|
||||
.map(t -> sceneRepository.findById(t.getSceneId()))
|
||||
.forEach(SceneService::apply);
|
||||
.map(
|
||||
t ->
|
||||
sceneRepository
|
||||
.findById(t.getSceneId())
|
||||
.orElseThrow(IllegalStateException::new))
|
||||
.forEach(sceneService::apply);
|
||||
|
||||
// map activated -> automations
|
||||
// remove duplicates
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SceneService {
|
||||
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
|
||||
public List<Device> apply(Scene newScene) {
|
||||
final List<Device> updated = new ArrayList<>();
|
||||
|
||||
for (final State<?> s : newScene.getStates()) {
|
||||
s.apply();
|
||||
updated.add(s.getDevice());
|
||||
}
|
||||
deviceRepository.saveAll(updated);
|
||||
|
||||
return updated;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue