Skip to content

Commit

Permalink
Fix device create/update/delete on azure iot hub #1308 (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
hocinehacherouf authored Oct 4, 2022
1 parent 2a6900e commit a100d73
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,13 @@ public async Task DeleteDevice_DeviceExist_DeviceDeleted()
.Returns(Task.CompletedTask);

_ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(new Device());
.ReturnsAsync(new Device
{
Tags = Fixture.CreateMany<DeviceTagValue>(5).ToList()
});

this.mockDeviceTagValueRepository.Setup(repository => repository.Delete(It.IsAny<string>()))
.Verifiable();

this.mockDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID))
.Verifiable();
Expand Down Expand Up @@ -475,7 +481,10 @@ public async Task DeleteDevice_DbUpdateExceptionIsRaised_InternalServerErrorExce
.Returns(Task.CompletedTask);

_ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(new Device());
.ReturnsAsync(new Device
{
Tags = new List<DeviceTagValue>()
});

this.mockDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID))
.Verifiable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,13 @@ public async Task DeleteDevice_DeviceExist_DeviceDeleted()
.Returns(Task.CompletedTask);

_ = this.mockLorawanDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(new LorawanDevice());
.ReturnsAsync(new LorawanDevice
{
Tags = Fixture.CreateMany<DeviceTagValue>(5).ToList()
});

this.mockDeviceTagValueRepository.Setup(repository => repository.Delete(It.IsAny<string>()))
.Verifiable();

this.mockLorawanDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID))
.Verifiable();
Expand Down Expand Up @@ -378,7 +384,10 @@ public async Task DeleteDevice_DbUpdateExceptionIsRaised_InternalServerErrorExce
.Returns(Task.CompletedTask);

_ = this.mockLorawanDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID))
.ReturnsAsync(new LorawanDevice());
.ReturnsAsync(new LorawanDevice
{
Tags = new List<DeviceTagValue>()
});

this.mockLorawanDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID))
.Verifiable();
Expand Down
17 changes: 8 additions & 9 deletions src/AzureIoTHub.Portal/Server/Services/DeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ public override async Task<DeviceDetails> GetDevice(string deviceId)
return deviceDto;
}

public override async Task<DeviceDetails> CreateDevice(DeviceDetails device)
protected override async Task<DeviceDetails> CreateDeviceInDatabase(DeviceDetails device)
{
_ = base.CreateDevice(device);

try
{
var deviceEntity = this.mapper.Map<Device>(device);
Expand All @@ -78,10 +76,8 @@ public override async Task<DeviceDetails> CreateDevice(DeviceDetails device)
}
}

public override async Task<DeviceDetails> UpdateDevice(DeviceDetails device)
protected override async Task<DeviceDetails> UpdateDeviceInDatabase(DeviceDetails device)
{
_ = await base.UpdateDevice(device);

try
{
var deviceEntity = await this.deviceRepository.GetByIdAsync(device.DeviceID);
Expand Down Expand Up @@ -109,10 +105,8 @@ public override async Task<DeviceDetails> UpdateDevice(DeviceDetails device)
}
}

public override async Task DeleteDevice(string deviceId)
protected override async Task DeleteDeviceInDatabase(string deviceId)
{
await base.DeleteDevice(deviceId);

try
{
var deviceEntity = await this.deviceRepository.GetByIdAsync(deviceId);
Expand All @@ -122,6 +116,11 @@ public override async Task DeleteDevice(string deviceId)
return;
}

foreach (var deviceTagEntity in deviceEntity.Tags)
{
this.deviceTagValueRepository.Delete(deviceTagEntity.Id);
}

this.deviceRepository.Delete(deviceId);

await this.unitOfWork.SaveAsync();
Expand Down
16 changes: 12 additions & 4 deletions src/AzureIoTHub.Portal/Server/Services/DeviceServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public async Task<PaginatedResult<DeviceListItem>> GetDevices(string searchText

public abstract Task<TDto> GetDevice(string deviceId);

public virtual async Task<TDto> CreateDevice(TDto device)
public async Task<TDto> CreateDevice(TDto device)
{
var newTwin = await this.externalDevicesService.CreateNewTwinFromDeviceId(device.DeviceID);

Expand All @@ -140,10 +140,12 @@ public virtual async Task<TDto> CreateDevice(TDto device)

_ = await this.externalDevicesService.CreateDeviceWithTwin(device.DeviceID, false, newTwin, status);

return device;
return await CreateDeviceInDatabase(device);
}

public virtual async Task<TDto> UpdateDevice(TDto device)
protected abstract Task<TDto> CreateDeviceInDatabase(TDto device);

public async Task<TDto> UpdateDevice(TDto device)
{
// Device status (enabled/disabled) has to be dealt with afterwards
var currentDevice = await this.externalDevicesService.GetDevice(device.DeviceID);
Expand All @@ -159,14 +161,20 @@ public virtual async Task<TDto> UpdateDevice(TDto device)

_ = await this.externalDevicesService.UpdateDeviceTwin(currentTwin);

return device;
return await UpdateDeviceInDatabase(device);
}

protected abstract Task<TDto> UpdateDeviceInDatabase(TDto device);

public virtual async Task DeleteDevice(string deviceId)
{
await this.externalDevicesService.DeleteDevice(deviceId);

await DeleteDeviceInDatabase(deviceId);
}

protected abstract Task DeleteDeviceInDatabase(string deviceId);

public virtual Task<EnrollmentCredentials> GetCredentials(string deviceId)
{
return this.externalDevicesService.GetEnrollmentCredentials(deviceId);
Expand Down
17 changes: 8 additions & 9 deletions src/AzureIoTHub.Portal/Server/Services/LoRaWanDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ public override async Task<LoRaDeviceDetails> GetDevice(string deviceId)
return deviceDto;
}

public override async Task<LoRaDeviceDetails> CreateDevice(LoRaDeviceDetails device)
protected override async Task<LoRaDeviceDetails> CreateDeviceInDatabase(LoRaDeviceDetails device)
{
_ = base.CreateDevice(device);

try
{
var deviceEntity = this.mapper.Map<LorawanDevice>(device);
Expand All @@ -79,10 +77,8 @@ public override async Task<LoRaDeviceDetails> CreateDevice(LoRaDeviceDetails dev
}
}

public override async Task<LoRaDeviceDetails> UpdateDevice(LoRaDeviceDetails device)
protected override async Task<LoRaDeviceDetails> UpdateDeviceInDatabase(LoRaDeviceDetails device)
{
_ = await base.UpdateDevice(device);

try
{
var deviceEntity = await this.lorawanDeviceRepository.GetByIdAsync(device.DeviceID);
Expand Down Expand Up @@ -110,10 +106,8 @@ public override async Task<LoRaDeviceDetails> UpdateDevice(LoRaDeviceDetails dev
}
}

public override async Task DeleteDevice(string deviceId)
protected override async Task DeleteDeviceInDatabase(string deviceId)
{
await base.DeleteDevice(deviceId);

try
{
var deviceEntity = await this.lorawanDeviceRepository.GetByIdAsync(deviceId);
Expand All @@ -123,6 +117,11 @@ public override async Task DeleteDevice(string deviceId)
return;
}

foreach (var deviceTagEntity in deviceEntity.Tags)
{
this.deviceTagValueRepository.Delete(deviceTagEntity.Id);
}

this.lorawanDeviceRepository.Delete(deviceId);

await this.unitOfWork.SaveAsync();
Expand Down

0 comments on commit a100d73

Please sign in to comment.