Merge branch 'name-fix' into 'dev'

fix duplicate device names

See merge request sa4-2020/the-sanmarinoes/backend!87
This commit is contained in:
Claudio Maggioni 2020-04-25 22:52:14 +02:00
commit 8be7b2c263
3 changed files with 19 additions and 3 deletions

View file

@ -29,7 +29,7 @@ public class BooleanTriggerController {
return booleanTriggerRepository.findAllByAutomationId(automationId); return booleanTriggerRepository.findAllByAutomationId(automationId);
} }
private BooleanTrigger<?> save(BooleanTrigger newRL, BooleanTriggerSaveRequest s) { private BooleanTrigger<?> save(BooleanTrigger<?> newRL, BooleanTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn()); newRL.setOn(s.isOn());
@ -40,7 +40,7 @@ public class BooleanTriggerController {
@PostMapping @PostMapping
public BooleanTrigger<?> create( public BooleanTrigger<?> create(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanTrigger(), booleanTriggerSaveRequest); return save(new BooleanTrigger<>(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping

View file

@ -14,6 +14,10 @@ import org.springframework.data.repository.query.Param;
public interface DeviceRepository<T extends Device> extends CrudRepository<T, Long> { public interface DeviceRepository<T extends Device> extends CrudRepository<T, Long> {
List<T> findByRoomId(@Param("roomId") long roomId); List<T> findByRoomId(@Param("roomId") long roomId);
@Query(
"SELECT COUNT(d) FROM Device d JOIN d.room r JOIN r.user u WHERE d.name = ?1 AND u.username = ?2")
Integer findDuplicates(String name, String username);
/** /**
* Finds devices by their id and a username * Finds devices by their id and a username
* *

View file

@ -29,7 +29,15 @@ public class DeviceService {
roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new); roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new);
} }
public void triggerTriggers(Device device) { private void renameIfDuplicate(Device toCreate, String username) {
while (deviceRepository.findDuplicates(toCreate.getName(), username)
- (toCreate.getId() <= 0 ? 0 : 1)
> 0) {
toCreate.setName(toCreate.getName() + " (new)");
}
}
private void triggerTriggers(Device device) {
final long deviceId = device.getId(); final long deviceId = device.getId();
@ -107,6 +115,8 @@ public class DeviceService {
final User currentUser = userRepository.findByUsername(guestUsername); final User currentUser = userRepository.findByUsername(guestUsername);
final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new);
if (!host.getGuests().contains(currentUser)) throw new NotFoundException(); if (!host.getGuests().contains(currentUser)) throw new NotFoundException();
renameIfDuplicate(device, host.getUsername());
device = deviceRepository.save(device);
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
@ -145,6 +155,7 @@ public class DeviceService {
public <T extends Device> List<T> saveAllAsOwner( public <T extends Device> List<T> saveAllAsOwner(
Iterable<T> devices, String username, boolean fromScene) { Iterable<T> devices, String username, boolean fromScene) {
devices.forEach(d -> renameIfDuplicate(d, username));
devices = deviceRepository.saveAll(devices); devices = deviceRepository.saveAll(devices);
devices.forEach((d) -> propagateUpdateAsOwner(d, username)); devices.forEach((d) -> propagateUpdateAsOwner(d, username));
@ -156,6 +167,7 @@ public class DeviceService {
} }
public <T extends Device> T saveAsOwner(T device, String username, boolean fromScene) { public <T extends Device> T saveAsOwner(T device, String username, boolean fromScene) {
renameIfDuplicate(device, username);
device = deviceRepository.save(device); device = deviceRepository.save(device);
propagateUpdateAsOwner(device, username); propagateUpdateAsOwner(device, username);