State controller, saveRequest and Repository

This commit is contained in:
omenem 2020-04-17 18:13:46 +02:00
parent e3aa44435e
commit 80088c0dc3
7 changed files with 234 additions and 5 deletions

View file

@ -0,0 +1,64 @@
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.SwitchableStateSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableStateRepository;
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.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/switchableState")
public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateService;
@GetMapping
public List<SwitchableState> findAll() {
return toList(switchableStateService.findAll());
}
@GetMapping("/{id}")
public SwitchableState findById(@PathVariable("id") long id) throws NotFoundException {
return switchableStateService.findById(id).orElseThrow(NotFoundException::new);
}
private SwitchableState save(SwitchableState initial, SwitchableStateSaveRequest ss) {
initial.setDeviceId(ss.getDeviceId());
initial.setSceneId(ss.getSceneId());
initial.setOn(ss.isOn());
return switchableStateService.save(initial);
}
@PostMapping
public SwitchableState create(@Valid @RequestBody SwitchableStateSaveRequest dl) {
return save(new SwitchableState(), dl);
}
@PutMapping
public SwitchableState update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException {
return save(
switchableStateService.findById(ss.getId()).orElseThrow(NotFoundException::new),
ss);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
switchableStateService.deleteById(id);
}
}

View file

@ -0,0 +1,67 @@
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.ThermostatStateSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatStateRepository;
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.RestController;
public class ThermostatStateController {
@RestController
@EnableAutoConfiguration
@RequestMapping("/thermostatState")
public class SwitchableStateController {
@Autowired private ThermostatStateRepository thermostatStateService;
@GetMapping
public List<ThermostatState> findAll() {
return toList(thermostatStateService.findAll());
}
@GetMapping("/{id}")
public ThermostatState findById(@PathVariable("id") long id) throws NotFoundException {
return thermostatStateService.findById(id).orElseThrow(NotFoundException::new);
}
private ThermostatState save(ThermostatState initial, ThermostatStateSaveRequest ts) {
initial.setDeviceId(ts.getDeviceId());
initial.setSceneId(ts.getSceneId());
initial.setMode(ts.getMode());
return thermostatStateService.save(initial);
}
@PostMapping
public ThermostatState create(@Valid @RequestBody ThermostatStateSaveRequest dl) {
return save(new ThermostatState(), dl);
}
@PutMapping
public ThermostatState update(@Valid @RequestBody ThermostatStateSaveRequest ts)
throws NotFoundException {
return save(
thermostatStateService.findById(ts.getId()).orElseThrow(NotFoundException::new),
ts);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
thermostatStateService.deleteById(id);
}
}
}

View file

@ -0,0 +1,43 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull;
public class SwitchableStateSaveRequest {
/** Device id (used only for update requests) */
private Long id;
@NotNull private Long deviceId;
@NotNull private Long sceneId;
@NotNull private boolean on;
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public Long getId() {
return id;
}
}

View file

@ -0,0 +1,44 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
import javax.validation.constraints.NotNull;
public class ThermostatStateSaveRequest {
/** Device id (used only for update requests) */
private Long id;
@NotNull private Long deviceId;
@NotNull private Long sceneId;
private Thermostat.Mode mode;
public Thermostat.Mode getMode() {
return mode;
}
public void setMode(Thermostat.Mode mode) {
this.mode = mode;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
public Long getId() {
return id;
}
}

View file

@ -2,8 +2,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@ -43,9 +41,7 @@ public abstract class State<D extends OutputDevice & AlterableFromState<State<D>
@NotNull
private Long sceneId;
/**
* Sets the state of the connected device to the state represented by this object.
*/
/** Sets the state of the connected device to the state represented by this object. */
public void apply() {
device.readStateAndSet(this);
}
@ -89,4 +85,13 @@ public abstract class State<D extends OutputDevice & AlterableFromState<State<D>
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
@PreRemove
public void removeDeviceAndScene() {
this.setScene(null);
this.setSceneId(null);
this.setDevice(null);
this.setDeviceId(null);
}
}

View file

@ -0,0 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SwitchableStateRepository extends StateRepository<SwitchableState> {}

View file

@ -0,0 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface ThermostatStateRepository extends StateRepository<ThermostatState> {}