Fixing issues and improving coverage

This commit is contained in:
2025-03-28 23:15:53 +01:00
parent df428b9201
commit b778a357ba
4 changed files with 35 additions and 6 deletions

View File

@@ -0,0 +1,11 @@
using System;
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
public class CreateException : Exception
{
public CreateException(string message, Exception innerException)
: base(message, innerException)
{
}
}

View File

@@ -0,0 +1,11 @@
using System;
namespace BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
public class UpdateException : Exception
{
public UpdateException(string message, Exception innerException)
: base(message, innerException)
{
}
}

View File

@@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using BasicDotnetTemplate.MainProject.Core.Database; using BasicDotnetTemplate.MainProject.Core.Database;
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
using BasicDotnetTemplate.MainProject.Models.Api.Data.Role; using BasicDotnetTemplate.MainProject.Models.Api.Data.Role;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer; using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -20,6 +21,7 @@ public interface IRoleService
public class RoleService : BaseService, IRoleService public class RoleService : BaseService, IRoleService
{ {
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public RoleService( public RoleService(
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
IConfiguration configuration, IConfiguration configuration,
@@ -103,10 +105,11 @@ public class RoleService : BaseService, IRoleService
await transaction.CommitAsync(); await transaction.CommitAsync();
role = tempRole; role = tempRole;
} }
catch (Exception) catch (Exception exception)
{ {
await transaction.RollbackAsync(); await transaction.RollbackAsync();
throw; Logger.Error(exception, $"[RoleService][CreateRoleAsync]");
throw new CreateException($"An error occurred while saving the role for transaction ID {transaction.TransactionId}.", exception);
} }
return role; return role;
@@ -130,10 +133,11 @@ public class RoleService : BaseService, IRoleService
await _sqlServerContext.SaveChangesAsync(); await _sqlServerContext.SaveChangesAsync();
await transaction.CommitAsync(); await transaction.CommitAsync();
} }
catch (Exception) catch (Exception exception)
{ {
Logger.Error(exception, $"[RoleService][UpdateRoleAsync] | {transaction.TransactionId}");
await transaction.RollbackAsync(); await transaction.RollbackAsync();
throw; throw new UpdateException($"An error occurred while updating the role for transaction ID {transaction.TransactionId}.", exception);
} }
return role; return role;

View File

@@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using BasicDotnetTemplate.MainProject.Core.Database; using BasicDotnetTemplate.MainProject.Core.Database;
using BasicDotnetTemplate.MainProject.Models.Api.Common.Exceptions;
using BasicDotnetTemplate.MainProject.Models.Api.Data.User; using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer; using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -19,6 +20,7 @@ public interface IUserService
public class UserService : BaseService, IUserService public class UserService : BaseService, IUserService
{ {
private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public UserService( public UserService(
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
IConfiguration configuration, IConfiguration configuration,
@@ -115,10 +117,11 @@ public class UserService : BaseService, IUserService
await transaction.CommitAsync(); await transaction.CommitAsync();
user = tempUser; user = tempUser;
} }
catch (Exception) catch (Exception exception)
{ {
await transaction.RollbackAsync(); await transaction.RollbackAsync();
throw; Logger.Error(exception, $"[UserService][CreateUserAsync]");
throw new CreateException($"An error occurred while creating the user for transaction ID {transaction.TransactionId}.", exception);
} }