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:
Matteo Omenetti 2020-04-27 14:00:47 +02:00
commit fb229b03f0
3 changed files with 49 additions and 22 deletions

View file

@ -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.AutomationRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; 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.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import java.security.Principal; import java.security.Principal;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid; 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;
@ -31,24 +30,15 @@ public class AutomationController {
@Autowired private AutomationRepository automationRepository; @Autowired private AutomationRepository automationRepository;
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private ScenePriorityRepository scenePriorityRepository; @Autowired private ScenePriorityRepository scenePriorityRepository;
@Autowired private UserRepository userService;
@GetMapping @GetMapping
public List<Automation> getAll( 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 { throws NotFoundException {
return sceneRepository final Long userId = userService.findByUsername(principal.getName()).getId();
.findByUsername(user.getName()) return automationRepository.findAllByUserId(userId);
.stream()
.map(s -> scenePriorityRepository.findAllBySceneId(s.getId()))
.flatMap(Collection::stream)
.distinct()
.map(
t ->
automationRepository
.findById(t.getAutomationId())
.orElseThrow(IllegalStateException::new))
.distinct()
.collect(Collectors.toList());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -56,25 +46,31 @@ public class AutomationController {
return automationRepository.findById(id).orElseThrow(NotFoundException::new); 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.setName(s.getName());
newRL.setUserId(userId);
return automationRepository.save(newRL); return automationRepository.save(newRL);
} }
@PostMapping @PostMapping
public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) { public Automation create(
return save(new Automation(), automationSaveRequest); @Valid @RequestBody AutomationSaveRequest automationSaveRequest, Principal principal) {
return save(new Automation(), automationSaveRequest, principal);
} }
@PutMapping @PutMapping
public Automation update(@Valid @RequestBody AutomationSaveRequest automation) public Automation update(
@Valid @RequestBody AutomationSaveRequest automation, Principal principal)
throws NotFoundException { throws NotFoundException {
return save( return save(
automationRepository automationRepository
.findById(automation.getId()) .findById(automation.getId())
.orElseThrow(NotFoundException::new), .orElseThrow(NotFoundException::new),
automation); automation,
principal);
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View file

@ -17,6 +17,16 @@ public class Automation {
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private long id; 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) @OneToMany(mappedBy = "automation", orphanRemoval = true)
@GsonExclude @GsonExclude
private Set<Trigger<?>> triggers = new HashSet<>(); private Set<Trigger<?>> triggers = new HashSet<>();
@ -50,4 +60,20 @@ public class Automation {
public void setName(String name) { public void setName(String name) {
this.name = 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;
}
} }

View file

@ -1,5 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List;
import org.springframework.data.repository.CrudRepository; 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);
}