|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using BrnEventos.Application.Contratos; |
| 5 | +using BrnEventos.Domain; |
| 6 | +using BrnEventos.Persistence.Contratos; |
| 7 | +using BrnEventos.Application.Dtos; |
| 8 | +using AutoMapper; |
| 9 | +using System.Linq; |
| 10 | + |
| 11 | +namespace BrnEventos.Application |
| 12 | +{ |
| 13 | + public class LoteService : ILoteService |
| 14 | + { |
| 15 | + private readonly IGeralPersist _geralPersist; |
| 16 | + |
| 17 | + private readonly ILotePersist _lotePersist; |
| 18 | + private readonly IMapper _mapper; |
| 19 | + |
| 20 | + public LoteService( |
| 21 | + IGeralPersist geralPersist, |
| 22 | + ILotePersist lotePersist, |
| 23 | + IMapper mapper |
| 24 | + ) |
| 25 | + { |
| 26 | + _geralPersist = geralPersist; |
| 27 | + _lotePersist = lotePersist; |
| 28 | + _mapper = mapper; |
| 29 | + } |
| 30 | + |
| 31 | + public async Task AddLote(int eventoId, LoteDto model) |
| 32 | + { |
| 33 | + try |
| 34 | + { |
| 35 | + var lote = _mapper.Map<Lote>(model); |
| 36 | + lote.EventoId = eventoId; |
| 37 | + |
| 38 | + _geralPersist.Add<Lote>(lote); |
| 39 | + |
| 40 | + await _geralPersist.SaveChangesAsync(); |
| 41 | + } |
| 42 | + catch (Exception ex) |
| 43 | + { |
| 44 | + throw new Exception(ex.Message); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public async Task<LoteDto[]> SaveLotes(int eventoId, LoteDto[] models) |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + var lotes = await _lotePersist.GetLotesByEventoIdAsync(eventoId); |
| 53 | + if (lotes == null) return null; |
| 54 | + |
| 55 | + foreach (var model in models) |
| 56 | + { |
| 57 | + |
| 58 | + if (model.Id == 0) |
| 59 | + { |
| 60 | + await AddLote(eventoId, model); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + var lote = lotes.FirstOrDefault(lote => lote.Id == model.Id); |
| 65 | + model.EventoId = eventoId; |
| 66 | + |
| 67 | + _mapper.Map(model, lote); |
| 68 | + |
| 69 | + _geralPersist.Update<Lote>(lote); |
| 70 | + await _geralPersist.SaveChangesAsync(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + var loteRetorno = await _lotePersist.GetLotesByEventoIdAsync(eventoId); |
| 75 | + |
| 76 | + return _mapper.Map<LoteDto[]>(loteRetorno); |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + throw new Exception(ex.Message); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public async Task<bool> DeleteLote(int eventoId, int loteId) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + var lote = await _lotePersist.GetLoteByIdsAsync(eventoId, loteId); |
| 89 | + if (lote == null) throw new Exception("Lote não encontrado!"); |
| 90 | + |
| 91 | + _geralPersist.Delete<Lote>(lote); |
| 92 | + return await _geralPersist.SaveChangesAsync(); |
| 93 | + } |
| 94 | + catch (Exception ex) |
| 95 | + { |
| 96 | + throw new Exception(ex.Message); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public async Task<LoteDto[]> GetLotesByEventoIdAsync(int eventoId) |
| 101 | + { |
| 102 | + try |
| 103 | + { |
| 104 | + var lotes = await _lotePersist.GetLotesByEventoIdAsync(eventoId); |
| 105 | + if (lotes == null) return null; |
| 106 | + |
| 107 | + var resultado = _mapper.Map<LoteDto[]>(lotes); |
| 108 | + |
| 109 | + return resultado; |
| 110 | + } |
| 111 | + catch (Exception ex) |
| 112 | + { |
| 113 | + throw new Exception(ex.Message); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public async Task<LoteDto> GetLoteByIdsAsync(int eventoId, int loteId) |
| 118 | + { |
| 119 | + try |
| 120 | + { |
| 121 | + var lote = await _lotePersist.GetLoteByIdsAsync(eventoId, loteId); |
| 122 | + if (lote == null) return null; |
| 123 | + |
| 124 | + var resultado = _mapper.Map<LoteDto>(lote); |
| 125 | + |
| 126 | + return resultado; |
| 127 | + } |
| 128 | + catch (Exception ex) |
| 129 | + { |
| 130 | + throw new Exception(ex.Message); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments