all controllers updated with DTOs (needs review)

This commit is contained in:
Tommaso Rodolfo Masera 2020-03-02 14:44:01 +01:00
parent 624c857734
commit eb6e935892
18 changed files with 582 additions and 21 deletions

View file

@ -34,6 +34,10 @@ public class ButtonDimmerController {
public ButtonDimmer create(final ButtonDimmerSaveRequest bd) {
ButtonDimmer newBD = new ButtonDimmer();
newBD.setLights(bd.getLights());
newBD.setId(bd.getId());
newBD.setName(bd.getName());
newBD.setRoom(bd.getRoom());
newBD.setRoomId(bd.getRoomId());
return buttonDimmerService.save(newBD);
}

View file

@ -35,6 +35,10 @@ public class DimmableLightController {
public DimmableLight create(DimmableLightSaveRequest dl) {
DimmableLight newDL = new DimmableLight();
newDL.setIntensity(dl.getIntensity());
newDL.setId(dl.getId());
newDL.setName(dl.getName());
newDL.setRoom(dl.getRoom());
newDL.setRoomId(dl.getRoomId());
return dimmableLightService.save(newDL);
}

View file

@ -35,6 +35,10 @@ public class KnobDimmerController {
public KnobDimmer create(KnobDimmerSaveRequest kd) {
KnobDimmer newKD = new KnobDimmer();
newKD.setLights(kd.getLights());
newKD.setId(kd.getId());
newKD.setName(kd.getName());
newKD.setRoom(kd.getRoom());
newKD.setRoomId(kd.getRoomId());
return knobDimmerService.save(newKD);
}

View file

@ -1,9 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.MotionSensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -11,7 +11,6 @@ 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;
@ -33,13 +32,20 @@ public class MotionSensorController {
}
@PostMapping
public MotionSensor save(@Valid @RequestBody MotionSensor ms) {
return motionSensorService.save(ms);
public MotionSensor create(MotionSensorSaveRequest ms) {
MotionSensor newMS = new MotionSensor();
newMS.setDetected(ms.isDetected());
newMS.setId(ms.getId());
newMS.setName(ms.getName());
newMS.setRoom(ms.getRoom());
newMS.setRoomId(ms.getRoomId());
return motionSensorService.save(newMS);
}
@PutMapping
public MotionSensor update(@Valid @RequestBody MotionSensor ms) {
return motionSensorService.save(ms);
public MotionSensor update(MotionSensorSaveRequest ms) {
return this.create(ms);
}
@DeleteMapping("/{id}")

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RegularLightSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import java.util.Optional;
@ -33,8 +34,15 @@ public class RegularLightController {
}
@PostMapping
public RegularLight save(@Valid @RequestBody RegularLight rl) {
return regularLightService.save(rl);
public RegularLight save(RegularLightSaveRequest rl) {
RegularLight newRL = new RegularLight();
newRL.setId(rl.getId());
newRL.setName(rl.getName());
newRL.setRoom(rl.getRoom());
newRL.setRoomId(rl.getRoomId());
newRL.setOn(rl.isOn());
return regularLightService.save(newRL);
}
@PutMapping

View file

@ -1,8 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@ -25,13 +25,21 @@ public class SensorController {
}
@PostMapping
public Sensor save(@Valid @RequestBody Sensor s) {
return sensorRepository.save(s);
public Sensor create(SensorSaveRequest s) {
Sensor newSensor = new Sensor();
newSensor.setSensor(s.getSensor());
newSensor.setValue(s.getValue());
newSensor.setId(s.getId());
newSensor.setName(s.getName());
newSensor.setRoom(s.getRoom());
newSensor.setRoomId(s.getRoomId());
return sensorRepository.save(newSensor);
}
@PutMapping
public Sensor update(@Valid @RequestBody Sensor s) {
return sensorRepository.save(s);
public Sensor update(SensorSaveRequest s) {
return this.create(s);
}
@DeleteMapping("/{id}")

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SmartPlugSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import javax.validation.Valid;
@ -25,8 +26,15 @@ public class SmartPlugController {
}
@PostMapping
public SmartPlug save(@Valid @RequestBody SmartPlug sp) {
return smartPlugRepository.save(sp);
public SmartPlug create(SmartPlugSaveRequest sp) {
SmartPlug newSP = new SmartPlug();
newSP.setOn(sp.isOn());
newSP.setId(sp.getId());
newSP.setName(sp.getName());
newSP.setRoom(sp.getRoom());
newSP.setRoomId(sp.getRoomId());
return smartPlugRepository.save(newSP);
}
@PutMapping

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import javax.validation.Valid;
@ -25,8 +26,15 @@ public class SwitchController {
}
@PostMapping
public Switch save(@Valid @RequestBody Switch s) {
return switchRepository.save(s);
public Switch save(SwitchSaveRequest s) {
Switch newSwitch = new Switch();
newSwitch.setId(s.getId());
newSwitch.setName(s.getName());
newSwitch.setRoom(s.getRoom());
newSwitch.setRoomId(s.getRoomId());
newSwitch.setOn(s.isOn());
return switchRepository.save(newSwitch);
}
@PutMapping

View file

@ -1,13 +1,30 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Lob;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
public class ButtonDimmerSaveRequest {
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public Set<DimmableLight> getLights() {
return this.lights;
}
@ -15,4 +32,36 @@ public class ButtonDimmerSaveRequest {
public void setLights(Set<DimmableLight> newLights) {
this.lights = newLights;
}
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
}

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@ -12,6 +13,53 @@ public class DimmableLightSaveRequest {
@Max(100)
private Integer intensity = 0;
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public Integer getIntensity() {
return intensity;
}

View file

@ -1,13 +1,62 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Lob;
import javax.validation.constraints.NotNull;
public class KnobDimmerSaveRequest {
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public void setLights(Set<DimmableLight> lights) {
this.lights = lights;
}

View file

@ -0,0 +1,63 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull;
public class MotionSensorSaveRequest {
private boolean detected;
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public boolean isDetected() {
return detected;
}
public void setDetected(boolean detected) {
this.detected = detected;
}
}

View file

@ -0,0 +1,64 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull;
public class RegularLightSaveRequest {
/** The state of this switch */
private boolean on;
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -0,0 +1,97 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import com.google.gson.annotations.SerializedName;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
public class SensorSaveRequest {
/** Type of sensor, i.e. of the thing the sensor measures. */
public enum SensorType {
/** A sensor that measures temperature in degrees celsius */
@SerializedName("TEMPERATURE")
TEMPERATURE,
/** A sensor that measures relative humidity in percentage points */
@SerializedName("HUMIDITY")
HUMIDITY,
/** A sensor that measures light in degrees */
@SerializedName("LIGHT")
LIGHT
}
/** The value of this sensor according to its sensor type */
private int value;
/** The type of this sensor */
@NotNull
@Enumerated(value = EnumType.STRING)
private Sensor.SensorType sensor;
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public Sensor.SensorType getSensor() {
return sensor;
}
public void setSensor(Sensor.SensorType sensor) {
this.sensor = sensor;
}
public int getValue() {
return this.value;
}
public void setValue(int newValue) {
this.value = newValue;
}
}

View file

@ -0,0 +1,64 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull;
public class SmartPlugSaveRequest {
/** Whether the smart plug is on */
@NotNull private boolean on;
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -0,0 +1,64 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull;
public class SwitchSaveRequest {
/** The state of this switch */
private boolean on;
/** Device identifier */
private long id;
/** The room this device belongs in */
private Room room;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
*/
@NotNull private Long roomId;
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
@NotNull private String name;
public void setId(long id) {
this.id = id;
}
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) {
this.roomId = roomId;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public Room getRoom() {
return room;
}
public Long getRoomId() {
return roomId;
}
public String getName() {
return name;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -1,10 +1,23 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
/** Represents a motion sensor device */
@Entity
public class MotionSensor extends InputDevice {
@Column(nullable = false)
private boolean detected;
public boolean isDetected() {
return detected;
}
public void setDetected(boolean detected) {
this.detected = detected;
}
public MotionSensor() {
super("motion-sensor");
}

View file

@ -3,7 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
/** A switch input device TODO: define switch behaviour (push button vs on/off state) */
/** A switch input device */
@Entity
public class Switch extends InputDevice {
@ -20,7 +20,7 @@ public class Switch extends InputDevice {
*
* @param state The state to be set
*/
void setState(boolean state) {
public void setOn(boolean state) {
on = state;
}
@ -29,7 +29,7 @@ public class Switch extends InputDevice {
*
* @return This Switch on state
*/
boolean getState() {
public boolean isOn() {
return on;
}
}