예제 #1
0
        public decimal?Process(PizzaOrderModel pizzaOrderDto)
        {
            // calculate total
            var total = 10 * pizzaOrderDto.Pizzas.Count;
            //todo: add sales tax
            //todo: enable coupons and specials

            // charge credit card
            var apiKey = "MyPizza API Key";
            var api    = new StripeClient(apiKey);
            var card   = Mapper.Map <CreditCard>(pizzaOrderDto.CreditCard);

            //dynamic response = api.CreateCharge(
            //    total,
            //    "usd", //todo: expand to other currencies
            //    card);

            //todo: credit card processing isn't working yet
            dynamic response = new
            {
                IsError = false,
                Paid    = true
            };

            if (response.IsError || !response.Paid)
            {
                return(null);
            }

            return(total);
        }
        public void Send(PizzaOrderModel pizzaOrderDto)
        {
            var client      = new SmtpClient("localhost", 25);
            var from        = new MailAddress("*****@*****.**", "My Pizza");
            var to          = new MailAddress(pizzaOrderDto.EmailAddress);
            var mailMessage = new MailMessage(from, to)
            {
                Subject = "MyPizza.com Order " + pizzaOrderDto.Id,
                Body    = "We are working on your cheesy awesome!"
            };

            //todo: get an smtp server
            client.Send(mailMessage);
        }
예제 #3
0
        public async Task <int> OrderAsync(PizzaOrderModel pizzaOrderDto)
        {
            try
            {
                // validate
                if (!pizzaOrderDto.Pizzas.Any())
                {
                    throw new InvalidOperationException("An order must contain at least 1 pizza.");
                }

                if (pizzaOrderDto.Pizzas.Any(pizza => pizza.Toppings.Sum(x => x.Percentage) != 100))
                {
                    throw new InvalidOperationException("The entire pizza must be covered by toppings.");
                }

                // process the payment
                var total = paymentService.Process(pizzaOrderDto);

                // check we got paid
                if (!total.HasValue)
                {
                    throw new Exception("Payment failed. :(");
                }

                // save order in database
                var pizzaOrder = Mapper.Map <PizzaOrder>(pizzaOrderDto);
                pizzaOrder.Total = total.Value;
                context.PizzaOrders.Add(pizzaOrder);

                await context.SaveChangesAsync();

                pizzaOrderDto.Id = pizzaOrder.Id;

                // queue message for the store to make the pizza
                storeQueueService.Send(pizzaOrderDto);

                // send email to user
                orderNotificationService.Send(pizzaOrderDto);

                return(pizzaOrderDto.Id);
            }
            catch (Exception ex)
            {
                // log exception
                loggingService.LogError(ex.Message);

                throw;
            }
        }
예제 #4
0
        public void Send(PizzaOrderModel pizzaOrderDto)
        {
            // queue message for the store to make the pizza
            var factory = new ConnectionFactory {
                HostName = "localhost"
            };

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    var message = Encoding.UTF8.GetBytes(pizzaOrderDto.Id.ToString());
                    channel.BasicPublish("", "MyPizza", null, message);
                }
            }
        }
예제 #5
0
        public async Task <IActionResult> PostOrder([FromBody] PizzaOrderModel order)
        {
            this._logger.LogInformation($"{nameof(PostOrder)} called");
            if (order != null && order.Pizza != null && order.Toppings != null)
            {
                var  service = new OrdersService(_context);
                bool ret     = await service.SaveOrder(order);

                this._logger.LogInformation($"{nameof(PostOrder)} correctly loaded data");
                if (ret)
                {
                    return(Ok(ret));
                }
            }

            this._logger.LogInformation($"{nameof(PostOrder)} unexpected data provided");

            return(UnprocessableEntity());
        }
