예제 #1
0
        public async Task <int> CreateOrder(Order order, ICollection <OrderItem> orderItems)
        {
            using (var context = new OrderManagementDBContext())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        context.Orders.Add(order);
                        await context.SaveChangesAsync();

                        foreach (var item in orderItems)
                        {
                            item.OrderId = order.Id;
                        }

                        context.OrderItems.AddRange(orderItems);
                        await context.SaveChangesAsync();

                        transaction.Commit();
                        return(order.Id);
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
예제 #2
0
        public async Task <ICollection <OrderDetails> > GetOrderDetails(int userId)
        {
            using (var context = new OrderManagementDBContext())
            {
                var param1       = new SqlParameter("@userId", userId);
                var orderDetails = await context.Database.SqlQuery <OrderDetails>("GetOrderDetails @userId", param1).ToListAsync();

                return(orderDetails);
            }
        }
예제 #3
0
        public async Task UpdateOrder(int orderId, int status)
        {
            using (var context = new OrderManagementDBContext())
            {
                var order = await context.Orders.FirstOrDefaultAsync(x => x.Id == orderId);

                if (order == null)
                {
                    throw new KeyNotFoundException($"{orderId} not found.");
                }

                order.OrderStatusId = status;
                await context.SaveChangesAsync();
            }
        }
예제 #4
0
 public OrderMastersController()
 {
     _context = new OrderManagementDBContext();
 }