diff --git a/src/Imprink.Application/Users/DeleteUserRoleHandler.cs b/src/Imprink.Application/Users/DeleteUserRoleHandler.cs index ec950fe..e30ef08 100644 --- a/src/Imprink.Application/Users/DeleteUserRoleHandler.cs +++ b/src/Imprink.Application/Users/DeleteUserRoleHandler.cs @@ -11,21 +11,35 @@ public class DeleteUserRoleHandler(IUnitOfWork uw) : IRequestHandler 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 - }; } } \ No newline at end of file diff --git a/src/Imprink.Application/Users/SetUserRoleHandler.cs b/src/Imprink.Application/Users/SetUserRoleHandler.cs index 76f5349..f077e99 100644 --- a/src/Imprink.Application/Users/SetUserRoleHandler.cs +++ b/src/Imprink.Application/Users/SetUserRoleHandler.cs @@ -11,21 +11,34 @@ public class SetUserRoleHandler(IUnitOfWork uw) : IRequestHandler 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; + } } } \ No newline at end of file diff --git a/src/Imprink.Infrastructure/Repositories/Products/CategoryRepository.cs b/src/Imprink.Infrastructure/Repositories/Products/CategoryRepository.cs index ac1f0c9..b5d9873 100644 --- a/src/Imprink.Infrastructure/Repositories/Products/CategoryRepository.cs +++ b/src/Imprink.Infrastructure/Repositories/Products/CategoryRepository.cs @@ -62,23 +62,21 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit .ToListAsync(cancellationToken); } - public async Task AddAsync(Category category, CancellationToken cancellationToken = default) + public Task 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 UpdateAsync(Category category, CancellationToken cancellationToken = default) + public Task 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); } } diff --git a/src/Imprink.Infrastructure/Repositories/Products/ProductRepository.cs b/src/Imprink.Infrastructure/Repositories/Products/ProductRepository.cs index 4b0427e..95af010 100644 --- a/src/Imprink.Infrastructure/Repositories/Products/ProductRepository.cs +++ b/src/Imprink.Infrastructure/Repositories/Products/ProductRepository.cs @@ -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 UpdateAsync(Product product, CancellationToken cancellationToken = default) + public Task 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); } } diff --git a/src/Imprink.Infrastructure/Repositories/Products/ProductVariantRepository.cs b/src/Imprink.Infrastructure/Repositories/Products/ProductVariantRepository.cs index 4b23bb5..1c892bf 100644 --- a/src/Imprink.Infrastructure/Repositories/Products/ProductVariantRepository.cs +++ b/src/Imprink.Infrastructure/Repositories/Products/ProductVariantRepository.cs @@ -58,23 +58,21 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa .ToListAsync(cancellationToken); } - public async Task AddAsync(ProductVariant productVariant, CancellationToken cancellationToken = default) + public Task 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 UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default) + public Task 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); } } } \ No newline at end of file diff --git a/src/Imprink.Infrastructure/Repositories/Users/UserRoleRepository.cs b/src/Imprink.Infrastructure/Repositories/Users/UserRoleRepository.cs index 0fc1f79..e9b04fb 100644 --- a/src/Imprink.Infrastructure/Repositories/Users/UserRoleRepository.cs +++ b/src/Imprink.Infrastructure/Repositories/Users/UserRoleRepository.cs @@ -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 AddUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default) + public Task 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 RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default) + public Task RemoveUserRoleAsync(UserRole userRole, CancellationToken cancellationToken = default) { var ur = context.UserRole.Remove(userRole); - await context.SaveChangesAsync(cancellationToken); - return ur.Entity; + return Task.FromResult(ur.Entity); } } \ No newline at end of file diff --git a/src/Imprink.WebApi/Controllers/Products/CategoriesController.cs b/src/Imprink.WebApi/Controllers/Products/CategoriesController.cs index 65be479..33a28de 100644 --- a/src/Imprink.WebApi/Controllers/Products/CategoriesController.cs +++ b/src/Imprink.WebApi/Controllers/Products/CategoriesController.cs @@ -12,7 +12,6 @@ public class CategoriesController(IMediator mediator) : ControllerBase [HttpGet] public async Task>> GetCategories([FromQuery] GetCategoriesQuery query) { - var result = await mediator.Send(query); - return Ok(result); + return Ok(await mediator.Send(query)); } } \ No newline at end of file diff --git a/src/Imprink.WebApi/Controllers/Products/ProductVariantsController.cs b/src/Imprink.WebApi/Controllers/Products/ProductVariantsController.cs index 1bacafc..95950ba 100644 --- a/src/Imprink.WebApi/Controllers/Products/ProductVariantsController.cs +++ b/src/Imprink.WebApi/Controllers/Products/ProductVariantsController.cs @@ -13,7 +13,6 @@ public class ProductVariantsController(IMediator mediator) : ControllerBase public async Task>> GetProductVariants( [FromQuery] GetProductVariantsQuery query) { - var result = await mediator.Send(query); - return Ok(result); + return Ok(await mediator.Send(query)); } } \ No newline at end of file diff --git a/src/Imprink.WebApi/Controllers/Products/ProductsController.cs b/src/Imprink.WebApi/Controllers/Products/ProductsController.cs index 63eb0f6..6567082 100644 --- a/src/Imprink.WebApi/Controllers/Products/ProductsController.cs +++ b/src/Imprink.WebApi/Controllers/Products/ProductsController.cs @@ -17,7 +17,7 @@ public class ProductsController(IMediator mediator) : ControllerBase public async Task>> 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); } } \ No newline at end of file diff --git a/src/Imprink.WebApi/Controllers/Users/UserController.cs b/src/Imprink.WebApi/Controllers/Users/UserController.cs index 75fba37..d4cba8f 100644 --- a/src/Imprink.WebApi/Controllers/Users/UserController.cs +++ b/src/Imprink.WebApi/Controllers/Users/UserController.cs @@ -27,7 +27,6 @@ public class UserController(IMediator mediator) : ControllerBase }; await mediator.Send(new SyncUserCommand(auth0User)); - return Ok("User Synced."); } } \ No newline at end of file diff --git a/src/Imprink.WebApi/Controllers/Users/UserRoleController.cs b/src/Imprink.WebApi/Controllers/Users/UserRoleController.cs index 34a2926..d6092b0 100644 --- a/src/Imprink.WebApi/Controllers/Users/UserRoleController.cs +++ b/src/Imprink.WebApi/Controllers/Users/UserRoleController.cs @@ -14,35 +14,21 @@ public class UserRoleController(IMediator mediator) : ControllerBase [HttpGet("me")] public async Task 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 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 UnsetUserRole(DeleteUserRoleCommand command) { - var userRole = await mediator.Send(command); - - if (userRole == null) - return BadRequest(); - - return Ok(userRole); + return Ok(await mediator.Send(command)); } } \ No newline at end of file