WIP: controllers check for owner or guest, device service needs review

This commit is contained in:
Tommaso Rodolfo Masera 2020-04-22 15:30:23 +02:00
parent c4f295d7d9
commit a428d57fe1
3 changed files with 73 additions and 16 deletions

View file

@ -35,12 +35,18 @@ public class DimmableLightController {
return dimmableLightRepository.findById(id).orElseThrow(NotFoundException::new);
}
private DimmableLight save(DimmableLight initial, DimmableSaveRequest dl, String username) {
private DimmableLight save(
DimmableLight initial, DimmableSaveRequest dl, String username, Long hostId)
throws NotFoundException {
initial.setIntensity(dl.getIntensity());
initial.setName(dl.getName());
initial.setRoomId(dl.getRoomId());
return deviceService.saveAsOwner(initial, username);
if (hostId == null) {
return deviceService.saveAsOwner(initial, username);
} else {
return deviceService.saveAsGuest(initial, username, hostId);
}
}
/*
@ -49,8 +55,9 @@ public class DimmableLightController {
*/
@PostMapping
public DimmableLight create(
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal) {
return save(new DimmableLight(), dl, principal.getName());
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal)
throws NotFoundException {
return save(new DimmableLight(), dl, principal.getName(), null);
}
private DimmableLight fetchIfOwnerOrGuest(final Principal principal, Long id, Long hostId)
@ -79,15 +86,18 @@ public class DimmableLightController {
}
/*
Here you must behave differently if hostId is given or not:
- if not given, assume the owner of the device wants to update the device. In this case, save with DeviceService.saveAsOwner();
- if given, assume a guest wants to update the intensity of this light. In this case, save with DeviceService.saveAsGuest();
Logic for saving either as owner or guest is handled in method save of this controller
*/
@PutMapping
public DimmableLight update(
@Valid @RequestBody DimmableSaveRequest sp, final Principal principal, Long hostId)
throws NotFoundException {
return save(fetchIfOwnerOrGuest(principal, sp.getId(), hostId), sp, principal.getName());
return save(
fetchIfOwnerOrGuest(principal, sp.getId(), hostId),
sp,
principal.getName(),
hostId);
}
@DeleteMapping("/{id}")

View file

@ -63,25 +63,36 @@ public class RegularLightController {
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
}
private RegularLight save(RegularLight initial, SwitchableSaveRequest rl, String username) {
private RegularLight save(
RegularLight initial, SwitchableSaveRequest rl, String username, Long hostId)
throws NotFoundException {
initial.setName(rl.getName());
initial.setRoomId(rl.getRoomId());
initial.setOn(rl.isOn());
return deviceService.saveAsOwner(initial, username);
if (hostId == null) {
return deviceService.saveAsOwner(initial, username);
} else {
return deviceService.saveAsGuest(initial, username, hostId);
}
}
@PostMapping
public RegularLight create(
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal) {
return save(new RegularLight(), rl, principal.getName());
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal)
throws NotFoundException {
return save(new RegularLight(), rl, principal.getName(), null);
}
@PutMapping
public RegularLight update(
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal, Long hostId)
throws NotFoundException {
return save(fetchIfOwnerOrGuest(principal, rl.getId(), hostId), rl, principal.getName());
return save(
fetchIfOwnerOrGuest(principal, rl.getId(), hostId),
rl,
principal.getName(),
hostId);
}
@DeleteMapping("/{id}")

View file

@ -1,10 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
import java.beans.Transient;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -21,15 +24,48 @@ public class DeviceService {
TODO: remember to put a @Transient @GsonIgnore (but not @SocketGsonIgnore) property on device to signal a device update
TODO: coming from DeviceService.saveAsGuest()
*/
public <T extends Device> T saveAsGuest(
@Transient @GsonExclude T device, String guestUsername, Long hostId)
throws NotFoundException {
final User currentUser = userRepository.findByUsername(guestUsername);
final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new);
final Set<User> guests = host.getGuests();
// TODO: create saveAsGuest(device, guestUsername, hostId)
// filter out currentUser from guests as we do not want to broadcast an update to the
// updating user itself
if (guests.contains(currentUser)) {
guests.remove(currentUser);
}
// broadcasting from not a host
device.setFromHost(false);
// broadcast device update for host
endpoint.queueDeviceUpdate(device, host);
userRepository.save(host);
endpoint.flushDeviceUpdates();
for (final User guest : guests) {
// enqueue all device updates for all other guests
endpoint.queueDeviceUpdate(device, guest);
userRepository.save(guest);
}
// broadcast device updates for all other guests
endpoint.flushDeviceUpdates();
return deviceRepository.save(device);
}
public <T extends Device> T saveAsOwner(T device, String username) {
final User user = userRepository.findByUsername(username);
final Set<User> guests = user.getGuests();
// make sure we're broadcasting from host
device.setFromHost(true);
for (final User guest : guests) {
// set set from host true
// broadcast to endpoint the object device, with recieving user set to guest
// broadcast to endpoint the object device, with receiving user set to guest
endpoint.queueDeviceUpdate(device, guest);
userRepository.save(guest);
}
// after queueing the device update for each user, flush them all in a single message
// can be moved inside the foreach loop to send a single message for each update enqueued
endpoint.flushDeviceUpdates();
return deviceRepository.save(device);
}
}