From 719bde08406d4defb168b536c2255c1f9363c284 Mon Sep 17 00:00:00 2001 From: omenem Date: Mon, 27 Apr 2020 13:55:59 +0200 Subject: [PATCH] fix --- .../controller/AutomationController.java | 38 +++++++++---------- .../smarthut/models/Automation.java | 26 +++++++++++++ .../smarthut/models/AutomationRepository.java | 7 +++- 3 files changed, 49 insertions(+), 22 deletions(-) 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 445bb83..7abd499 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 @@ -6,10 +6,9 @@ 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 ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; 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; @@ -31,24 +30,15 @@ public class AutomationController { @Autowired private AutomationRepository automationRepository; @Autowired private SceneRepository sceneRepository; @Autowired private ScenePriorityRepository scenePriorityRepository; + @Autowired private UserRepository userService; @GetMapping public List getAll( - @RequestParam(value = "hostId", required = false) Long hostId, final Principal user) + @RequestParam(value = "hostId", required = false) Long hostId, + final Principal principal) 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()); + final Long userId = userService.findByUsername(principal.getName()).getId(); + return automationRepository.findAllByUserId(userId); } @GetMapping("/{id}") @@ -56,25 +46,31 @@ public class AutomationController { return automationRepository.findById(id).orElseThrow(NotFoundException::new); } - private Automation save(Automation newRL, AutomationSaveRequest s) { + private Automation save(Automation newRL, AutomationSaveRequest s, Principal principal) { + + final Long userId = userService.findByUsername(principal.getName()).getId(); newRL.setName(s.getName()); + newRL.setUserId(userId); return automationRepository.save(newRL); } @PostMapping - public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) { - return save(new Automation(), automationSaveRequest); + public Automation create( + @Valid @RequestBody AutomationSaveRequest automationSaveRequest, Principal principal) { + return save(new Automation(), automationSaveRequest, principal); } @PutMapping - public Automation update(@Valid @RequestBody AutomationSaveRequest automation) + public Automation update( + @Valid @RequestBody AutomationSaveRequest automation, Principal principal) throws NotFoundException { return save( automationRepository .findById(automation.getId()) .orElseThrow(NotFoundException::new), - automation); + automation, + principal); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java index 27bda84..17d5bfc 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Automation.java @@ -17,6 +17,16 @@ public class Automation { @ApiModelProperty(hidden = true) private long id; + @ManyToOne + @JoinColumn(name = "user_id", updatable = false, insertable = false) + @GsonExclude + private User user; + + @NotNull + @Column(name = "user_id", nullable = false) + @GsonExclude + private Long userId; + @OneToMany(mappedBy = "automation", orphanRemoval = true) @GsonExclude private Set> triggers = new HashSet<>(); @@ -50,4 +60,20 @@ public class Automation { public void setName(String name) { this.name = name; } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java index b8b2ac2..a54a9ee 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/AutomationRepository.java @@ -1,5 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.List; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; -public interface AutomationRepository extends CrudRepository {} +public interface AutomationRepository extends CrudRepository { + + List findAllByUserId(@Param("userId") long userId); +}