Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1247_-_Catch all database exceptions #1520

Merged
merged 11 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
</MudTd>
<MudTd DataLabel="Delete" Style="text-align: center">
<MudTooltip Text="Delete device">
<MudIconButton Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" OnClick="@(e => ShowDeleteDialog(context))"></MudIconButton>
<MudIconButton id="@($"delete_{context.DeviceId}")" Color="Color.Default" Icon="@Icons.Material.Filled.Delete" Size="Size.Medium" OnClick="@(e => ShowDeleteDialog(context))"></MudIconButton>
</MudTooltip>
</MudTd>
</RowTemplate>
Expand Down Expand Up @@ -269,23 +269,14 @@

internal async Task ChangeModel(IoTEdgeModelListItem edgeModel)
{
try
{
this._edgeModel = edgeModel;

if (edgeModel == null || string.IsNullOrWhiteSpace(edgeModel.ModelId))
{
return;
}
this._edgeModel = edgeModel;

}
catch (ProblemDetailsException exception)
if (edgeModel == null || string.IsNullOrWhiteSpace(edgeModel.ModelId))
{
Error?.ProcessProblemDetails(exception);
}
finally
{
await InvokeAsync(StateHasChanged);
return;
}

await InvokeAsync(StateHasChanged);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Azure.Data.Tables" Version="12.6.1" />
<PackageReference Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0" />
Expand Down
7 changes: 7 additions & 0 deletions src/AzureIoTHub.Portal.Infrastructure/PortalDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace AzureIoTHub.Portal.Infrastructure
{
using AzureIoTHub.Portal.Domain.Entities;
using EntityFramework.Exceptions.PostgreSQL;
using Microsoft.EntityFrameworkCore;


Expand Down Expand Up @@ -32,5 +33,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseExceptionProcessor();
base.OnConfiguring(optionsBuilder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.6.1" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.14.1" />
<PackageReference Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="6.0.3" />
<PackageReference Include="Hellang.Middleware.ProblemDetails" Version="6.5.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.10" NoWarn="NU1605" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace AzureIoTHub.Portal.Server.Services
using AzureIoTHub.Portal.Domain.Entities;
using Domain.Exceptions;
using Domain.Repositories;
using Microsoft.EntityFrameworkCore;

public class DeviceModelPropertiesService : IDeviceModelPropertiesService
{
Expand Down Expand Up @@ -43,16 +42,9 @@ public async Task SavePropertiesForModel(string modelId, IEnumerable<DeviceModel
{
_ = await AssertModelExists(modelId);

try
{
await this.deviceModelPropertiesRepository.SavePropertiesForModel(modelId, items);
await this.deviceModelPropertiesRepository.SavePropertiesForModel(modelId, items);

await this.unitOfWork.SaveAsync();
}
catch (DbUpdateException e)
{
throw new InternalServerErrorException($"Unable to set properties for model {modelId}", e);
}
await this.unitOfWork.SaveAsync();
}

private async Task<bool> AssertModelExists(string deviceModelId)
Expand Down
88 changes: 33 additions & 55 deletions src/AzureIoTHub.Portal.Server/Services/DeviceModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace AzureIoTHub.Portal.Server.Services
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Devices.Shared;
using Helpers;
using Microsoft.EntityFrameworkCore;

public class DeviceModelService<TListItem, TModel> : IDeviceModelService<TListItem, TModel>
where TListItem : class, IDeviceModel
Expand Down Expand Up @@ -81,83 +80,62 @@ public async Task<TModel> GetDeviceModel(string deviceModelId)

public async Task CreateDeviceModel(TModel deviceModel)
{
try
{
var deviceModelEntity = this.mapper.Map<DeviceModel>(deviceModel);
var deviceModelEntity = this.mapper.Map<DeviceModel>(deviceModel);

await this.deviceModelRepository.InsertAsync(deviceModelEntity);
await this.unitOfWork.SaveAsync();
await this.deviceModelRepository.InsertAsync(deviceModelEntity);
await this.unitOfWork.SaveAsync();

await CreateDeviceModelConfiguration(deviceModel);
}
catch (DbUpdateException e)
{
throw new InternalServerErrorException($"Unable to create the device model {deviceModel.Name}", e);
}
await CreateDeviceModelConfiguration(deviceModel);
}

public async Task UpdateDeviceModel(TModel deviceModel)
{
try
{
var deviceModelEntity = await this.deviceModelRepository.GetByIdAsync(deviceModel.ModelId);
var deviceModelEntity = await this.deviceModelRepository.GetByIdAsync(deviceModel.ModelId);

if (deviceModelEntity == null)
{
throw new ResourceNotFoundException($"The device model {deviceModel.ModelId} doesn't exist");
}
if (deviceModelEntity == null)
{
throw new ResourceNotFoundException($"The device model {deviceModel.ModelId} doesn't exist");
}

_ = this.mapper.Map(deviceModel, deviceModelEntity);
_ = this.mapper.Map(deviceModel, deviceModelEntity);

this.deviceModelRepository.Update(deviceModelEntity);
await this.unitOfWork.SaveAsync();
this.deviceModelRepository.Update(deviceModelEntity);
await this.unitOfWork.SaveAsync();

await CreateDeviceModelConfiguration(deviceModel);
}
catch (DbUpdateException e)
{
throw new InternalServerErrorException($"Unable to update the device model {deviceModel.Name}", e);
}
await CreateDeviceModelConfiguration(deviceModel);
}

public async Task DeleteDeviceModel(string deviceModelId)
{
try
{
var deviceModelEntity = await this.deviceModelRepository.GetByIdAsync(deviceModelId);
var deviceModelEntity = await this.deviceModelRepository.GetByIdAsync(deviceModelId);

if (deviceModelEntity == null)
{
return;
}
if (deviceModelEntity == null)
{
return;
}

var devices = await this.externalDeviceService.GetAllDevice();
var devices = await this.externalDeviceService.GetAllDevice();

if (devices.Items.Any(x => DeviceHelper.RetrieveTagValue(x, "modelId") == deviceModelId))
{
throw new ResourceAlreadyExistsException(
$"The device model {deviceModelId} is already in use by a device and cannot be deleted");
}
if (devices.Items.Any(x => DeviceHelper.RetrieveTagValue(x, "modelId") == deviceModelId))
{
throw new ResourceAlreadyExistsException(
$"The device model {deviceModelId} is already in use by a device and cannot be deleted");
}

var deviceModelCommands = this.deviceModelCommandRepository.GetAll().Where(command =>
var deviceModelCommands = this.deviceModelCommandRepository.GetAll().Where(command =>
command.DeviceModelId.Equals(deviceModelId, StringComparison.Ordinal)).ToList();

foreach (var deviceModelCommand in deviceModelCommands)
{
this.deviceModelCommandRepository.Delete(deviceModelCommand.Id);
}
foreach (var deviceModelCommand in deviceModelCommands)
{
this.deviceModelCommandRepository.Delete(deviceModelCommand.Id);
}

// Image deletion
await this.deviceModelImageManager.DeleteDeviceModelImageAsync(deviceModelId);
// Image deletion
await this.deviceModelImageManager.DeleteDeviceModelImageAsync(deviceModelId);

this.deviceModelRepository.Delete(deviceModelId);
this.deviceModelRepository.Delete(deviceModelId);

await this.unitOfWork.SaveAsync();
}
catch (DbUpdateException e)
{
throw new InternalServerErrorException($"Unable to delete the device model {deviceModelId}", e);
}
await this.unitOfWork.SaveAsync();
}

public Task<string> GetDeviceModelAvatar(string deviceModelId)
Expand Down
78 changes: 28 additions & 50 deletions src/AzureIoTHub.Portal.Server/Services/DeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace AzureIoTHub.Portal.Server.Services
using Managers;
using Infrastructure;
using AzureIoTHub.Portal.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Domain;
using Mappers;

Expand Down Expand Up @@ -61,74 +60,53 @@ public override async Task<DeviceDetails> GetDevice(string deviceId)

protected override async Task<DeviceDetails> CreateDeviceInDatabase(DeviceDetails device)
{
try
{
var deviceEntity = this.mapper.Map<Device>(device);
var deviceEntity = this.mapper.Map<Device>(device);

await this.deviceRepository.InsertAsync(deviceEntity);
await this.unitOfWork.SaveAsync();
await this.deviceRepository.InsertAsync(deviceEntity);
await this.unitOfWork.SaveAsync();

return device;
}
catch (DbUpdateException e)
{
throw new InternalServerErrorException($"Unable to create the device {device.DeviceName}", e);
}
return device;
}

protected override async Task<DeviceDetails> UpdateDeviceInDatabase(DeviceDetails device)
{
try
{
var deviceEntity = await this.deviceRepository.GetByIdAsync(device.DeviceID);
var deviceEntity = await this.deviceRepository.GetByIdAsync(device.DeviceID);

if (deviceEntity == null)
{
throw new ResourceNotFoundException($"The device {device.DeviceID} doesn't exist");
}
if (deviceEntity == null)
{
throw new ResourceNotFoundException($"The device {device.DeviceID} doesn't exist");
}

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

_ = this.mapper.Map(device, deviceEntity);
_ = this.mapper.Map(device, deviceEntity);

this.deviceRepository.Update(deviceEntity);
await this.unitOfWork.SaveAsync();
this.deviceRepository.Update(deviceEntity);
await this.unitOfWork.SaveAsync();

return device;
}
catch (DbUpdateException e)
{
throw new InternalServerErrorException($"Unable to update the device {device.DeviceName}", e);
}
return device;
}

protected override async Task DeleteDeviceInDatabase(string deviceId)
{
try
{
var deviceEntity = await this.deviceRepository.GetByIdAsync(deviceId);

if (deviceEntity == null)
{
return;
}

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

this.deviceRepository.Delete(deviceId);
var deviceEntity = await this.deviceRepository.GetByIdAsync(deviceId);

await this.unitOfWork.SaveAsync();
if (deviceEntity == null)
{
return;
}
catch (DbUpdateException e)

foreach (var deviceTagEntity in deviceEntity.Tags)
{
throw new InternalServerErrorException($"Unable to delete the device {deviceId}", e);
this.deviceTagValueRepository.Delete(deviceTagEntity.Id);
}

this.deviceRepository.Delete(deviceId);

await this.unitOfWork.SaveAsync();
}
}
}
Loading