Use transactions
This commit is contained in:
@@ -11,21 +11,35 @@ public class DeleteUserRoleHandler(IUnitOfWork uw) : IRequestHandler<DeleteUserR
|
|||||||
{
|
{
|
||||||
public async Task<UserRoleDto?> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
|
public async Task<UserRoleDto?> Handle(DeleteUserRoleCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
|
await uw.BeginTransactionAsync(cancellationToken);
|
||||||
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
|
|
||||||
|
|
||||||
var userRole = new UserRole
|
try
|
||||||
{
|
{
|
||||||
UserId = request.Sub,
|
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
|
||||||
RoleId = request.RoleId
|
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
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,21 +11,34 @@ public class SetUserRoleHandler(IUnitOfWork uw) : IRequestHandler<SetUserRoleCom
|
|||||||
{
|
{
|
||||||
public async Task<UserRoleDto?> Handle(SetUserRoleCommand request, CancellationToken cancellationToken)
|
public async Task<UserRoleDto?> Handle(SetUserRoleCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
|
await uw.BeginTransactionAsync(cancellationToken);
|
||||||
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
|
|
||||||
|
|
||||||
var userRole = new UserRole
|
try
|
||||||
{
|
{
|
||||||
UserId = request.Sub,
|
if (!await uw.UserRepository.UserExistsAsync(request.Sub, cancellationToken))
|
||||||
RoleId = request.RoleId
|
throw new NotFoundException("User with ID: " + request.Sub + " does not exist.");
|
||||||
};
|
|
||||||
|
|
||||||
var addedRole = await uw.UserRoleRepository.AddUserRoleAsync(userRole, cancellationToken);
|
|
||||||
|
|
||||||
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,
|
await uw.RollbackTransactionAsync(cancellationToken);
|
||||||
RoleId = addedRole.RoleId
|
throw;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,23 +62,21 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit
|
|||||||
.ToListAsync(cancellationToken);
|
.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.Id = Guid.NewGuid();
|
||||||
category.CreatedAt = DateTime.UtcNow;
|
category.CreatedAt = DateTime.UtcNow;
|
||||||
category.ModifiedAt = DateTime.UtcNow;
|
category.ModifiedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
context.Categories.Add(category);
|
context.Categories.Add(category);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(category);
|
||||||
return category;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
|
public Task<Category> UpdateAsync(Category category, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
category.ModifiedAt = DateTime.UtcNow;
|
category.ModifiedAt = DateTime.UtcNow;
|
||||||
context.Categories.Update(category);
|
context.Categories.Update(category);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(category);
|
||||||
return category;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||||
@@ -87,7 +85,6 @@ public class CategoryRepository(ApplicationDbContext context) : ICategoryReposit
|
|||||||
if (category != null)
|
if (category != null)
|
||||||
{
|
{
|
||||||
context.Categories.Remove(category);
|
context.Categories.Remove(category);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using Imprink.Domain.Entities.Product;
|
using Imprink.Domain.Entities.Product;
|
||||||
using Imprink.Domain.Models;
|
using Imprink.Domain.Models;
|
||||||
using Imprink.Domain.Repositories;
|
|
||||||
using Imprink.Domain.Repositories.Products;
|
using Imprink.Domain.Repositories.Products;
|
||||||
using Imprink.Infrastructure.Database;
|
using Imprink.Infrastructure.Database;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -143,12 +142,11 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
|
|||||||
return product;
|
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;
|
product.ModifiedAt = DateTime.UtcNow;
|
||||||
context.Products.Update(product);
|
context.Products.Update(product);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(product);
|
||||||
return product;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||||
@@ -157,7 +155,6 @@ public class ProductRepository(ApplicationDbContext context) : IProductRepositor
|
|||||||
if (product != null)
|
if (product != null)
|
||||||
{
|
{
|
||||||
context.Products.Remove(product);
|
context.Products.Remove(product);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,23 +58,21 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
|
|||||||
.ToListAsync(cancellationToken);
|
.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.Id = Guid.NewGuid();
|
||||||
productVariant.CreatedAt = DateTime.UtcNow;
|
productVariant.CreatedAt = DateTime.UtcNow;
|
||||||
productVariant.ModifiedAt = DateTime.UtcNow;
|
productVariant.ModifiedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
context.ProductVariants.Add(productVariant);
|
context.ProductVariants.Add(productVariant);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(productVariant);
|
||||||
return productVariant;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ProductVariant> UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
public Task<ProductVariant> UpdateAsync(ProductVariant productVariant, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
productVariant.ModifiedAt = DateTime.UtcNow;
|
productVariant.ModifiedAt = DateTime.UtcNow;
|
||||||
context.ProductVariants.Update(productVariant);
|
context.ProductVariants.Update(productVariant);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(productVariant);
|
||||||
return productVariant;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default)
|
||||||
@@ -83,7 +81,6 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
|
|||||||
if (productVariant != null)
|
if (productVariant != null)
|
||||||
{
|
{
|
||||||
context.ProductVariants.Remove(productVariant);
|
context.ProductVariants.Remove(productVariant);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +109,6 @@ public class ProductVariantRepository(ApplicationDbContext context) : IProductVa
|
|||||||
{
|
{
|
||||||
productVariant.StockQuantity = quantity;
|
productVariant.StockQuantity = quantity;
|
||||||
productVariant.ModifiedAt = DateTime.UtcNow;
|
productVariant.ModifiedAt = DateTime.UtcNow;
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Imprink.Domain.Entities.Users;
|
using Imprink.Domain.Entities.Users;
|
||||||
using Imprink.Domain.Repositories;
|
|
||||||
using Imprink.Domain.Repositories.Users;
|
using Imprink.Domain.Repositories.Users;
|
||||||
using Imprink.Infrastructure.Database;
|
using Imprink.Infrastructure.Database;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -33,17 +32,15 @@ public class UserRoleRepository(ApplicationDbContext context) : IUserRoleReposit
|
|||||||
.FirstOrDefaultAsync(ur => ur.UserId == userId && ur.RoleId == roleId, cancellationToken);
|
.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);
|
var ur = context.UserRole.Add(userRole);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(ur.Entity);
|
||||||
return 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);
|
var ur = context.UserRole.Remove(userRole);
|
||||||
await context.SaveChangesAsync(cancellationToken);
|
return Task.FromResult(ur.Entity);
|
||||||
return ur.Entity;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,6 @@ public class CategoriesController(IMediator mediator) : ControllerBase
|
|||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetCategories([FromQuery] GetCategoriesQuery query)
|
public async Task<ActionResult<IEnumerable<CategoryDto>>> GetCategories([FromQuery] GetCategoriesQuery query)
|
||||||
{
|
{
|
||||||
var result = await mediator.Send(query);
|
return Ok(await mediator.Send(query));
|
||||||
return Ok(result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,6 @@ public class ProductVariantsController(IMediator mediator) : ControllerBase
|
|||||||
public async Task<ActionResult<IEnumerable<ProductVariantDto>>> GetProductVariants(
|
public async Task<ActionResult<IEnumerable<ProductVariantDto>>> GetProductVariants(
|
||||||
[FromQuery] GetProductVariantsQuery query)
|
[FromQuery] GetProductVariantsQuery query)
|
||||||
{
|
{
|
||||||
var result = await mediator.Send(query);
|
return Ok(await mediator.Send(query));
|
||||||
return Ok(result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,7 @@ public class ProductsController(IMediator mediator) : ControllerBase
|
|||||||
public async Task<ActionResult<PagedResultDto<ProductDto>>> GetProducts(
|
public async Task<ActionResult<PagedResultDto<ProductDto>>> GetProducts(
|
||||||
[FromQuery] ProductFilterParameters filterParameters)
|
[FromQuery] ProductFilterParameters filterParameters)
|
||||||
{
|
{
|
||||||
var result = await mediator.Send(new GetProductsQuery { FilterParameters = filterParameters });
|
var result = await mediator.Send(new GetProductsQuery { FilterParameters = filterParameters});
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,7 +27,6 @@ public class UserController(IMediator mediator) : ControllerBase
|
|||||||
};
|
};
|
||||||
|
|
||||||
await mediator.Send(new SyncUserCommand(auth0User));
|
await mediator.Send(new SyncUserCommand(auth0User));
|
||||||
|
|
||||||
return Ok("User Synced.");
|
return Ok("User Synced.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,35 +14,21 @@ public class UserRoleController(IMediator mediator) : ControllerBase
|
|||||||
[HttpGet("me")]
|
[HttpGet("me")]
|
||||||
public async Task<IActionResult> GetMyRoles()
|
public async Task<IActionResult> GetMyRoles()
|
||||||
{
|
{
|
||||||
var claims = User.Claims as Claim[] ?? User.Claims.ToArray();
|
var sub = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
|
||||||
var sub = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value ?? string.Empty;
|
return Ok(await mediator.Send(new GetUserRolesCommand(sub)));
|
||||||
|
|
||||||
var myRoles = await mediator.Send(new GetUserRolesCommand(sub));
|
|
||||||
|
|
||||||
return Ok(myRoles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize(Roles = "Admin")]
|
[Authorize(Roles = "Admin")]
|
||||||
[HttpPost("set")]
|
[HttpPost("set")]
|
||||||
public async Task<IActionResult> SetUserRole(SetUserRoleCommand command)
|
public async Task<IActionResult> SetUserRole(SetUserRoleCommand command)
|
||||||
{
|
{
|
||||||
var userRole = await mediator.Send(command);
|
return Ok(await mediator.Send(command));
|
||||||
|
|
||||||
if (userRole == null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
return Ok(userRole);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Authorize(Roles = "Admin")]
|
[Authorize(Roles = "Admin")]
|
||||||
[HttpPost("unset")]
|
[HttpPost("unset")]
|
||||||
public async Task<IActionResult> UnsetUserRole(DeleteUserRoleCommand command)
|
public async Task<IActionResult> UnsetUserRole(DeleteUserRoleCommand command)
|
||||||
{
|
{
|
||||||
var userRole = await mediator.Send(command);
|
return Ok(await mediator.Send(command));
|
||||||
|
|
||||||
if (userRole == null)
|
|
||||||
return BadRequest();
|
|
||||||
|
|
||||||
return Ok(userRole);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user