Use transactions

This commit is contained in:
lumijiez
2025-06-10 23:49:14 +03:00
parent db61171ada
commit 09f1668351
11 changed files with 73 additions and 76 deletions

View File

@@ -11,21 +11,35 @@ public class DeleteUserRoleHandler(IUnitOfWork uw) : IRequestHandler<DeleteUserR
{
public async Task<UserRoleDto?> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
{
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
await uw.BeginTransactionAsync(cancellationToken);
var userRole = new UserRole
try
{
UserId = request.Sub,
RoleId = request.RoleId
};
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
var userRole = new UserRole
{
UserId = request.Sub,
RoleId = request.RoleId
};
var removedRole = await uw.UserRoleRepository.RemoveUserRoleAsync(userRole, cancellationToken);
await uw.SaveAsync(cancellationToken);
await uw.CommitTransactionAsync(cancellationToken);
return new UserRoleDto
{
UserId = removedRole.UserId,
RoleId = removedRole.RoleId
};
}
catch
{
await uw.RollbackTransactionAsync(cancellationToken);
throw;
}
var removedRole = await uw.UserRoleRepository.RemoveUserRoleAsync(userRole, cancellationToken);
return new UserRoleDto
{
UserId = removedRole.UserId,
RoleId = removedRole.RoleId
};
}
}

View File

@@ -11,21 +11,34 @@ public class SetUserRoleHandler(IUnitOfWork uw) : IRequestHandler<SetUserRoleCom
{
public async Task<UserRoleDto?> Handle(SetUserRoleCommand request, CancellationToken cancellationToken)
{
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
await uw.BeginTransactionAsync(cancellationToken);
var userRole = new UserRole
try
{
UserId = request.Sub,
RoleId = request.RoleId
};
var addedRole = await uw.UserRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
return new UserRoleDto
var userRole = new UserRole
{
UserId = request.Sub,
RoleId = request.RoleId
};
var addedRole = await uw.UserRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
await uw.SaveAsync(cancellationToken);
await uw.CommitTransactionAsync(cancellationToken);
return new UserRoleDto
{
UserId = addedRole.UserId,
RoleId = addedRole.RoleId
};
}
catch
{
UserId = addedRole.UserId,
RoleId = addedRole.RoleId
};
await uw.RollbackTransactionAsync(cancellationToken);
throw;
}
}
}

View File

@@ -62,23 +62,21 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit
.ToListAsync(cancellationToken);
}
public async Task<Category> AddAsync(Category category, CancellationToken cancellationToken = default)
public Task<Category> AddAsync(Category category, CancellationToken cancellationToken = default)
{
category.Id = Guid.NewGuid();
category.CreatedAt = DateTime.UtcNow;
category.ModifiedAt = DateTime.UtcNow;
context.Categories.Add(category);
await context.SaveChangesAsync(cancellationToken);
return category;
return Task.FromResult(category);
}
public async Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
public Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
{
category.ModifiedAt = DateTime.UtcNow;
context.Categories.Update(category);
await context.SaveChangesAsync(cancellationToken);
return category;
return Task.FromResult(category);
}
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
@@ -87,7 +85,6 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit
if (category != null)
{
context.Categories.Remove(category);
await context.SaveChangesAsync(cancellationToken);
}
}

View File

@@ -1,6 +1,5 @@
using Imprink.Domain.Entities.Product;
using Imprink.Domain.Models;
using Imprink.Domain.Repositories;
using Imprink.Domain.Repositories.Products;
using Imprink.Infrastructure.Database;
using Microsoft.EntityFrameworkCore;
@@ -143,12 +142,11 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
return product;
}
public async Task<Product> UpdateAsync(Product product, CancellationToken cancellationToken = default)
public Task<Product> UpdateAsync(Product product, CancellationToken cancellationToken = default)
{
product.ModifiedAt = DateTime.UtcNow;
context.Products.Update(product);
await context.SaveChangesAsync(cancellationToken);
return product;
return Task.FromResult(product);
}
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
@@ -157,7 +155,6 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
if (product != null)
{
context.Products.Remove(product);
await context.SaveChangesAsync(cancellationToken);
}
}

View File

