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 39b6d39..caeac2a 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 @@ -32,12 +32,10 @@ public class ButtonDimmerController { @PostMapping public ButtonDimmer create(final ButtonDimmerSaveRequest bd) { + ButtonDimmer newBD = new ButtonDimmer(); + newBD.setLights(bd.getLights()); - ButtonDimmer toSave = new ButtonDimmer(); - - toSave.setLights(bd.getLights()); - - return buttonDimmerService.save(toSave); + return buttonDimmerService.save(newBD); } @PutMapping diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 5391e43..597a899 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -1,9 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableLightSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository; 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,16 @@ public class DimmableLightController { } @PostMapping - public DimmableLight save(@Valid @RequestBody DimmableLight dl) { - return dimmableLightService.save(dl); + public DimmableLight create(DimmableLightSaveRequest dl) { + DimmableLight newDL = new DimmableLight(); + newDL.setIntensity(dl.getIntensity()); + + return dimmableLightService.save(newDL); } @PutMapping - public DimmableLight update(@Valid @RequestBody DimmableLight dl) { - return dimmableLightService.save(dl); + public DimmableLight update(DimmableLightSaveRequest dl) { + return this.create(dl); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index 3dd53d7..a42af17 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -1,9 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; 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,16 @@ public class KnobDimmerController { } @PostMapping - public KnobDimmer save(@Valid @RequestBody KnobDimmer kd) { - return knobDimmerService.save(kd); + public KnobDimmer create(KnobDimmerSaveRequest kd) { + KnobDimmer newKD = new KnobDimmer(); + newKD.setLights(kd.getLights()); + + return knobDimmerService.save(newKD); } @PutMapping - public KnobDimmer update(@Valid @RequestBody KnobDimmer kd) { - return knobDimmerService.save(kd); + public KnobDimmer update(KnobDimmerSaveRequest kd) { + return this.create(kd); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java new file mode 100644 index 0000000..1d3a8e2 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java @@ -0,0 +1,25 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +public class DimmableLightSaveRequest { + + /** The light intensity value. Goes from 0 (off) to 100 (on) */ + @NotNull + @Min(1) + @Max(100) + private Integer intensity = 0; + + public Integer getIntensity() { + return intensity; + } + + public void setIntensity(Integer intensity) throws IllegalArgumentException { + if (intensity < 0 || intensity > 100) { + throw new IllegalArgumentException("The intensity level can't go below 0 or above 100"); + } + this.intensity = intensity; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java new file mode 100644 index 0000000..c7d5193 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.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 KnobDimmerSaveRequest { + @Lob private Set lights = new HashSet(); + + public void setLights(Set lights) { + this.lights = lights; + } + + public Set getLights() { + return lights; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java index 5c5c603..cbba6a2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -61,4 +61,12 @@ public class KnobDimmer extends Dimmer { public void clearSet() { lights.clear(); } + + public void setLights(Set lights) { + this.lights = lights; + } + + public Set getLights() { + return lights; + } }