public IHttpActionResult GetOrderById(int id) { Order order = db.Order.Find(id); OrderViewModel orderViewModel = new OrderViewModel(); orderViewModel.DishId = order.DishId; orderViewModel.Id = order.Id; orderViewModel.State = order.State; orderViewModel.UserName = order.UserName; orderViewModel.Date = order.Date; DishViewModel dish = new DishViewModel(); dish.Id = order.Dish.Id; dish.Name = order.Dish.Name; dish.Description = order.Dish.Description; dish.Price = order.Dish.Price; dish.Photo = order.Dish.Photo; orderViewModel.Dish = dish; if (order == null) { return NotFound(); } return Ok(orderViewModel); }
public List<OrderViewModel> GetOrders(string username) { var orders = new List<OrderViewModel>(); foreach (var order in db.Order.Where(order => order.UserName == username).ToList()) { OrderViewModel orderViewModel = new OrderViewModel(); orderViewModel.DishId = order.DishId; orderViewModel.Id = order.Id; orderViewModel.State = order.State; orderViewModel.UserName = order.UserName; orderViewModel.Date = order.Date; DishViewModel dish = new DishViewModel(); dish.Id = order.Dish.Id; dish.Name = order.Dish.Name; dish.Description = order.Dish.Description; dish.Price = order.Dish.Price; dish.Photo = order.Dish.Photo; orderViewModel.Dish = dish; orders.Add(orderViewModel); } return orders; }