Exemple #1
0
        public bool CreateOrder([FromBody] List <CreateOrderEntity> order)
        {
            List <OrderLineItemEntity> items = new List <OrderLineItemEntity>();

            UserEntity currentUser = _dinerContext.User.FirstOrDefault(x => x.Email == User.Identity.Name);

            decimal total = 0m;

            foreach (CreateOrderEntity item in order)
            {
                MenuEntity menuItem = _dinerContext.Menu.FirstOrDefault(x => x.Id == item.MenuId);
                items.Add(new OrderLineItemEntity {
                    Item = menuItem, Quantity = item.Quantity
                });
                total += menuItem.Price;
            }

            OrderEntity newOrder = new OrderEntity
            {
                LineItems  = items,
                StatusNew  = DateTime.Now,
                OrderTotal = total
            };

            currentUser.AddOrder(newOrder);
            _dinerContext.SaveChanges();

            EmailMessage email = new EmailMessage();

            email.ToAddresses.Add(new EmailAddress {
                Address = User.Identity.Name, Name = User.Identity.Name
            });
            email.Subject = "You have placed a new order!";
            email.Content = "Thanks for ordering! Your order will be ready shortly!";
            email.FromAddresses.Add(new EmailAddress {
                Address = "*****@*****.**", Name = "Bonnies Diner"
            });

            _emailService.Send(email);

            return(true);
        }