Exemplo n.º 1
0
        public async Task <TicketDTO> GetOrCreateTicketAsync(int discountId, User user)
        {
            var userTicket = await _ticketRepository.GetTicketAsync(discountId, user.Id);

            var discount = await _discountRepository.GetByIdAsync(discountId);

            if (userTicket is null)
            {
                await _validator.ValidateAsync(discount);

                userTicket = await _ticketRepository.CreateAndReturnTicketAsync(discountId, user);

                await _ticketRepository.SaveChangesAsync();
                await SendEmailIfAllowed(user, userTicket);
            }

            var ticketDTO = _mapper.Map <TicketDTO>(userTicket);

            ticketDTO.IsSavedDiscount = await _discountRepository.IsSavedDiscountAsync(ticketDTO.DiscountId, user.Id);

            return(ticketDTO);
        }