// UpdateOrderAccount(Orders, Customer) => async void // Associates a previously anonymous order with a user account // PRODUCTION: This is called if someone starts an order while logged out and then logs back in afterwards. public async Task UpdateOrderAccount(Orders o, Customer c) { o.CustomerId = c.Id; o.AccountId = (await this.database.Users.Where(u => u.Id == c.AccountId).FirstOrDefaultAsync()).Id; if (this.database.Entry(o).State == EntityState.Detached){ this.database.Orders.Attach(o); } this.database.Entry(o).State = EntityState.Modified; await this.database.SaveChangesAsync(); }
// CancelOrder(Orders) => async void // Cancels all items in an order and closes the order status // PRODUCTION: This is called if someone logged in starts an order and then logs out without finalizing the order. public Task CancelOrder(Orders o) { if (o != null) { var query = (from item in this.database.OrderItems where item.OrderId == o.Id select item).ToList(); foreach (var i in query) { i.Removed = true; if (database.Entry(i).State == EntityState.Detached) { this.database.OrderItems.Attach(i); } this.database.Entry(i).State = EntityState.Modified; } o.OrderStatus = OrderStatus.Cancelled; if (database.Entry(o).State == EntityState.Detached) { this.database.Orders.Attach(o); } this.database.Entry(o).State = EntityState.Modified; } return this.database.SaveChangesAsync(); }
// StartOrder(string) => async Orders // initializes an order for a user and sets the state to Shopping // PRODUCTION: This is called any time someone reaches the Orders page without a valid cookie set public async Task<Orders> StartOrder(String custId) { // start the order Orders o = new Orders(); // try to get corresponding account if it exists Customer c = await this.database.Customers.Where(x => x.Id == custId).FirstOrDefaultAsync(); AccountUser user = null; if (c != null) { user = await this.database.Users.Where(u => u.Id == c.AccountId).FirstOrDefaultAsync(); } if (user != null) { // person is signed in supposedly o.AccountId = user.Id; } // we set the remaining parameters or the Orders object and save. o.CustomerId = custId; o.Created = DateTime.Now; o.OrderStatus = OrderStatus.Shopping; this.database.Orders.Add(o); await this.database.SaveChangesAsync(); return o; }