From 54c27921d942da090bc2b20e7a005b9a061bcb21 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 23 Apr 2020 17:10:59 +0200 Subject: [PATCH] Fixed DeviceService --- .../smarthut/models/EagerUserRepository.java | 13 +++++++++++++ .../smarthut/models/UserRepository.java | 7 +------ .../smarthut/service/DeviceService.java | 19 +++++-------------- 3 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/EagerUserRepository.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/EagerUserRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/EagerUserRepository.java new file mode 100644 index 0000000..ac68fa3 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/EagerUserRepository.java @@ -0,0 +1,13 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.util.Optional; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.repository.CrudRepository; + +public interface EagerUserRepository extends CrudRepository { + @EntityGraph(attributePaths = {"guests"}) + User findByUsername(String username); + + @EntityGraph(attributePaths = {"guests"}) + Optional findById(Long userId); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java index d1569d6..bd93385 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/UserRepository.java @@ -1,17 +1,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.util.*; -import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository { User findByUsername(String username); - @Query("SELECT u FROM #{#entityName} u JOIN FETCH u.guests WHERE u.username = ?1") - User findByUsernameFetchGuests(String username); - - @Query("SELECT u FROM #{#entityName} u JOIN FETCH u.guests WHERE u.id = ?1") - User findByIdFetchGuests(Long id); + Optional findById(Long userId); User findByEmailIgnoreCase(String email); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index f043b05..87df762 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -21,7 +21,7 @@ public class DeviceService { @Autowired private SceneService sceneService; @Autowired private TriggerRepository> triggerRepository; @Autowired private RoomRepository roomRepository; - @Autowired private UserRepository userRepository; + @Autowired private EagerUserRepository userRepository; @Autowired private SensorSocketEndpoint endpoint; @Autowired private ThermostatService thermostatService; @@ -105,8 +105,8 @@ public class DeviceService { public T saveAsGuest(T device, String guestUsername, Long hostId) throws NotFoundException { final User currentUser = userRepository.findByUsername(guestUsername); - final User host = userRepository.findByIdFetchGuests(hostId); - if (host == null) throw new NotFoundException(); + final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); + if (!host.getGuests().contains(currentUser)) throw new NotFoundException(); final Set guests = Set.copyOf(host.getGuests()); // We're telling the host that a guest has modified a device. Therefore, fromGuest becomes @@ -132,7 +132,7 @@ public class DeviceService { } private void propagateUpdateAsOwner(Device device, String username) { - final User user = userRepository.findByUsernameFetchGuests(username); + final User user = userRepository.findByUsername(username); final Set guests = user.getGuests(); // make sure we're broadcasting from host device.setFromHost(true); @@ -177,15 +177,6 @@ public class DeviceService { .orElseThrow(NotFoundException::new); deviceRepository.delete(device); - final User user = userRepository.findByUsernameFetchGuests(username); - final Set guests = user.getGuests(); - - device.setFromHost(true); - device.setFromGuest(false); - device.setDeleted(true); - for (final User guest : guests) { - // broadcast to endpoint the object device, with receiving user set to guest - endpoint.queueDeviceUpdate(device, guest); - } + propagateUpdateAsOwner(device, username); } }