Merge branch '47-fix-cascade-deletion-when-room-is-deleted' into 'dev'

Resolve "Fix cascade deletion when room is deleted"

Closes #47

See merge request sa4-2020/the-sanmarinoes/backend!59
This commit is contained in:
Matteo Omenetti 2020-03-25 15:08:39 +01:00
commit 03bbf6b824
6 changed files with 32 additions and 5 deletions

View file

@ -23,6 +23,12 @@ public class RoomController {
@Autowired private DeviceRepository<Device> deviceRepository; @Autowired private DeviceRepository<Device> deviceRepository;
@Autowired private SwitchRepository switchRepository;
@Autowired private ButtonDimmerRepository buttonDimmerRepository;
@Autowired private KnobDimmerRepository knobDimmerRepository;
@GetMapping @GetMapping
public List<Room> findAll() { public List<Room> findAll() {
return toList(roomRepository.findAll()); return toList(roomRepository.findAll());
@ -81,6 +87,9 @@ public class RoomController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id) { public void deleteById(@PathVariable("id") long id) {
switchRepository.deleteAllByRoomId(id);
knobDimmerRepository.deleteAllByRoomId(id);
buttonDimmerRepository.deleteAllByRoomId(id);
roomRepository.deleteById(id); roomRepository.deleteById(id);
} }

View file

@ -11,7 +11,7 @@ public class DimmableLightSaveRequest {
/** The light intensity value. Goes from 0 (off) to 100 (on) */ /** The light intensity value. Goes from 0 (off) to 100 (on) */
@NotNull @NotNull
@Min(1) @Min(0)
@Max(100) @Max(100)
private Integer intensity = 0; private Integer intensity = 0;

View file

@ -1,3 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface ButtonDimmerRepository extends DeviceRepository<ButtonDimmer> {} import javax.transaction.Transactional;
public interface ButtonDimmerRepository extends DeviceRepository<ButtonDimmer> {
@Transactional
void deleteAllByRoomId(long roomId);
}

View file

@ -1,3 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface KnobDimmerRepository extends DeviceRepository<KnobDimmer> {} import javax.transaction.Transactional;
public interface KnobDimmerRepository extends DeviceRepository<KnobDimmer> {
@Transactional
void deleteAllByRoomId(long roomId);
}

View file

@ -54,7 +54,7 @@ public class Switch extends InputDevice {
} }
@PreRemove @PreRemove
private void removeSwitchable() { public void removeSwitchable() {
for (Switchable s : getOutputs()) { for (Switchable s : getOutputs()) {
s.setSwitchId(null); s.setSwitchId(null);
} }

View file

@ -1,3 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SwitchRepository extends DeviceRepository<Switch> {} import javax.transaction.Transactional;
public interface SwitchRepository extends DeviceRepository<Switch> {
@Transactional
void deleteAllByRoomId(long roomId);
}