Fixed DeviceService

This commit is contained in:
Claudio Maggioni 2020-04-23 17:10:59 +02:00
parent df8a9e6449
commit 54c27921d9
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;
import java.util.*;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {
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<User> findById(Long userId);
User findByEmailIgnoreCase(String email);
}

View file

@ -21,7 +21,7 @@ public class DeviceService {
@Autowired private SceneService sceneService;
@Autowired private TriggerRepository<Trigger<? extends Device>> 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 extends Device> 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<User> 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<User> 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<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);
}
propagateUpdateAsOwner(device, username);
}
}