Merge branch 'fix' into 'dev'

Fixed DeviceService

See merge request sa4-2020/the-sanmarinoes/backend!81
This commit is contained in:
Claudio Maggioni 2020-04-23 17:11:26 +02:00
commit 6a90f478d1
3 changed files with 19 additions and 20 deletions

View file

@ -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<User, Long> {
@EntityGraph(attributePaths = {"guests"})
User findByUsername(String username);
@EntityGraph(attributePaths = {"guests"})
Optional<User> findById(Long userId);
}

View file

@ -1,17 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.*; import java.util.*;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> { public interface UserRepository extends CrudRepository<User, Long> {
User findByUsername(String username); User findByUsername(String username);
@Query("SELECT u FROM #{#entityName} u JOIN FETCH u.guests WHERE u.username = ?1") Optional<User> findById(Long userId);
User findByUsernameFetchGuests(String username);
@Query("SELECT u FROM #{#entityName} u JOIN FETCH u.guests WHERE u.id = ?1")
User findByIdFetchGuests(Long id);
User findByEmailIgnoreCase(String email); User findByEmailIgnoreCase(String email);
} }

View file

@ -21,7 +21,7 @@ public class DeviceService {
@Autowired private SceneService sceneService; @Autowired private SceneService sceneService;
@Autowired private TriggerRepository<Trigger<? extends Device>> triggerRepository; @Autowired private TriggerRepository<Trigger<? extends Device>> triggerRepository;
@Autowired private RoomRepository roomRepository; @Autowired private RoomRepository roomRepository;
@Autowired private UserRepository userRepository; @Autowired private EagerUserRepository userRepository;
@Autowired private SensorSocketEndpoint endpoint; @Autowired private SensorSocketEndpoint endpoint;
@Autowired private ThermostatService thermostatService; @Autowired private ThermostatService thermostatService;
@ -105,8 +105,8 @@ public class DeviceService {
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId) public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
throws NotFoundException { throws NotFoundException {
final User currentUser = userRepository.findByUsername(guestUsername); final User currentUser = userRepository.findByUsername(guestUsername);
final User host = userRepository.findByIdFetchGuests(hostId); final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new);
if (host == null) throw new NotFoundException(); if (!host.getGuests().contains(currentUser)) throw new NotFoundException();
final Set<User> guests = Set.copyOf(host.getGuests()); final Set<User> guests = Set.copyOf(host.getGuests());
// We're telling the host that a guest has modified a device. Therefore, fromGuest becomes // 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) { private void propagateUpdateAsOwner(Device device, String username) {
final User user = userRepository.findByUsernameFetchGuests(username); final User user = userRepository.findByUsername(username);
final Set<User> guests = user.getGuests(); final Set<User> guests = user.getGuests();
// make sure we're broadcasting from host // make sure we're broadcasting from host
device.setFromHost(true); device.setFromHost(true);
@ -177,15 +177,6 @@ public class DeviceService {
.orElseThrow(NotFoundException::new); .orElseThrow(NotFoundException::new);
deviceRepository.delete(device); deviceRepository.delete(device);
final User user = userRepository.findByUsernameFetchGuests(username); propagateUpdateAsOwner(device, username);
final Set<User> 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);
}
} }
} }