Exemple #1
0
 public List <User> GetAllUsers()
 {
     using (var dbContext = new MyFoodDbContext())
     {
         return(dbContext.Users.ToList());
     }
 }
Exemple #2
0
 public List <Rate> GetRates(Guid recipeId)
 {
     using (var dbContext = new MyFoodDbContext())
     {
         return(dbContext.Rates.Where(x => x.Recipe.Id == recipeId).Include(x => x.User).ToList());
     }
 }
Exemple #3
0
 public User GetUserByLogin(string login)
 {
     using (var dbContext = new MyFoodDbContext())
     {
         var user = dbContext.Users.Include(x => x.Favourites).SingleOrDefault(x => x.Login == login);
         return(user);
     }
 }
Exemple #4
0
 public Recipe AddRecipe(Recipe recipe)
 {
     using (var dbContext = new MyFoodDbContext())
     {
         var newRecipe = dbContext.Recipes.Add(recipe);
         dbContext.SaveChanges();
         return(newRecipe);
     }
 }
Exemple #5
0
 public List <Recipe> GetRecipes(Category category)
 {
     using (var dbContext = new MyFoodDbContext())
     {
         if (category == 0)
         {
             return(dbContext.Recipes.Include(x => x.Users).ToList());
         }
         else
         {
             return(dbContext.Recipes.Where(x => x.Category == category).Include(x => x.Users).ToList());
         }
     }
 }
Exemple #6
0
        public User RegisterUser(User newUser)
        {
            using (var dbContext = new MyFoodDbContext())
            {
                var existingUser = dbContext.Users.SingleOrDefault(x => x.Login == newUser.Login);
                if (existingUser != null)
                {
                    return(null);
                }

                var registeredUser = dbContext.Users.Add(newUser);
                dbContext.SaveChanges();
                return(registeredUser);
            }
        }
Exemple #7
0
 public bool AddToFavorites(User user, Recipe recipe)
 {
     using (var dbContext = new MyFoodDbContext())
     {
         if (user != null && !dbContext.Users.Local.Contains(user) && recipe != null && !dbContext.Recipes.Local.Contains(recipe))
         {
             var userFromBase   = dbContext.Users.Include(x => x.Favourites).SingleOrDefault(x => x.Id == user.Id);
             var recipeFromBase = dbContext.Recipes.Include(x => x.Users).Single(x => x.Id == recipe.Id);
             dbContext.Users.Attach(userFromBase);
             dbContext.Recipes.Attach(recipeFromBase);
             userFromBase.Favourites.Add(recipeFromBase);
             recipeFromBase.Users.Add(userFromBase);
             dbContext.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
Exemple #8
0
        public Rate AddRate(Rate rate)
        {
            using (var dbContext = new MyFoodDbContext())
            {
                var user = dbContext.Users.SingleOrDefault(x => x.Id == rate.User.Id);
                rate.User = user;

                var recipe = dbContext.Recipes.SingleOrDefault(x => x.Id == rate.Recipe.Id);
                rate.Recipe = recipe;

                if (dbContext.Rates.SingleOrDefault(x => x.User.Id == user.Id && x.Recipe.Id == recipe.Id) != null)
                {
                    return(null);
                }

                var newRate = dbContext.Rates.Add(rate);
                dbContext.SaveChanges();
                return(newRate);
            }
        }
Exemple #9
0
 FoodRepository(MyFoodDbContext foodContext)
 {
     _foodContext = foodContext;
 }
 public FoodController(MyFoodDbContext context, IFoodInterface foodInterface)
 {
     _context        = context;
     _foodRepository = foodInterface;
 }