feat(AddFastPriceChange)
parent
8f28afbf50
commit
e9764c6cd4
|
@ -26,7 +26,11 @@ public class ProductController : ICarterModule
|
||||||
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts))
|
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts))
|
||||||
.HasApiVersion(1.0);
|
.HasApiVersion(1.0);
|
||||||
|
|
||||||
group.MapPut("{productId}", ChangeDisplayedAsync)
|
group.MapPut("{productId}/displayed", ChangeDisplayedAsync)
|
||||||
|
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts))
|
||||||
|
.HasApiVersion(1.0);
|
||||||
|
|
||||||
|
group.MapPut("{productId}/cost", ChangeCostAsync)
|
||||||
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts))
|
.RequireAuthorization(builder => builder.AddAuthenticationSchemes("Bearer").RequireAuthenticatedUser().RequireClaim(CustomClaimType.Permission, ApplicationPermission.ManageProducts))
|
||||||
.HasApiVersion(1.0);
|
.HasApiVersion(1.0);
|
||||||
|
|
||||||
|
@ -65,6 +69,9 @@ public class ProductController : ICarterModule
|
||||||
public async Task<IResult> ChangeDisplayedAsync(Guid productId, [FromQuery] bool beDisplayed, [FromServices] IMediator mediator, CancellationToken cancellationToken)
|
public async Task<IResult> ChangeDisplayedAsync(Guid productId, [FromQuery] bool beDisplayed, [FromServices] IMediator mediator, CancellationToken cancellationToken)
|
||||||
=> TypedResults.Ok(await mediator.Send(new ChangeProductDisplayedCommand(productId, beDisplayed), cancellationToken));
|
=> TypedResults.Ok(await mediator.Send(new ChangeProductDisplayedCommand(productId, beDisplayed), cancellationToken));
|
||||||
|
|
||||||
|
public async Task<IResult> ChangeCostAsync(Guid productId, [FromQuery] double cost, [FromServices] IMediator mediator, CancellationToken cancellationToken)
|
||||||
|
=> TypedResults.Ok(await mediator.Send(new ChangeProductCostCommand(productId, cost), cancellationToken));
|
||||||
|
|
||||||
// DELETE:Delete Entity
|
// DELETE:Delete Entity
|
||||||
public async Task<IResult> Delete(Guid id, IMediator mediator, CancellationToken cancellationToken)
|
public async Task<IResult> Delete(Guid id, IMediator mediator, CancellationToken cancellationToken)
|
||||||
=> TypedResults.Ok(await mediator.Send(new DeleteProductCommand(id), cancellationToken));
|
=> TypedResults.Ok(await mediator.Send(new DeleteProductCommand(id), cancellationToken));
|
||||||
|
|
|
@ -42,6 +42,7 @@ public sealed record UpdateProductCommand(
|
||||||
List<StorageFileSDto> Files) : IRequest<bool>;
|
List<StorageFileSDto> Files) : IRequest<bool>;
|
||||||
|
|
||||||
public sealed record ChangeProductDisplayedCommand(Guid Id,bool BeDisplayed) : IRequest<bool>;
|
public sealed record ChangeProductDisplayedCommand(Guid Id,bool BeDisplayed) : IRequest<bool>;
|
||||||
|
public sealed record ChangeProductCostCommand(Guid Id,double Cost) : IRequest<bool>;
|
||||||
|
|
||||||
public sealed record DeleteProductCommand(Guid Id) : IRequest<bool>;
|
public sealed record DeleteProductCommand(Guid Id) : IRequest<bool>;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Netina.Repository.Handlers.Products;
|
||||||
|
|
||||||
|
public class ChangeProductCostCommandHandler(IRepositoryWrapper repositoryWrapper)
|
||||||
|
: IRequestHandler<ChangeProductCostCommand, bool>
|
||||||
|
{
|
||||||
|
public async Task<bool> Handle(ChangeProductCostCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
|
||||||
|
var ent = await repositoryWrapper.SetRepository<Product>()
|
||||||
|
.TableNoTracking
|
||||||
|
.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
||||||
|
if (ent == null)
|
||||||
|
throw new AppException("Product not found");
|
||||||
|
var newEnt = Product.Create(ent.PersianName, ent.EnglishName, ent.Summery, ent.ExpertCheck, ent.Tags,
|
||||||
|
ent.Warranty,
|
||||||
|
ent.BeDisplayed, request.Cost, ent.PackingCost, ent.HasExpressDelivery, ent.Stock, ent.MaxOrderCount,
|
||||||
|
ent.BrandId,
|
||||||
|
ent.CategoryId);
|
||||||
|
newEnt.CreatedAt = ent.CreatedAt;
|
||||||
|
newEnt.CreatedBy = ent.CreatedBy;
|
||||||
|
newEnt.Id = ent.Id;
|
||||||
|
|
||||||
|
repositoryWrapper.SetRepository<Product>()
|
||||||
|
.Update(newEnt);
|
||||||
|
|
||||||
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,17 +2,12 @@
|
||||||
|
|
||||||
namespace Netina.Repository.Handlers.Products;
|
namespace Netina.Repository.Handlers.Products;
|
||||||
|
|
||||||
public class ChangeProductDisplayedCommandHandler : IRequestHandler<ChangeProductDisplayedCommand,bool>
|
public class ChangeProductDisplayedCommandHandler(IRepositoryWrapper repositoryWrapper)
|
||||||
|
: IRequestHandler<ChangeProductDisplayedCommand, bool>
|
||||||
{
|
{
|
||||||
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
||||||
|
|
||||||
public ChangeProductDisplayedCommandHandler(IRepositoryWrapper repositoryWrapper)
|
|
||||||
{
|
|
||||||
_repositoryWrapper = repositoryWrapper;
|
|
||||||
}
|
|
||||||
public async Task<bool> Handle(ChangeProductDisplayedCommand request, CancellationToken cancellationToken)
|
public async Task<bool> Handle(ChangeProductDisplayedCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var ent = await _repositoryWrapper.SetRepository<Product>()
|
var ent = await repositoryWrapper.SetRepository<Product>()
|
||||||
.TableNoTracking
|
.TableNoTracking
|
||||||
.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
||||||
if (ent == null)
|
if (ent == null)
|
||||||
|
@ -26,10 +21,10 @@ public class ChangeProductDisplayedCommandHandler : IRequestHandler<ChangeProduc
|
||||||
newEnt.CreatedBy = ent.CreatedBy;
|
newEnt.CreatedBy = ent.CreatedBy;
|
||||||
newEnt.Id = ent.Id;
|
newEnt.Id = ent.Id;
|
||||||
|
|
||||||
_repositoryWrapper.SetRepository<Product>()
|
repositoryWrapper.SetRepository<Product>()
|
||||||
.Update(newEnt);
|
.Update(newEnt);
|
||||||
|
|
||||||
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue