| | 1 | | using AutoMapper; |
| | 2 | | using Despesas.Application.Abstractions; |
| | 3 | | using Despesas.Application.Dtos; |
| | 4 | | using Despesas.GlobalException.CustomExceptions; |
| | 5 | | using Despesas.GlobalException.CustomExceptions.Core; |
| | 6 | | using Despesas.Repository.UnitOfWork.Abstractions; |
| | 7 | | using Domain.Core.ValueObject; |
| | 8 | | using Domain.Entities; |
| | 9 | | using Repository.Persistency.Generic; |
| | 10 | |
|
| | 11 | | namespace Despesas.Application.Implementations; |
| | 12 | | public class ReceitaBusinessImpl<Dto> : BusinessBase<Dto, Receita> where Dto : ReceitaDto, new() |
| | 13 | | { |
| | 14 | | private readonly IRepositorio<Receita> _repositorio; |
| | 15 | | private readonly IUnitOfWork<Categoria> _unitOfWorkCategoria; |
| | 16 | | private readonly IMapper _mapper; |
| 7 | 17 | | public ReceitaBusinessImpl(IMapper mapper, IRepositorio<Receita> repositorio, IUnitOfWork<Receita> unitOfWork, IUnit |
| 7 | 18 | | { |
| 7 | 19 | | _mapper = mapper; |
| 7 | 20 | | _repositorio = repositorio; |
| 7 | 21 | | _unitOfWorkCategoria = unitOfWorkCategoria; |
| 7 | 22 | | } |
| | 23 | |
|
| | 24 | | private async Task IsValidReceita(Receita dto) |
| 3 | 25 | | { |
| 3 | 26 | | var receita = await UnitOfWork!.Repository.Get(dto.Id); |
| 3 | 27 | | if (receita == null || receita.UsuarioId != dto.UsuarioId) |
| 0 | 28 | | throw new ReceitaUsuarioInvalidaException(); |
| 3 | 29 | | } |
| | 30 | |
|
| | 31 | | private async Task IsValidCategoria(Receita dto) |
| 3 | 32 | | { |
| 3 | 33 | | var categoria = await _unitOfWorkCategoria.Repository.Get(dto.CategoriaId); |
| 3 | 34 | | if (categoria == null |
| 3 | 35 | | || categoria.UsuarioId != dto.UsuarioId |
| 3 | 36 | | || categoria == null |
| 3 | 37 | | || categoria.TipoCategoria != TipoCategoria.CategoriaType.Receita |
| 3 | 38 | | || categoria.Id != dto.CategoriaId) |
| 1 | 39 | | throw new CategoriaUsuarioInvalidaException(); |
| 2 | 40 | | } |
| | 41 | |
|
| | 42 | | public override async Task<Dto> Create(Dto dto) |
| 2 | 43 | | { |
| | 44 | | try |
| 2 | 45 | | { |
| 2 | 46 | | var receita = _mapper.Map<Receita>(dto); |
| 2 | 47 | | await IsValidCategoria(receita); |
| 1 | 48 | | await UnitOfWork.Repository.Insert(receita); |
| 1 | 49 | | await UnitOfWork.CommitAsync(); |
| 1 | 50 | | receita = await UnitOfWork.Repository.Get(receita.Id) |
| 1 | 51 | | ?? throw new CustomException("Não foi possível realizar o cadastro da receita."); |
| 1 | 52 | | return _mapper.Map<Dto>(receita); |
| | 53 | | } |
| 1 | 54 | | catch |
| 1 | 55 | | { |
| 1 | 56 | | throw; |
| | 57 | | } |
| 1 | 58 | | } |
| | 59 | |
|
| | 60 | | public override async Task<List<Dto>> FindAll(Guid idUsuario) |
| 1 | 61 | | { |
| 1 | 62 | | var result = await UnitOfWork.Repository.Find(repo => repo.UsuarioId == idUsuario); |
| 1 | 63 | | var receitas = result.ToList(); |
| 1 | 64 | | return _mapper.Map<List<Dto>>(receitas); |
| 1 | 65 | | } |
| | 66 | |
|
| | 67 | | public override async Task<Dto> FindById(Guid id, Guid idUsuario) |
| 2 | 68 | | { |
| 2 | 69 | | var receita = await UnitOfWork.Repository.Get(id); |
| 3 | 70 | | if (receita is null) return null; |
| 1 | 71 | | receita.UsuarioId = idUsuario; |
| 1 | 72 | | await IsValidReceita(receita); |
| 1 | 73 | | var receitaDto = _mapper.Map<Dto>(receita); |
| 1 | 74 | | return receitaDto; |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | public override async Task<Dto> Update(Dto dto) |
| 1 | 78 | | { |
| | 79 | | try |
| 1 | 80 | | { |
| 1 | 81 | | var receita = _mapper.Map<Receita>(dto); |
| 1 | 82 | | await IsValidReceita(receita); |
| 1 | 83 | | await IsValidCategoria(receita); |
| 1 | 84 | | await UnitOfWork.Repository.Update(receita); |
| 1 | 85 | | await UnitOfWork.CommitAsync(); |
| 1 | 86 | | receita = await UnitOfWork.Repository.Get(dto.Id.Value) |
| 1 | 87 | | ?? throw new CustomException("Não foi possível atualizar o cadastro da receita."); |
| 1 | 88 | | return _mapper.Map<Dto>(receita); |
| | 89 | | } |
| 0 | 90 | | catch |
| 0 | 91 | | { |
| 0 | 92 | | throw; |
| | 93 | | } |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | public override async Task<bool> Delete(Dto dto) |
| 1 | 97 | | { |
| | 98 | | try |
| 1 | 99 | | { |
| 1 | 100 | | Receita receita = _mapper.Map<Receita>(dto); |
| 1 | 101 | | await IsValidReceita(receita); |
| 1 | 102 | | await UnitOfWork.Repository.Delete(receita.Id); |
| 1 | 103 | | await UnitOfWork.CommitAsync(); |
| 1 | 104 | | return true; |
| | 105 | | } |
| 0 | 106 | | catch |
| 0 | 107 | | { |
| 0 | 108 | | return false; |
| | 109 | | } |
| 1 | 110 | | } |
| | 111 | | } |