// DELETE api/Orders/{id} public HttpResponseMessage DeleteOrder(int id) { using (var context = new AngularReduxDemoEntities()) { var response = new HttpResponseMessage(HttpStatusCode.OK); try { var order = context.Orders.FirstOrDefault(x => x.Id == id); var mealItems = context.MealItems.Where(x => x.OrderId == id).ToList(); var drinkItems = context.DrinkItems.Where(x => x.OrderId == id).ToList(); context.MealItems.RemoveRange(mealItems); context.SaveChanges(); context.DrinkItems.RemoveRange(drinkItems); context.SaveChanges(); context.Orders.Remove(order); context.SaveChanges(); return(response); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } } }
// GET api/Tables/{id}/Orders public TableOrdersViewModel GetTableOrders(int id) { using (var context = new AngularReduxDemoEntities()) { Table table = context.Tables.FirstOrDefault(x => x.Id == id); Server server = context.Servers.FirstOrDefault(x => x.Id == table.ServerId); List <Order> orders = context.Orders.Where(x => x.TableId == id).ToList(); List <OrderDetailViewModel> orderDetails = new List <OrderDetailViewModel>(); foreach (Order order in orders) { List <MealItem> mealItems = context.MealItems.Where(x => x.OrderId == order.Id).ToList(); List <OrderItemDomainModel> mealItemsDetails = new List <OrderItemDomainModel>(); foreach (MealItem item in mealItems) { var mealItemDetail = new OrderItemDomainModel { Id = item.Id, Name = item.Name, OrderId = item.OrderId }; mealItemsDetails.Add(mealItemDetail); } List <DrinkItem> drinkItems = context.DrinkItems.Where(x => x.OrderId == order.Id).ToList(); List <OrderItemDomainModel> drinkItemsDetails = new List <OrderItemDomainModel>(); foreach (DrinkItem item in drinkItems) { var drinkItemDetail = new OrderItemDomainModel { Id = item.Id, Name = item.Name, OrderId = item.OrderId }; drinkItemsDetails.Add(drinkItemDetail); } var orderDetail = new OrderDetailViewModel { Id = order.Id, TableId = order.TableId, ServerId = order.ServerId, MealItems = mealItemsDetails, DrinkItems = drinkItemsDetails }; orderDetails.Add(orderDetail); } var tableOrdersDetails = new TableOrdersViewModel { Id = table.Id, SectionId = table.SectionId, ServerId = table.ServerId, ServerName = server.Name, Orders = orderDetails }; return(tableOrdersDetails); } }
// POST api/Orders public async Task <HttpResponseMessage> PostOrder([FromBody] OrderDetailViewModel order) { using (var context = new AngularReduxDemoEntities()) { var response = new HttpResponseMessage(HttpStatusCode.OK); try { Order newOrder = new Order { TableId = order.TableId, ServerId = order.ServerId }; context.Orders.Add(newOrder); await Task.Run(() => context.SaveChanges()); List <MealItem> mealItems = new List <MealItem>(); foreach (OrderItemDomainModel item in order.MealItems) { var mealItem = new MealItem { Name = item.Name, OrderId = newOrder.Id }; mealItems.Add(mealItem); } context.MealItems.AddRange(mealItems); context.SaveChanges(); List <DrinkItem> drinkItems = new List <DrinkItem>(); foreach (OrderItemDomainModel item in order.DrinkItems) { var drinkItem = new DrinkItem { Name = item.Name, OrderId = newOrder.Id }; drinkItems.Add(drinkItem); } context.DrinkItems.AddRange(drinkItems); context.SaveChanges(); return(response); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } } }
// GET api/Servers public IEnumerable <ServerDomainModel> GetSections() { using (var context = new AngularReduxDemoEntities()) { List <Server> servers = context.Servers.Select(x => x).ToList(); IEnumerable <ServerDomainModel> serverList = new List <ServerDomainModel>(); serverList = from server in servers select new ServerDomainModel { Id = server.Id, Name = server.Name, TableCount = server.TableCount }; return(serverList); } }
// PUT api/Sections/{id}/Update public HttpResponseMessage UpdateSection(int id, [FromBody] CountChange tableCountChange) { using (var context = new AngularReduxDemoEntities()) { var response = new HttpResponseMessage(HttpStatusCode.OK); try { var section = context.Sections.FirstOrDefault(x => x.Id == id); section.TableCount += Convert.ToInt32(tableCountChange.change); context.SaveChanges(); return(response); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } } }
// GET api/Sections public IEnumerable <SectionDetailViewModel> GetSections() { using (var context = new AngularReduxDemoEntities()) { List <Section> sections = context.Sections.Select(x => x).ToList(); List <SectionDetailViewModel> sectionList = new List <SectionDetailViewModel>(); foreach (var section in sections) { List <Table> tables = context.Tables.Where(x => x.SectionId == section.Id).ToList(); List <TableDetailViewModel> tableList = new List <TableDetailViewModel>(); foreach (var table in tables) { Server server = context.Servers.FirstOrDefault(x => x.Id == table.ServerId); List <Order> orders = context.Orders.Where(x => x.TableId == table.Id).ToList(); TableDetailViewModel tableDetail = new TableDetailViewModel { Id = table.Id, SectionId = table.SectionId, ServerId = table.ServerId, ServerName = server.Name, OrderIds = orders.Select(x => x.Id).ToList() }; tableList.Add(tableDetail); } SectionDetailViewModel sectionDetail = new SectionDetailViewModel { Id = section.Id, Name = section.Name, TableCount = section.TableCount, Tables = tableList }; sectionList.Add(sectionDetail); } return(sectionList); } }
// POST api/Tables public HttpResponseMessage PostTable([FromBody] TableDomainModel table) { using (var context = new AngularReduxDemoEntities()) { var response = new HttpResponseMessage(HttpStatusCode.OK); try { Table newTable = new Table { SectionId = table.SectionId, ServerId = table.ServerId }; context.Tables.Add(newTable); context.SaveChanges(); return(response); } catch (Exception ex) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } } }