public async Task <PurchaseDTO> CreatePurchaseAsync(CreatePurchaseDTO createPurchaseDTO) { var ticket = await _DBContext.Tickets.FirstOrDefaultAsync(t => t.Id == createPurchaseDTO.TicketId); //1 звернення до БД if (ticket == null) { throw new ItemNotFoundException($"Ticket with id {createPurchaseDTO.TicketId} not found."); } var customer = await _customerService.CreateCustomerOrUpdateIfExistAsync(createPurchaseDTO.Customer); //2 звернення до БД var purchase = new PurchaseEntity { IsAvailable = true, BarCode = Guid.NewGuid(), CustomerId = customer.Id, TicketId = ticket.Id }; await _DBContext.Purchases.AddAsync(purchase); //1 звернення до БД await _DBContext.SaveChangesAsync(); //Email розсилка string type = ""; switch (ticket.Type) { case TicketType.Festival: { type = "фестиваль"; break; } case TicketType.Parking: { type = "парковка"; break; } case TicketType.Tent: { type = "наметове містечко"; break; } } var date = ticket.BeginingTime.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture); string message = string.Format(Constants.Email.tickert, $"{customer.FirstName} {customer.LastName}", type, date, ticket.Duration, ticket.Price, purchase.BarCode.ToString()); _emailSenderService.SendEmailHtmlTextAsync(customer.Email, "Квиток на фестиваль", message); // var purchaseDTO = _mapper.Map <PurchaseDTO>(purchase); return(purchaseDTO); }