updated controllers

This commit is contained in:
Tommaso Rodolfo Masera 2020-03-02 12:11:50 +01:00
parent 3bbc4bcff0
commit 624c857734
6 changed files with 70 additions and 17 deletions

View file

@ -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

View file

@ -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}")

View file

@ -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}")

View file

@ -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;
}
}

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 KnobDimmerSaveRequest {
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
public void setLights(Set<DimmableLight> lights) {
this.lights = lights;
}
public Set<DimmableLight> getLights() {
return lights;
}
}

View file

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