From e3aa44435e9d1786a121b5f095583da610508f36 Mon Sep 17 00:00:00 2001 From: omenem Date: Fri, 17 Apr 2020 17:03:21 +0200 Subject: [PATCH] Scene Controller, saveRequest and Repository --- .../smarthut/controller/SceneController.java | 92 +++++++++++++++++++ .../smarthut/dto/SceneSaveRequest.java | 24 +++++ .../sanmarinoes/smarthut/models/Scene.java | 19 +++- .../smarthut/models/SceneRepository.java | 18 ++++ .../smarthut/models/StateRepository.java | 14 +++ 5 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SceneRepository.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java new file mode 100644 index 0000000..0cbf2d7 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java @@ -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 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 of all Devices that are associated to a given scene (identified by its + * id). + */ + @GetMapping(path = "/{sceneId}/devices") + public List getDevices(@PathVariable("sceneId") long sceneId) { + Iterable states = stateService.findBySceneId(sceneId); + return toList(states); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java new file mode 100644 index 0000000..ea3c6bd --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SceneSaveRequest.java @@ -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; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java index d006430..3d026b7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java @@ -27,12 +27,25 @@ public class Scene { @OneToMany(mappedBy = "scene", orphanRemoval = true) @GsonExclude - private Set states = new HashSet<>(); + private Set> states = new HashSet<>(); @NotNull @Column(name = "user_id", nullable = false) 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() { return id; } @@ -49,11 +62,11 @@ public class Scene { this.user = user; } - public Set getStates() { + public Set> getStates() { return states; } - public void setStates(Set states) { + public void setStates(Set> states) { this.states = states; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SceneRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SceneRepository.java new file mode 100644 index 0000000..513ed9c --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SceneRepository.java @@ -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 { + + /** + * 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 findByIdAndUsername(Long id, String username); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java new file mode 100644 index 0000000..4cf31f9 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java @@ -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 extends CrudRepository { + + @Transactional + void deleteAllBySceneId(long roomId); + + List findBySceneId(@Param("sceneId") long sceneId); +}