updated buttondimmer controller with dto

This commit is contained in:
Tommaso Rodolfo Masera 2020-03-02 11:02:30 +01:00
parent 1cf3b1088e
commit 3bbc4bcff0
4 changed files with 48 additions and 6 deletions

View file

@ -1,9 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
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;
@ -32,13 +31,18 @@ public class ButtonDimmerController {
}
@PostMapping
public ButtonDimmer save(@Valid @RequestBody ButtonDimmer bd) {
return buttonDimmerService.save(bd);
public ButtonDimmer create(final ButtonDimmerSaveRequest bd) {
ButtonDimmer toSave = new ButtonDimmer();
toSave.setLights(bd.getLights());
return buttonDimmerService.save(toSave);
}
@PutMapping
public ButtonDimmer update(@Valid @RequestBody ButtonDimmer bd) {
return buttonDimmerService.save(bd);
public ButtonDimmer update(ButtonDimmerSaveRequest bd) {
return this.create(bd);
}
@DeleteMapping("/{id}")

View file

@ -0,0 +1,18 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Lob;
public class ButtonDimmerSaveRequest {
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
public Set<DimmableLight> getLights() {
return this.lights;
}
public void setLights(Set<DimmableLight> newLights) {
this.lights = newLights;
}
}

View file

@ -54,4 +54,12 @@ public class ButtonDimmer extends Dimmer {
public void clearSet() {
lights.clear();
}
public Set<DimmableLight> getLights() {
return this.lights;
}
public void setLights(Set<DimmableLight> newLights) {
this.lights = newLights;
}
}

View file

@ -26,6 +26,10 @@ public class Sensor extends InputDevice {
LIGHT
}
/** The value of this sensor according to its sensor type */
@Column(nullable = false)
private int value;
/** The type of this sensor */
@Column(nullable = false)
@NotNull
@ -40,6 +44,14 @@ public class Sensor extends InputDevice {
this.sensor = sensor;
}
public int getValue() {
return this.value;
}
public void setValue(int newValue) {
this.value = newValue;
}
public Sensor() {
super("sensor");
}