Added execpetion when a device is not found

This commit is contained in:
omenem 2020-03-04 16:55:53 +01:00
parent bff433e954
commit 2c95915333
11 changed files with 39 additions and 23 deletions

View file

@ -16,6 +16,7 @@ repositories {
dependencies {
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'com.sun.mail:javax.mail:1.6.2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-mail'

View file

@ -3,10 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -24,8 +24,8 @@ public class ButtonDimmerController {
}
@GetMapping("/{id}")
public Optional<ButtonDimmer> findById(@PathVariable("id") long id) {
return buttonDimmerService.findById(id);
public ButtonDimmer findById(@PathVariable("id") long id) throws NotFoundException {
return buttonDimmerService.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,10 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableLightSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -25,8 +25,8 @@ public class DimmableLightController {
}
@GetMapping("/{id}")
public Optional<DimmableLight> findById(@PathVariable("id") long id) {
return dimmableLightService.findById(id);
public DimmableLight findById(@PathVariable("id") long id) throws NotFoundException {
return dimmableLightService.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,10 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -25,8 +25,8 @@ public class KnobDimmerController {
}
@GetMapping("/{id}")
public Optional<KnobDimmer> findById(@PathVariable("id") long id) {
return knobDimmerService.findById(id);
public KnobDimmer findById(@PathVariable("id") long id) throws NotFoundException {
return knobDimmerService.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,10 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.MotionSensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -25,8 +25,8 @@ public class MotionSensorController {
}
@GetMapping("/{id}")
public Optional<MotionSensor> findById(@PathVariable("id") long id) {
return motionSensorService.findById(id);
public MotionSensor findById(@PathVariable("id") long id) throws NotFoundException {
return motionSensorService.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,10 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RegularLightSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -32,8 +32,8 @@ public class RegularLightController {
}
@GetMapping("/{id}")
public Optional<RegularLight> findById(@PathVariable("id") long id) {
return regularLightService.findById(id);
public RegularLight findById(@PathVariable("id") long id) throws NotFoundException {
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.*;
@ -29,8 +30,8 @@ public class RoomController {
}
@GetMapping("/{id}")
public @ResponseBody Optional<Room> findById(@PathVariable("id") long id) {
return roomRepository.findById(id);
public @ResponseBody Room findById(@PathVariable("id") long id) throws NotFoundException {
return roomRepository.findById(id).orElseThrow(NotFoundException::new);
}
private Room save(final RoomSaveRequest r, final Principal principal, boolean setWhenNull) {

View file

@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import java.util.List;
@ -24,8 +25,8 @@ public class SensorController {
}
@GetMapping("/{id}")
public Optional<Sensor> findById(@PathVariable("id") long id) {
return sensorRepository.findById(id);
public Sensor findById(@PathVariable("id") long id) throws NotFoundException {
return sensorRepository.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SmartPlugSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import java.util.List;
@ -24,8 +25,8 @@ public class SmartPlugController {
}
@GetMapping("/{id}")
public Optional<SmartPlug> findById(@PathVariable("id") long id) {
return smartPlugRepository.findById(id);
public SmartPlug findById(@PathVariable("id") long id) throws NotFoundException {
return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import java.util.List;
@ -24,8 +25,8 @@ public class SwitchController {
}
@GetMapping("/{id}")
public Optional<Switch> findById(@PathVariable("id") long id) {
return switchRepository.findById(id);
public Switch findById(@PathVariable("id") long id) throws NotFoundException {
return switchRepository.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping

View file

@ -0,0 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.error;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class NotFoundException extends Exception {
public NotFoundException() {
super("Not Found");
}
}