76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using AutoMapper;
|
|
using Project.Application.DTOs.Incoming;
|
|
using Project.Application.DTOs.Outgoing;
|
|
using Project.Application.Interfaces;
|
|
using Project.Domain.Entities;
|
|
using Project.Infrastructure.Interfaces;
|
|
|
|
namespace Project.Application.Services
|
|
{
|
|
public class ProductService : IProductService
|
|
{
|
|
// FIELDS FOR CTOR
|
|
private readonly IProductRepository _productRepository;
|
|
private readonly IMapper _mapper;
|
|
|
|
// CTOR
|
|
public ProductService(IProductRepository productRepository, IMapper mapper)
|
|
{
|
|
_productRepository = productRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
// CREATE
|
|
public async Task<Product?> AddProductAsync(CreatingProductDto creatingProductDto)
|
|
{
|
|
var product = _mapper.Map<Product>(creatingProductDto);
|
|
var created = await _productRepository.AddAsync(product);
|
|
return created;
|
|
}
|
|
|
|
// READ ALL
|
|
public async Task<IEnumerable<ReadingProductDto>> GetAllAsync()
|
|
{
|
|
var products = await _productRepository.GetAllAsync();
|
|
var readDto = _mapper.Map<IEnumerable<ReadingProductDto>>(products);
|
|
return readDto;
|
|
}
|
|
|
|
// READ BY ID
|
|
public async Task<ReadingProductDto> GetByIdAsync(int id)
|
|
{
|
|
var product = await _productRepository.GetByIdAsync(id);
|
|
var readDto = _mapper.Map<ReadingProductDto>(product);
|
|
return readDto;
|
|
}
|
|
|
|
// READ BY NAME
|
|
public async Task<ReadingProductDto> GetByNameAsync(string name)
|
|
{
|
|
var product = await _productRepository.GetByNameAsync(name);
|
|
var readDto = _mapper.Map<ReadingProductDto>(product);
|
|
return readDto;
|
|
}
|
|
|
|
// UPDATE
|
|
public async Task<bool> UpdateProductAsync(UpdatingProductDto updatingProductDto)
|
|
{
|
|
var product = _mapper.Map<Product>(updatingProductDto);
|
|
bool isUpdated = await _productRepository.UpdateAsync(product);
|
|
return isUpdated;
|
|
}
|
|
|
|
// DELETE
|
|
public async Task<bool> DeleteProductAsync(int id)
|
|
{
|
|
Product? product = await _productRepository.GetByIdAsync(id);
|
|
|
|
if (product is null)
|
|
return false;
|
|
|
|
bool isDeleted = await _productRepository.DeleteAsync(product);
|
|
return isDeleted;
|
|
}
|
|
}
|
|
}
|