Merge branch '65-fix-in-automation-controller' into 'dev'
fix Closes #65 See merge request sa4-2020/the-sanmarinoes/backend!93
This commit is contained in:
commit
fb229b03f0
3 changed files with 49 additions and 22 deletions
|
@ -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<Automation> 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}")
|
||||
|
|
|
@ -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<Trigger<?>> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Automation, Long> {}
|
||||
public interface AutomationRepository extends CrudRepository<Automation, Long> {
|
||||
|
||||
List<Automation> findAllByUserId(@Param("userId") long userId);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue