Scene Controller, saveRequest and Repository
This commit is contained in:
parent
495c317eb8
commit
e3aa44435e
5 changed files with 164 additions and 3 deletions
|
@ -0,0 +1,92 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
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.Device;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.List;
|
||||||
|
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.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@RequestMapping("/scene")
|
||||||
|
public class SceneController {
|
||||||
|
|
||||||
|
@Autowired SceneRepository sceneService;
|
||||||
|
@Autowired UserRepository userService;
|
||||||
|
@Autowired StateRepository stateService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<Scene> findAll() {
|
||||||
|
return toList(sceneService.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public @ResponseBody Scene findById(@PathVariable("id") long id) throws NotFoundException {
|
||||||
|
return sceneService.findById(id).orElseThrow(NotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public @ResponseBody Scene create(
|
||||||
|
@Valid @RequestBody SceneSaveRequest s, final Principal principal) {
|
||||||
|
|
||||||
|
final String username = principal.getName();
|
||||||
|
final Long userId = userService.findByUsername(username).getId();
|
||||||
|
|
||||||
|
final Scene newScene = new Scene();
|
||||||
|
|
||||||
|
newScene.setUserId(userId);
|
||||||
|
newScene.setName(s.getName());
|
||||||
|
|
||||||
|
return sceneService.save(newScene);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public @ResponseBody Scene update(
|
||||||
|
@PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
final Scene newScene =
|
||||||
|
sceneService
|
||||||
|
.findByIdAndUsername(id, principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
|
|
||||||
|
if (s.getName() != null) {
|
||||||
|
newScene.setName(s.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
return sceneService.save(newScene);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void deleteById(@PathVariable("id") long id) {
|
||||||
|
stateService.deleteAllBySceneId(id);
|
||||||
|
sceneService.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a List<State> of all Devices that are associated to a given scene (identified by its
|
||||||
|
* id).
|
||||||
|
*/
|
||||||
|
@GetMapping(path = "/{sceneId}/devices")
|
||||||
|
public List<Device> getDevices(@PathVariable("sceneId") long sceneId) {
|
||||||
|
Iterable<Device> states = stateService.findBySceneId(sceneId);
|
||||||
|
return toList(states);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import com.sun.istack.NotNull;
|
||||||
|
|
||||||
|
public class SceneSaveRequest {
|
||||||
|
|
||||||
|
/** Room identifier */
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
/** The user given name of this room (e.g. 'Master bedroom') */
|
||||||
|
@NotNull private String name;
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,12 +27,25 @@ public class Scene {
|
||||||
|
|
||||||
@OneToMany(mappedBy = "scene", orphanRemoval = true)
|
@OneToMany(mappedBy = "scene", orphanRemoval = true)
|
||||||
@GsonExclude
|
@GsonExclude
|
||||||
private Set<State> states = new HashSet<>();
|
private Set<State<?>> states = new HashSet<>();
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Column(name = "user_id", nullable = false)
|
@Column(name = "user_id", nullable = false)
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
|
/** The user given name of this room (e.g. 'Master bedroom') */
|
||||||
|
@NotNull
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -49,11 +62,11 @@ public class Scene {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<State> getStates() {
|
public Set<State<?>> getStates() {
|
||||||
return states;
|
return states;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStates(Set<State> states) {
|
public void setStates(Set<State<?>> states) {
|
||||||
this.states = states;
|
this.states = states;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface SceneRepository extends CrudRepository<Scene, Long> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a room by their id and a username
|
||||||
|
*
|
||||||
|
* @param id the scene id
|
||||||
|
* @param username a User's username
|
||||||
|
* @return an optional scene, empty if none found
|
||||||
|
*/
|
||||||
|
@Query("SELECT r FROM Room r JOIN r.user u WHERE r.id = ?1 AND u.username = ?2")
|
||||||
|
Optional<Scene> findByIdAndUsername(Long id, String username);
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
public interface StateRepository<T extends State> extends CrudRepository<T, Long> {
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
void deleteAllBySceneId(long roomId);
|
||||||
|
|
||||||
|
List<T> findBySceneId(@Param("sceneId") long sceneId);
|
||||||
|
}
|
Loading…
Reference in a new issue