private static IList<RestaurantMenuModel> MapMenuGroups(IEnumerable<MenuItem> menuItems,
                                                                      User user, bool onlyStarred, bool allergyFree)
        {
            var starredItems = user.StarredMenuItems.Select(x => x.MenuItemId).ToArray();

            var groups = (onlyStarred
                              ? menuItems.Where(menuItem => (starredItems.Any(f => f == menuItem.Id)))
                              : menuItems)
                .OrderBy(menuItem => menuItem.GroupName)
                .GroupBy(menuItem => menuItem.GroupName)
                .ToList();

            var allergensToAvoid = user.Allergies.ToArray();

            return
                groups.Select(
                    menuGroup =>
                    new RestaurantMenuModel
                        {
                            CategoryName = menuGroup.Key,
                            Items =
                                MapMenuItems(menuGroup, allergensToAvoid, starredItems, user.Id, onlyStarred, allergyFree)
                                .OrderBy(item => item.Name)
                                .ToArray()
                        }).ToList();
        }
        public static IList<KeyValuePair<string, IList<IngredientItemModel>>> Map(MenuItem menuItem, User user)
        {
            var allergensToAvoid = user.Allergies.Select(allergen => allergen.Id).ToArray();
            var res = new List<KeyValuePair<string, IList<IngredientItemModel>>>();

            var ingredients = menuItem.Ingredients
                .Where(ingredient => !ingredient.Allergens.Any(allergen => allergensToAvoid.Contains(allergen.Id)))
                .OrderBy(ingredient => ingredient.Name)
                .Select(ingredient => new IngredientItemModel
                    {
                        AllergenNames = string.Empty,
                        Name = ingredient.Name,
                        Description = ingredient.Description ?? ""
                    })
                .ToList();

            res.Add(
                new KeyValuePair<string, IList<IngredientItemModel>>(
                    string.Format("{0} Items you can have", ingredients.Count), ingredients));

            ingredients = menuItem.Ingredients
                .Where(ingredient => ingredient.Allergens.Any(allergen => allergensToAvoid.Contains(allergen.Id)))
                .OrderBy(ingredient => ingredient.Name)
                .Select(ingredient => new IngredientItemModel
                    {
                        AllergenNames = string.Join(", ", ingredient.Allergens
                                                              .Where(
                                                                  allergen =>
                                                                  allergensToAvoid.
                                                                      Contains(
                                                                          allergen.Id))
                                                              .Select(
                                                                  allergen =>
                                                                  allergen.Name).OrderBy(a => a)),
                        Name = ingredient.Name,
                        Description = ingredient.Description ?? ""
                    }).ToList();

            res.Add(
                new KeyValuePair<string, IList<IngredientItemModel>>(
                    string.Format("{0} Items you can't have", ingredients.Count), ingredients));

            return res;
        }
Пример #3
0
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var userRepository = ObjectFactory.GetInstance<IUserRepository>();
            Guid userId;
            Guid.TryParse(bindingContext.ValueProvider.GetValue("userId").AttemptedValue, out userId);

            var user = userRepository.GetUser(userId);

            if (user == null)
            {
                user = new User {CreateDate = DateTime.Now, LastActivity = DateTime.UtcNow, Id = userId};
                userRepository.Create(user);
            }
            else
            {
                user.LastActivity = DateTime.UtcNow;
                userRepository.Update(user);
            }

            bindingContext.Model = user;

            return true;
        }
 public static IList<RestaurantMenuModel> Map(IEnumerable<MenuItem> menuItems, User user, bool onlyStarred, bool allergyFree)
 {
     return MapMenuGroups(menuItems, user, onlyStarred, allergyFree);
 }