예제 #6
0
        public Task <bool> SaveOrder(PizzaOrderModel orderModel)
        {
            var order = new PizzaOrder(orderModel.Pizza);

            foreach (var topping in orderModel.Toppings)
            {
                order.Orders.Add(new PizzaOrderDetail(order, topping));
            }

            var saveOrder = new TaskCompletionSource <bool>();

            if (order.Orders.Any())
            {
                _context.PizzaOrders.Add(order);
                _context.SaveChanges();

                saveOrder.SetResult(true);
            }
            return(saveOrder.Task);
        }
예제 #7
0
        public async Task <int> OrderMenuItemAsync(int menuItem)
        {
            var order = new PizzaOrderModel
            {
                // all orders are free using our credit card
                CreditCard = new CreditCardModel
                {
                    Number   = "5555137689",
                    Cvc      = "123",
                    ExpMonth = 10,
                    ExpYear  = 2020
                },
                EmailAddress = "*****@*****.**"
            };

            var pizza = new PizzaModel();

            order.Pizzas.Add(pizza);

            switch (menuItem)
            {
            case 1:
                pizza.Toppings.Add(new PizzaToppingModel {
                    Name = "Cheese", Percentage = 100
                });
                break;

            case 2:
                pizza.Toppings.Add(new PizzaToppingModel {
                    Name = "Pepperoni", Percentage = 100
                });
                break;

            default:
                throw new Exception("Invalid menu item");
            }

            return(await OrderAsync(order));
        }
예제 #8
0
        public async Task <int> OrderAsync(PizzaOrderModel pizzaOrderDto)
        {
            try
            {
                // validate
                if (!pizzaOrderDto.Pizzas.Any())
                {
                    throw new InvalidOperationException("An order must contain at least 1 pizza.");
                }

                if (pizzaOrderDto.Pizzas.Any(pizza => pizza.Toppings.Sum(x => x.Percentage) != 100))
                {
                    throw new InvalidOperationException("The entire pizza must be covered by toppings.");
                }

                // calculate total
                var total = 10 * pizzaOrderDto.Pizzas.Count;
                //todo: add sales tax
                //todo: enable coupons and specials

                // charge credit card
                var apiKey = "MyPizza API Key";
                var api    = new StripeClient(apiKey);
                var card   = Mapper.Map <CreditCard>(pizzaOrderDto.CreditCard);

                //dynamic response = api.CreateCharge(
                //    total,
                //    "usd", //todo: expand to other currencies
                //    card);

                //todo: credit card processing isn't working yet
                dynamic response = new
                {
                    IsError = false,
                    Paid    = true
                };

                // check we got paid
                if (response.IsError || !response.Paid)
                {
                    throw new Exception("Payment failed. :(");
                }

                // save order in database
                var pizzaOrder = Mapper.Map <PizzaOrder>(pizzaOrderDto);
                pizzaOrder.Total = total;
                using (var context = new MyPizzaDbContext())
                {
                    context.PizzaOrders.Add(pizzaOrder);
                    await context.SaveChangesAsync();
                }

                // queue message for the store to make the pizza
                var factory = new ConnectionFactory {
                    HostName = "localhost"
                };
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        var message = Encoding.UTF8.GetBytes(pizzaOrder.Id.ToString());
                        channel.BasicPublish("", "MyPizza", null, message);
                    }
                }

                // send email to user
                var client      = new SmtpClient("localhost", 25);
                var from        = new MailAddress("*****@*****.**", "My Pizza");
                var to          = new MailAddress(pizzaOrderDto.EmailAddress);
                var mailMessage = new MailMessage(from, to)
                {
                    Subject = "MyPizza.com Order " + pizzaOrder.Id,
                    Body    = "We are working on your cheesy awesome!"
                };

                //todo: get an smtp server
                client.Send(mailMessage);

                return(pizzaOrder.Id);
            }
            catch (Exception ex)
            {
                // log exception
                var log = new EventLog {
                    Source = "Application"
                };
                log.WriteEntry(ex.Message, EventLogEntryType.Error);

                throw;
            }
        }