public async Task <IActionResult> Create([Bind("ID,Email,Description,TotalPrice")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(order));
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Order).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderExists(Order.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
        public async Task<ConfirmOrder> Response(MessageDirectory.Request.ConfirmOrder request, MessageContext context)
        {
            ConfirmOrder response = new ConfirmOrder();
            this.dataContext = DataUtility.GetDataContext(dataContext);
            using (dataContext)
            { 
                var user = await dataContext.Users.SingleOrDefaultAsync(p => p.Id == request.UserId);

                if (user == null)
                {
                    Exception ex = new Exception("User Doesnt not exist");
                    ex.Data.Add("Email",request.UserId); 
                    throw ex;
                }

                var restaurant = await dataContext.Restaurants.SingleOrDefaultAsync(p => p.Id == request.RestaurantId);

                if (restaurant == null)
                {
                    Exception ex = new Exception("Restaurant Doesnt not exist");
                    ex.Data.Add("Id", request.RestaurantId);
                    throw ex;
                }

                var order = new Order()
                {
                    Amount = request.Amount.Value,
                    Address = request.Address,
                    User = user,
                    Restaurant = restaurant,
                    CreateDate = DateTime.Now,
                    PhoneNumber = request.PhoneNumber,
                    Email = request.Email,
                    City = request.City,
                    DeliveryType = (Data.Enum.DeliveryType)request.DeliveryType,
                    Products = request.Products.Select(p => new Product()
                    {
                        ProductId = p.ProductId,
                        Quantity = p.Quantity
                    }).ToList(),     
                };

                dataContext.Orders.Add(order);

                await dataContext.SaveChangesAsync();

                OrderConfirmed orderConfiremd = new OrderConfirmed()
                {
                    Id = order.Id
                };

                response.id = order.Id;

                await Bus.PublishAsync(orderConfiremd);
            }

            return response;
        }
        public async Task <IActionResult> _CreateNew(Order order)
        {
            _context.Add(order);
            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Example #5
0
 public async Task Consume(RestaurantDeleted message, MessageContext context)
 {
     this.dataContext = DataUtility.GetDataContext(dataContext);
     using (dataContext)
     {
         Restaurant restaurant = dataContext.Restaurants.SingleOrDefault(p => p.Id == message.Id);
         if (restaurant == null)
         {
             throw new Exception($"restaurant Delete : not found by Code {message.Code} by Id {message.Id}");
         }
         dataContext.Restaurants.Remove(restaurant);
         await dataContext.SaveChangesAsync();
     }
 }
Example #6
0
        public async Task Consume(UserDeleted message, MessageContext context)
        {
            this.dataContext = DataUtility.GetDataContext(dataContext);
            using (dataContext)
            {
                User user = dataContext.Users.SingleOrDefault(p => p.Email == message.Email);
                if (user == null)
                {
                    Exception ex = new Exception("User Created : not found by Email");
                    ex.Data.Add("Email", message.Email);
                    throw ex;
                }

                dataContext.Users.Remove(user);
                await dataContext.SaveChangesAsync();
            }
        }
        public async Task Consume(RestaurantCreated message, MessageContext context)
        {
            this.dataContext = DataUtility.GetDataContext(dataContext);;
            using (dataContext)
            {
                Restaurant restaurant = dataContext.Restaurants.SingleOrDefault(p => p.Id == message.Id);
                if (restaurant != null)
                {
                    return;
                }

                restaurant = new Restaurant
                {
                    Id   = message.Id,
                    Code = message.Code
                };

                dataContext.Restaurants.Add(restaurant);

                await dataContext.SaveChangesAsync();
            }
        }
Example #8
0
        public async Task Consume(UserCreated message, MessageContext context)
        {
            this.dataContext = DataUtility.GetDataContext(dataContext);
            using (dataContext)
            {
                User user = dataContext.Users.SingleOrDefault(p => p.Email == message.Email);
                if (user != null)
                {
                    return;
                }

                user = new User
                {
                    Id       = message.Id,
                    Email    = message.Email,
                    Username = message.Username
                };

                dataContext.Users.Add(user);

                await dataContext.SaveChangesAsync();
            }
        }
Example #9
0
        public async Task <UpdateOrder> Response(MessageDirectory.Request.UpdateOrder request, MessageContext context)
        {
            UpdateOrder response = new UpdateOrder();

            using (OrderDataContext dataContext = new OrderDataContext())
            {
                var order = await dataContext.Orders.SingleOrDefaultAsync(p => p.Id == request.Id);

                if (order == null)
                {
                    Exception ex = new Exception("Update Order : Does not exist");
                    ex.Data.Add("Id", request.Id);
                    throw ex;
                }

                order.Email        = request.Email;
                order.City         = request.City;
                order.DeliveryType = (Data.Enum.DeliveryType)request.DeliveryType;
                order.PhoneNumber  = request.PhoneNumber;
                order.Address      = request.Address;
                order.City         = request.City;

                dataContext.Products.RemoveRange(order.Products);
                order.Products.Clear();

                order.Products = request.Products.Select(p => new Product()
                {
                    ProductId = p.ProductId,
                    Quantity  = p.Quantity
                }).ToList();

                await dataContext.SaveChangesAsync();

                response.Id = order.Id;
                return(response);
            }
        }