From 3bbc4bcff0e44c82370ae13076ea30ca8a77a878 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Mon, 2 Mar 2020 11:02:30 +0100 Subject: [PATCH] updated buttondimmer controller with dto --- .../controller/ButtonDimmerController.java | 16 ++++++++++------ .../smarthut/dto/ButtonDimmerSaveRequest.java | 18 ++++++++++++++++++ .../smarthut/models/ButtonDimmer.java | 8 ++++++++ .../sanmarinoes/smarthut/models/Sensor.java | 12 ++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 7c2d0b7..39b6d39 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -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}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java new file mode 100644 index 0000000..f611090 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java @@ -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 lights = new HashSet(); + + public Set getLights() { + return this.lights; + } + + public void setLights(Set newLights) { + this.lights = newLights; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java index 23a69f8..6f6def8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java @@ -54,4 +54,12 @@ public class ButtonDimmer extends Dimmer { public void clearSet() { lights.clear(); } + + public Set getLights() { + return this.lights; + } + + public void setLights(Set newLights) { + this.lights = newLights; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index e3fbae0..e352a52 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -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"); }