@@ -58,23 +58,21 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
.ToListAsync(cancellationToken);
}
public async Task<ProductVariant> AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
public Task<ProductVariant> AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
{
productVariant.Id = Guid.NewGuid();
productVariant.CreatedAt = DateTime.UtcNow;
productVariant.ModifiedAt = DateTime.UtcNow;
context.ProductVariants.Add(productVariant);
await context.SaveChangesAsync(cancellationToken);
return productVariant;
return Task.FromResult(productVariant);
}
public async Task<ProductVariant> UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
public Task<ProductVariant> UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
{
productVariant.ModifiedAt = DateTime.UtcNow;
context.ProductVariants.Update(productVariant);
await context.SaveChangesAsync(cancellationToken);
return productVariant;
return Task.FromResult(productVariant);
}
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
@@ -83,7 +81,6 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
if (productVariant != null)
{
context.ProductVariants.Remove(productVariant);
await context.SaveChangesAsync(cancellationToken);
}
}
@@ -112,7 +109,6 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
{
productVariant.StockQuantity = quantity;
productVariant.ModifiedAt = DateTime.UtcNow;
await context.SaveChangesAsync(cancellationToken);
}
}
}

View File

@@ -1,5 +1,4 @@
using Imprink.Domain.Entities.Users;
using Imprink.Domain.Repositories;
using Imprink.Domain.Repositories.Users;
using Imprink.Infrastructure.Database;
using Microsoft.EntityFrameworkCore;
@@ -33,17 +32,15 @@ public class UserRoleRepository(ApplicationDbContext context) : IUserRoleReposit
.FirstOrDefaultAsync(ur => ur.UserId == userId && ur.RoleId == roleId, cancellationToken);
}
public async Task<UserRole> AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
public Task<UserRole> AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
{
var ur = context.UserRole.Add(userRole);
await context.SaveChangesAsync(cancellationToken);
return ur.Entity;
return Task.FromResult(ur.Entity);
}
public async Task<UserRole> RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
public Task<UserRole> RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default)
{
var ur = context.UserRole.Remove(userRole);
await context.SaveChangesAsync(cancellationToken);
return ur.Entity;
return Task.FromResult(ur.Entity);
}
}

View File

@@ -12,7 +12,6 @@ public class CategoriesController(IMediator mediator) : ControllerBase
[HttpGet]
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetCategories([FromQuery] GetCategoriesQuery query)
{
var result = await mediator.Send(query);
return Ok(result);
return Ok(await mediator.Send(query));
}
}

View File

@@ -13,7 +13,6 @@ public class ProductVariantsController(IMediator mediator) : ControllerBase
public async Task<ActionResult<IEnumerable<ProductVariantDto>>> GetProductVariants(
[FromQuery] GetProductVariantsQuery query)
{
var result = await mediator.Send(query);
return Ok(result);
return Ok(await mediator.Send(query));
}
}

View File

@@ -17,7 +17,7 @@ public class ProductsController(IMediator mediator) : ControllerBase
public async Task<ActionResult<PagedResultDto<ProductDto>>> GetProducts(
[FromQuery] ProductFilterParameters filterParameters)
{
var result = await mediator.Send(new GetProductsQuery { FilterParameters = filterParameters });
var result = await mediator.Send(new GetProductsQuery { FilterParameters = filterParameters});
return Ok(result);
}
}

View File

@@ -27,7 +27,6 @@ public class UserController(IMediator mediator) : ControllerBase
};
await mediator.Send(new SyncUserCommand(auth0User));
return Ok("User Synced.");
}
}

View File

@@ -14,35 +14,21 @@ public class UserRoleController(IMediator mediator) : ControllerBase
[HttpGet("me")]
public async Task<IActionResult> GetMyRoles()
{
var claims = User.Claims as Claim[] ?? User.Claims.ToArray();
var sub = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
var myRoles = await mediator.Send(new GetUserRolesCommand(sub));
return Ok(myRoles);
var sub = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
return Ok(await mediator.Send(new GetUserRolesCommand(sub)));
}
[Authorize(Roles = "Admin")]
[HttpPost("set")]
public async Task<IActionResult> SetUserRole(SetUserRoleCommand command)
{
var userRole = await mediator.Send(command);
if (userRole == null)
return BadRequest();
return Ok(userRole);
return Ok(await mediator.Send(command));
}
[Authorize(Roles = "Admin")]
[HttpPost("unset")]
public async Task<IActionResult> UnsetUserRole(DeleteUserRoleCommand command)
{
var userRole = await mediator.Send(command);
if (userRole == null)
return BadRequest();
return Ok(userRole);
return Ok(await mediator.Send(command));
}
}