public List <User> GetAllUsers() { using (var dbContext = new MyFoodDbContext()) { return(dbContext.Users.ToList()); } }
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()); } }
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); } }
public Recipe AddRecipe(Recipe recipe) { using (var dbContext = new MyFoodDbContext()) { var newRecipe = dbContext.Recipes.Add(recipe); dbContext.SaveChanges(); return(newRecipe); } }
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()); } } }
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); } }
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); } }
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); } }
FoodRepository(MyFoodDbContext foodContext) { _foodContext = foodContext; }
public FoodController(MyFoodDbContext context, IFoodInterface foodInterface) { _context = context; _foodRepository = foodInterface; }