public Recipe GetByGuid(string guid)
 {
     using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
     {
         return(context.Recipes.Where(x => x.Guid == guid).ToList().FirstOrDefault());
     }
 }
 public User Get(string username, string password)
 {
     using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
     {
         return(context.Users.Where(x => x.Username == username && x.Password == password).ToList().FirstOrDefault());
     }
 }
 public string GetDishTypeById(int id)
 {
     using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
     {
         return(context.DishTypes.Where(x => x.Id == id).Select(x => x.Name).ToList().FirstOrDefault());
     }
 }
 public IList <SpecialAssign> GetSpecialsForRecipe(string guid)
 {
     using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
     {
         return((from s in context.SpecialAssigns where s.RecipeGuid == guid select s).ToList());
     }
 }
        public IList <Recipe> GetToplist(bool forceReload)
        {
            var cacheKey = "GetToplist";
            var result   = CacheHandler.Get(cacheKey) as List <Recipe>;

            if (forceReload)
            {
                CacheHandler.Remove(cacheKey);
                result = null;
            }

            if (result == null)
            {
                result = new List <Recipe>();
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    var query = context.Votes.GroupBy(x => x.RecipeGuid)
                                .Select(x => new { Total = x.Sum(z => z.Rating), Id = x.Key })
                                .OrderByDescending(y => y.Total).Take(5).ToList();

                    result.AddRange(query.Select(q => GetByGuid(q.Id)));
                    CacheHandler.AddCacheKeyToCollection(cacheKey);
                    CacheHandler.Set(cacheKey, result);
                }
            }
            return(result);
        }
        public bool Save(string guid, string username, int rating)
        {
            bool voteSaved;

            try
            {
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    var newVote = new Vote
                    {
                        Username   = username,
                        Rating     = rating,
                        RecipeGuid = guid
                    };
                    context.Votes.InsertOnSubmit(newVote);
                    context.SubmitChanges();
                    voteSaved = true;
                }
            }
            catch (Exception)
            {
                voteSaved = false;
            }
            return(voteSaved);
        }
        public IList <Recipe> GetLatest(bool forceReload)
        {
            var cacheKey = "GetLatest";
            var result   = CacheHandler.Get(cacheKey) as List <Recipe>;

            if (forceReload)
            {
                CacheHandler.Remove(cacheKey);
                result = null;
            }

            if (result == null)
            {
                result = new List <Recipe>();
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    result = (from w in context.Recipes
                              orderby w.Date descending
                              select w).Take(15).ToList();
                }
                CacheHandler.AddCacheKeyToCollection(cacheKey);
                CacheHandler.Set(cacheKey, result);
            }
            return(result);
        }
 public IList <Recipe> Search(string text)
 {
     using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
     {
         return(context.Recipes.Where(r => r.Name.Contains(text)).OrderBy(r => r.Date).ToList());
     }
 }
        public bool Create(string userName, string password, string fullName, string emailAddress)
        {
            var result = false;

            try
            {
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    var existingUser = (from u in context.Users where u.Username == userName select u).FirstOrDefault();
                    if (existingUser == null)
                    {
                        var user = new User
                        {
                            Username = userName,
                            Password = password,
                            FullName = fullName,
                            Email    = emailAddress,
                            Role     = 1
                        };
                        context.Users.InsertOnSubmit(user);
                        context.SubmitChanges();
                        result = true;
                    }
                }
            }
            catch
            {
                LogHandler.Log(nameof(UserRepository), LogType.Error, string.Format("Could not create user {0} with e-mail {1}", userName, emailAddress));
            }
            return(result);
        }
 public void Delete(string guid)
 {
     using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
     {
         var query = context.Votes.FirstOrDefault(x => x.RecipeGuid == guid);
         if (query != null)
         {
             context.Votes.DeleteOnSubmit(query);
             context.SubmitChanges();
         }
     }
 }
        public bool UserHasVoted(string username, string guid)
        {
            var userHasVoted = false;

            using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
            {
                var query = context.Votes.FirstOrDefault(x => x.Username == username && x.RecipeGuid == guid);

                if (query != null)
                {
                    userHasVoted = true;
                }
            }

            return(userHasVoted);
        }
        public int GetAvarage(string guid)
        {
            var avarageRating = 0;

            using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
            {
                var query = (from v in context.Votes
                             join r in context.Recipes on v.RecipeGuid equals r.Guid
                             where r.Guid == guid
                             select v.Rating).Average();
                if (query.HasValue)
                {
                    avarageRating = Convert.ToInt32(query.Value);
                }
            }
            return(avarageRating);
        }
        public IList <Category> GetAllCategories()
        {
            var cacheKey = "GetAllCategories";
            var result   = CacheHandler.Get(cacheKey) as List <Category>;

            if (result == null)
            {
                result = new List <Category>();
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    result = (from c in context.Categories select c).ToList();
                    CacheHandler.AddCacheKeyToCollection(cacheKey);
                    CacheHandler.Set(cacheKey, result);
                }
            }
            return(result);
        }
        public bool Save(Recipe recipe)
        {
            var result = false;

            try
            {
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    if (recipe.Id == 0)
                    {
                        var newRecipe = new Recipe
                        {
                            Name        = recipe.Name,
                            Ingredients = recipe.Ingredients,
                            Description = recipe.Description,
                            Portions    = recipe.Portions,
                            CategoryId  = recipe.CategoryId,
                            DishTypeId  = recipe.DishTypeId,
                            Date        = DateTime.Now,
                            Guid        = recipe.Guid
                        };
                        context.Recipes.InsertOnSubmit(newRecipe);
                    }
                    else
                    {
                        var r = (from c in context.Recipes where c.Id == recipe.Id select c).FirstOrDefault();
                        r.Name        = recipe.Name;
                        r.Ingredients = recipe.Ingredients;
                        r.Description = recipe.Description;
                        r.Portions    = recipe.Portions;
                        r.CategoryId  = recipe.CategoryId;
                        r.DishTypeId  = recipe.DishTypeId;
                        r.Guid        = recipe.Guid;
                        r.Id          = recipe.Id;
                    }
                    context.SubmitChanges();
                    result = true;
                    CacheHandler.PurgeCache();
                }
            }
            catch (Exception ex)
            {
                LogHandler.Log(nameof(RecipeRepository), LogType.Error, string.Format("Could not save recipe with name {0}", recipe.Name));
            }
            return(result);
        }
        public IList <Recipe> GetAll()
        {
            var cacheKey = "GetAll";
            var result   = CacheHandler.Get(cacheKey) as List <Recipe>;

            if (result == null)
            {
                result = new List <Recipe>();
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    result = (from r in context.Recipes select r).ToList();
                }
                CacheHandler.AddCacheKeyToCollection(cacheKey);
                CacheHandler.Set(cacheKey, result);
            }
            return(result);
        }
        public bool DeleteVotes(string guid)
        {
            var result = false;

            try
            {
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    var query = context.Votes.FirstOrDefault(x => x.RecipeGuid == guid);
                    if (query != null)
                    {
                        context.Votes.DeleteOnSubmit(query);
                        context.SubmitChanges();
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHandler.Log(nameof(RecipeRepository), LogType.Error, string.Format("Could not delete specials for recipe with GUID {0}", guid));
            }
            return(result);
        }
        public bool SaveSpecial(string guid, int specialId)
        {
            var result = false;

            try
            {
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    var assign = new SpecialAssign
                    {
                        SpecialId  = specialId,
                        RecipeGuid = guid
                    };
                    context.SpecialAssigns.InsertOnSubmit(assign);
                    context.SubmitChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogHandler.Log(nameof(RecipeRepository), LogType.Error, string.Format("Could not save specials for recipe with GUID {0}", guid));
            }
            return(result);
        }
        public bool Delete(string guid)
        {
            var result = false;

            try
            {
                using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
                {
                    var q = context.Recipes.Where(x => x.Guid == guid).ToList().FirstOrDefault();
                    if (q != null)
                    {
                        context.Recipes.DeleteOnSubmit(q);
                        context.SubmitChanges();
                        result = true;
                        CacheHandler.PurgeCache();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHandler.Log(nameof(RecipeRepository), LogType.Error, string.Format("Could not delete recipe with GUID {0}", guid));
            }
            return(result);
        }
        public IList <Recipe> Search(string query, int categoryId, int dishTypeId, IList <Special> specials)
        {
            var  recipeList = new List <Recipe>();
            var  recipesFilteredForSpecials = new List <Recipe>();
            bool searchQuery = false, searchCategory = false, searchDishType = false;

            if (!string.IsNullOrWhiteSpace(query))
            {
                searchQuery = true;
            }
            if (categoryId != 0)
            {
                searchCategory = true;
            }
            if (dishTypeId != 0)
            {
                searchDishType = true;
            }

            using (var context = new ReceptsamlingenDataContext(ConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString))
            {
                if (searchQuery && searchCategory && searchDishType)
                {
                    recipeList = context.Recipes.Where(r => r.Name.Contains(query) && r.CategoryId.Equals(categoryId) && r.DishTypeId.Equals(dishTypeId))
                                 .OrderBy(r => r.Date)
                                 .ToList();
                }
                else if (searchQuery && searchCategory)
                {
                    recipeList = context.Recipes.Where(r => r.Name.Contains(query) && r.CategoryId.Equals(categoryId))
                                 .OrderBy(r => r.Date)
                                 .ToList();
                }
                else if (searchQuery && searchDishType)
                {
                    recipeList = context.Recipes.Where(r => r.Name.Contains(query) && r.DishTypeId.Equals(dishTypeId))
                                 .OrderBy(r => r.Date)
                                 .ToList();
                }
                else if (searchCategory && searchDishType)
                {
                    recipeList = context.Recipes.Where(r => r.CategoryId.Equals(categoryId) && r.DishTypeId.Equals(dishTypeId))
                                 .OrderBy(r => r.Date)
                                 .ToList();
                }
                else if (searchCategory)
                {
                    recipeList = context.Recipes.Where(r => r.CategoryId.Equals(categoryId))
                                 .OrderBy(r => r.Date)
                                 .ToList();
                }
                else if (searchDishType)
                {
                    recipeList = context.Recipes.Where(r => r.DishTypeId.Equals(dishTypeId))
                                 .OrderBy(r => r.Date)
                                 .ToList();
                }
                else if (searchQuery)
                {
                    recipeList = context.Recipes.Where(r => r.Name.Contains(query)).OrderBy(r => r.Date).ToList();
                }

                if (specials.Count > 0)
                {
                    if (recipeList.Count > 0)
                    {
                        recipesFilteredForSpecials = (from recipeCopy in recipeList
                                                      from special in specials
                                                      let specialCopy = special
                                                                        select(from sa in context.SpecialAssigns
                                                                               where sa.RecipeGuid == recipeCopy.Guid && sa.SpecialId == specialCopy.Id
                                                                               select sa.RecipeGuid).FirstOrDefault()
                                                                        into r
                                                                        select(from rr in recipeList where rr.Guid == r select rr)).Cast <Recipe>().ToList();
                    }
                    else
                    {
                        foreach (var special in specials)
                        {
                            recipesFilteredForSpecials = (from r in context.Recipes
                                                          join s in context.SpecialAssigns on r.Guid equals s.RecipeGuid
                                                          where s.SpecialId == special.Id
                                                          select r).ToList();
                        }
                    }
                }

                return(recipesFilteredForSpecials.Count > 0 ? recipesFilteredForSpecials : recipeList);
            }
        }