This commit is contained in:
omenem 2020-04-17 18:28:42 +02:00
parent 80088c0dc3
commit 3cedaf5f50

View File

@ -19,49 +19,46 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/thermostatState")
public class ThermostatStateController { public class ThermostatStateController {
@RestController @Autowired private ThermostatStateRepository thermostatStateService;
@EnableAutoConfiguration
@RequestMapping("/thermostatState")
public class SwitchableStateController {
@Autowired private ThermostatStateRepository thermostatStateService; @GetMapping
public List<ThermostatState> findAll() {
return toList(thermostatStateService.findAll());
}
@GetMapping @GetMapping("/{id}")
public List<ThermostatState> findAll() { public ThermostatState findById(@PathVariable("id") long id) throws NotFoundException {
return toList(thermostatStateService.findAll()); return thermostatStateService.findById(id).orElseThrow(NotFoundException::new);
} }
@GetMapping("/{id}") private ThermostatState save(ThermostatState initial, ThermostatStateSaveRequest ts) {
public ThermostatState findById(@PathVariable("id") long id) throws NotFoundException { initial.setDeviceId(ts.getDeviceId());
return thermostatStateService.findById(id).orElseThrow(NotFoundException::new); initial.setSceneId(ts.getSceneId());
} initial.setMode(ts.getMode());
private ThermostatState save(ThermostatState initial, ThermostatStateSaveRequest ts) { return thermostatStateService.save(initial);
initial.setDeviceId(ts.getDeviceId()); }
initial.setSceneId(ts.getSceneId());
initial.setMode(ts.getMode());
return thermostatStateService.save(initial); @PostMapping
} public ThermostatState create(@Valid @RequestBody ThermostatStateSaveRequest dl) {
return save(new ThermostatState(), dl);
}
@PostMapping @PutMapping
public ThermostatState create(@Valid @RequestBody ThermostatStateSaveRequest dl) { public ThermostatState update(@Valid @RequestBody ThermostatStateSaveRequest ts)
return save(new ThermostatState(), dl); throws NotFoundException {
} return save(
thermostatStateService.findById(ts.getId()).orElseThrow(NotFoundException::new),
ts);
}
@PutMapping @DeleteMapping("/{id}")
public ThermostatState update(@Valid @RequestBody ThermostatStateSaveRequest ts) public void delete(@PathVariable("id") long id) {
throws NotFoundException { thermostatStateService.deleteById(id);
return save(
thermostatStateService.findById(ts.getId()).orElseThrow(NotFoundException::new),
ts);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
thermostatStateService.deleteById(id);
}
} }
} }