Project/Project.Application/Services/CategoryService.cs
2024-06-28 15:15:15 +02:00

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 CategoryService : ICategoryService
{
// FIELDS FOR CTOR
private readonly ICategoryRepository _categoryRepository;
private readonly IMapper _mapper;
// CTOR
public CategoryService(ICategoryRepository categoryService, IMapper mapper)
{
_categoryRepository = categoryService;
_mapper = mapper;
}
// CREATE
public async Task<Category?> AddCategoryAsync(CreatingCategoryDto creatingCategoryDto)
{
var category = _mapper.Map<Category>(creatingCategoryDto);
var created = await _categoryRepository.AddAsync(category);
return created;
}
// READ ALL
public async Task<IEnumerable<ReadingCategoryDto>> GetAllAsync()
{
var categories = await _categoryRepository.GetAllAsync();
var readDto = _mapper.Map<IEnumerable<ReadingCategoryDto>>(categories);
return readDto;
}
// READ BY ID
public async Task<ReadingCategoryDto> GetByIdAsync(int id)
{
var category = await _categoryRepository.GetByIdAsync(id);
var readDto = _mapper.Map<ReadingCategoryDto>(category);
return readDto;
}
// READ BY NAME
public async Task<ReadingCategoryDto> GetByNameAsync(string name)
{
var category = await _categoryRepository.GetByNameAsync(name);
var readDto = _mapper.Map<ReadingCategoryDto>(category);
return readDto;
}
// UPDATE
public async Task<bool> UpdateCategoryAsync(UpdatingCategoryDto updatingCategoryDto)
{
var category = _mapper.Map<Category>(updatingCategoryDto);
bool isUpdated = await _categoryRepository.UpdateAsync(category);
return isUpdated;
}
// DELETE
public async Task<bool> DeleteCategoryAsync(int id)
{
Category? category = await _categoryRepository.GetByIdAsync(id);
if (category is null)
return false;
bool isDeleted = await _categoryRepository.DeleteAsync(category);
return isDeleted;
}
}
}