public async Task <CreatePurchaseResult> BuyBook(AddPurchaseDto purchaseDto, CancellationToken cancellationToken)
        {
            var book = await context.Books.Include(x => x.Price).Include(x => x.Discount).Where(x => x.Id == purchaseDto.BookId).FirstOrDefaultAsync();

            var customer = context.Customers.Find(purchaseDto.CustomerId);

            if (customer == null)
            {
                return(new CreatePurchaseResult(ActionStatus.Failure, "Customer not found"));
            }
            if (book == null || customer == null)
            {
                return(new CreatePurchaseResult(ActionStatus.Failure, "Book not found"));
            }
            int?discountId = null;

            if (book.Discount != null)
            {
                discountId = book.Discount.Id;
            }
            var purchase = new Purchase
                               (purchaseDto.BookId, purchaseDto.PurchaseTime, book.Price.Id, discountId, purchaseDto.CustomerId);

            context.Purchases.Add(purchase);

            await context.SaveChangesAsync();

            var purchaseEvent = new PurchaseCreatedEvent(customer.Email, book.Name, book.Price);
            await mediator.Publish(purchaseEvent, cancellationToken);

            return(new CreatePurchaseResult(ActionStatus.Success, mapper.Map <PurchaseDto>(purchase)));
        }
        public async Task <AddBookActionResult> AddBook(AddBookDto bookDto, CancellationToken cancellationToken)
        {
            var authorsInDb = GetAuthors(bookDto.Authors);
            var price       = bookDto.Price.GetFromDataBase(context);

            bookDto.AddToDatabase(context, price, authorsInDb.Authors);
            int id = 0;

            try
            {
                id = await context.SaveChangesAsync(cancellationToken);
            }
            catch (System.Exception ex)
            {
                return(new AddBookActionResult(ActionStatus.Failure, ex.Message));
            }
            if (id == 0)
            {
                return(new AddBookActionResult(ActionStatus.Failure, "Book not added in Database"));
            }
            return(new AddBookActionResult(ActionStatus.Success, new BookModel(bookDto, id)));
        }