From bf3b3730b58c2dbdb45f8cd612175ba0e4e9fa2e Mon Sep 17 00:00:00 2001 From: baranacikgoz Date: Fri, 10 Dec 2021 13:40:07 +0300 Subject: [PATCH] used Mapper for Update --- .../Commands/UpdateProduct/UpdateProductCommand.cs | 14 ++++++++++---- Application/Mappings/GeneralProfile.cs | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Application/Features/Products/Commands/UpdateProduct/UpdateProductCommand.cs b/Application/Features/Products/Commands/UpdateProduct/UpdateProductCommand.cs index d3288aa..3b4c463 100644 --- a/Application/Features/Products/Commands/UpdateProduct/UpdateProductCommand.cs +++ b/Application/Features/Products/Commands/UpdateProduct/UpdateProductCommand.cs @@ -1,6 +1,8 @@ using Application.Exceptions; using Application.Interfaces.Repositories; using Application.Wrappers; +using AutoMapper; +using Domain.Entities; using MediatR; using System; using System.Collections.Generic; @@ -19,9 +21,11 @@ public class UpdateProductCommand : IRequest> public class UpdateProductCommandHandler : IRequestHandler> { private readonly IProductRepositoryAsync _productRepository; - public UpdateProductCommandHandler(IProductRepositoryAsync productRepository) + private readonly IMapper _mapper; + public UpdateProductCommandHandler(IProductRepositoryAsync productRepository, IMapper mapper) { _productRepository = productRepository; + _mapper = mapper; } public async Task> Handle(UpdateProductCommand command, CancellationToken cancellationToken) { @@ -33,9 +37,11 @@ public async Task> Handle(UpdateProductCommand command, Cancellati } else { - product.Name = command.Name; - product.Rate = command.Rate; - product.Description = command.Description; + //product.Name = command.Name; + //product.Rate = command.Rate; + //product.Description = command.Description; + + product = _mapper.Map(command); await _productRepository.UpdateAsync(product); return new Response(product.Id); } diff --git a/Application/Mappings/GeneralProfile.cs b/Application/Mappings/GeneralProfile.cs index 2dd0788..6c1e976 100644 --- a/Application/Mappings/GeneralProfile.cs +++ b/Application/Mappings/GeneralProfile.cs @@ -1,4 +1,5 @@ using Application.Features.Products.Commands.CreateProduct; +using Application.Features.Products.Commands.UpdateProduct; using Application.Features.Products.Queries.GetAllProducts; using AutoMapper; using Domain.Entities; @@ -15,6 +16,7 @@ public GeneralProfile() CreateMap().ReverseMap(); CreateMap(); CreateMap(); + CreateMap(); } } }