-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathUpdateProductCommand.cs
51 lines (48 loc) · 1.8 KB
/
UpdateProductCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Application.Exceptions;
using Application.Interfaces.Repositories;
using Application.Wrappers;
using AutoMapper;
using Domain.Entities;
using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Application.Features.Products.Commands.UpdateProduct
{
public class UpdateProductCommand : IRequest<Response<int>>
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Rate { get; set; }
public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand, Response<int>>
{
private readonly IProductRepositoryAsync _productRepository;
private readonly IMapper _mapper;
public UpdateProductCommandHandler(IProductRepositoryAsync productRepository, IMapper mapper)
{
_productRepository = productRepository;
_mapper = mapper;
}
public async Task<Response<int>> Handle(UpdateProductCommand command, CancellationToken cancellationToken)
{
var product = await _productRepository.GetByIdAsync(command.Id);
if (product == null)
{
throw new ApiException($"Product Not Found.");
}
else
{
//product.Name = command.Name;
//product.Rate = command.Rate;
//product.Description = command.Description;
product = _mapper.Map<Product>(command);
await _productRepository.UpdateAsync(product);
return new Response<int>(product.Id);
}
}
}
}
}