public ActionResult Login(ExUser userObj) { RecipeapiContext con = new RecipeapiContext(); Users loginUser = con.Users.Where(user => user.Login == userObj.Login && user.Password == userObj.Password).FirstOrDefault(); if (loginUser != null) { if (loginUser.IsLoggedIn == 0) { loginUser.IsLoggedIn = 1; } else { return(Ok(loginUser));; } loginUser.Favourite = con.Favourite.Where(fav => fav.UserId == loginUser.Id).ToList(); loginUser.Comment = con.Comment.Where(fav => fav.User == loginUser.Id).ToList(); int result = con.SaveChanges(); if (result > 0) { return(Ok(loginUser)); } } return(BadRequest("Failed to login")); }
public ActionResult Post(ExUser userObj) { RecipeapiContext con = new RecipeapiContext(); Users usr = con.Users.FirstOrDefault(user => user.Login == userObj.Login); if (usr == null) { usr = new Users(); usr.Login = userObj.Login; usr.Name = userObj.Name; usr.Password = userObj.Password; usr.Image = userObj.Image; con.Users.Add(usr); } else { usr.Name = userObj.Name; usr.Password = userObj.Password; usr.Image = userObj.Image; } int result = con.SaveChanges(); if (result > 0) { return(Ok(usr)); } return(BadRequest("Could not save")); }
public ActionResult Logout(string UserID) { int id = 0; int.TryParse(UserID, out id); RecipeapiContext con = new RecipeapiContext(); Users loginUser = con.Users.Where(user => user.Id == id).FirstOrDefault(); if (loginUser != null) { if (loginUser.IsLoggedIn == 1) { loginUser.IsLoggedIn = 0; } else { return(Ok(loginUser));; } int result = con.SaveChanges(); if (result > 0) { return(Ok(loginUser)); } } return(BadRequest("Failed to logout")); }
public ActionResult Delete(int id) { RecipeapiContext con = new RecipeapiContext(); Recipe res = con.Recipe.FirstOrDefault(recipe => recipe.Id == id); if (res == null) { return(NotFound("This record not found")); } con.Recipe.Remove(res); con.SaveChanges(); return(Ok("Deleted")); }
public ActionResult Favourite(Favourite favourite, bool isFavourite) { if (favourite.RecipeId == 0 && favourite.UserId == 0) { return(BadRequest("Id cannot be 0")); } try { RecipeapiContext con = new RecipeapiContext(); Recipe recipe = con.Recipe.FirstOrDefault(res => res.Id == favourite.RecipeId); Users usr = con.Users.FirstOrDefault(usr => usr.Id == favourite.UserId); //Favourite favourite22 = new Favourite(); //favourite22.RecipeId = favourite.RecipeId; //favourite22.UserId = favourite.UserId; //favourite22.Recipe = recipe; //favourite22.User = usr; if (recipe == null || usr == null) { return(BadRequest("Could not find one of entities")); } if (con.Favourite.Any(fav => fav.RecipeId == favourite.RecipeId && fav.UserId == favourite.UserId) && !isFavourite) { con.Favourite.Remove(favourite); //usr.Favourite.Remove(favourite); } else { con.Favourite.Add(favourite); //usr.Favourite.Add(favourite22); } int result = con.SaveChanges(); if (result > 0) { return(Ok("Success")); } else { return(BadRequest("Failed to update")); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public ActionResult Post(ExComment commentObj) { RecipeapiContext con = new RecipeapiContext(); Comment newComment = new Comment(); newComment.Rating = commentObj.Rating; newComment.Comment1 = commentObj.Comment; newComment.User = commentObj.UserID; newComment.Recipe = commentObj.RecipeId; con.Comment.Add(newComment); int result = con.SaveChanges(); if (result > 0) { return(Ok(newComment)); } return(BadRequest("Could not save")); }
public ActionResult Post(ExRecipe recipe) { RecipeapiContext con = new RecipeapiContext(); //List<Ingredients> ingrList = new List<Ingredients>(); //foreach(Ingredients ingredient in recipe.IngredientList) //{ // ingrList.Add(ingredient); //} Recipe recipeObj = new Recipe(); if (recipe.id > 0) { recipeObj = con.Recipe.FirstOrDefault(rec => rec.Id == recipe.id) ?? new Recipe();; } recipeObj.Name = recipe.name; recipeObj.Description = recipe.description; recipeObj.Ingredients = null;// JsonConvert.SerializeObject(recipe.IngredientList); recipeObj.Image = recipe.Image; recipeObj.NumberOfServings = recipe.NumberOfServings; recipeObj.CookTime = recipe.CookTime; recipeObj.PrepTime = recipe.PrepTime; recipeObj.Instructions = JsonConvert.SerializeObject(recipe.Instructions); recipeObj.Cuisine = recipe.Cuisine; //recipeObj.IngredientsNavigation = ingrList; //List< DietToRecipe> dietToRList = new List<DietToRecipe>(); //List<CategoryToRecipe> CategoryToRList = new List<CategoryToRecipe>(); //List<MealTypeToRecipe> MealTypeToRList = new List<MealTypeToRecipe>(); // Search for deleted Nutrition items List <int> existinglistIngre = con.Ingredients.Where(nutr => nutr.Recipe == recipe.id).Select(dbnutr => dbnutr.Id).ToList(); List <int> deleteListIngr = new List <int>(); if (recipe.IngredientList != null) { deleteListIngr = existinglistIngre.Except(recipe.IngredientList.Select(x => x.Id)).ToList(); foreach (int id in deleteListIngr) { Ingredients n = con.Ingredients.FirstOrDefault(nut => nut.Id == id); if (n != null) { con.Ingredients.Remove(n); } } // Add or update nutrition records foreach (Ingredients nutr in recipe.IngredientList) { if (nutr.Id > 0) { Ingredients n = con.Ingredients.FirstOrDefault(nutrition => nutrition.Id == nutr.Id); n.Product = nutr.Product; n.Units = nutr.Units; n.Quantity = nutr.Quantity; } else { recipeObj.IngredientsNavigation.Add(nutr); } } } // Search for deleted Nutrition items List <int> existinglist = con.Nutrition.Where(nutr => nutr.Recipe == recipe.id).Select(dbnutr => dbnutr.Id).ToList(); List <int> deleteList = new List <int>(); if (recipe.Nutrition != null) { deleteList = existinglist.Except(recipe.Nutrition.Select(x => x.Id)).ToList(); foreach (int id in deleteList) { Nutrition n = con.Nutrition.FirstOrDefault(nut => nut.Id == id); if (n != null) { con.Nutrition.Remove(n); } } // Add or update nutrition records foreach (Nutrition nutr in recipe.Nutrition) { if (nutr.Id > 0) { Nutrition n = con.Nutrition.FirstOrDefault(nutrition => nutrition.Id == nutr.Id); n.Label = nutr.Label; n.Unit = nutr.Unit; n.Value = nutr.Value; } else { recipeObj.Nutrition.Add(nutr); } } } // Search for deleted Nutrition items List <string> existinglistMeal = con.MealTypeToRecipe.Where(nutr => nutr.RecipeId == recipe.id).Select(dbnutr => dbnutr.MealType.Name).ToList(); List <string> deleteListMeal = new List <string>(); if (recipe.MealTypeList != null) { deleteListMeal = existinglistMeal.Except(recipe.MealTypeList.Select(x => x.Name)).ToList(); foreach (string id in deleteListMeal) { MealTypeToRecipe n = con.MealTypeToRecipe.FirstOrDefault(nut => nut.MealType.Name == id && nut.RecipeId == recipe.id); if (n != null) { con.MealTypeToRecipe.Remove(n); } } foreach (MealType meal in recipe.MealTypeList) { MealTypeToRecipe dtor = null; //Getting mealType from DB list MealType mealType = con.MealType.FirstOrDefault(d => d.Name == meal.Name); if (mealType != null) { // checking if it is linked to recipe dtor = con.MealTypeToRecipe.FirstOrDefault(mtr => mtr.MealTypeId == mealType.Id && recipe.id == mtr.RecipeId); } // if connection between recipe and mealtype dont exist, create it if (dtor == null) { dtor = new MealTypeToRecipe(); dtor.MealType = mealType ?? meal; dtor.Recipe = recipeObj; recipeObj.MealTypeToRecipe.Add(dtor); } } } // Search for deleted Category items List <string> existinglistCate = con.CategoryToRecipe.Where(nutr => nutr.RecipeId == recipe.id).Select(dbnutr => dbnutr.Category.Name).ToList(); List <string> deleteListCat = new List <string>(); if (recipe.Categories != null) { deleteListCat = existinglistCate.Except(recipe.Categories.Select(x => x.Name)).ToList(); } foreach (string id in deleteListCat) { CategoryToRecipe n = con.CategoryToRecipe.FirstOrDefault(nut => nut.Category.Name == id && nut.RecipeId == recipe.id); if (n != null) { con.CategoryToRecipe.Remove(n); } } foreach (Category category in recipe.Categories) { CategoryToRecipe dtor = null; Category cat = con.Category.FirstOrDefault(d => d.Name == category.Name); if (cat != null) { // checking if it is linked to recipe dtor = con.CategoryToRecipe.FirstOrDefault(ctr => ctr.CategoryId == cat.Id && recipe.id == ctr.RecipeId); } if (dtor == null) { dtor = new CategoryToRecipe(); dtor.Category = cat ?? category; dtor.Recipe = recipeObj; recipeObj.CategoryToRecipe.Add(dtor); } } // Delete diets connection List <string> existinglistDietCon = con.DietToRecipe.Where(nutr => nutr.RecipeId == recipe.id).Select(dbnutr => dbnutr.Diet.Name).ToList(); List <string> deleteListDiet = new List <string>(); if (recipe.DietList != null) { deleteListDiet = existinglistDietCon.Except(recipe.DietList.Select(x => x.Name)).ToList(); foreach (string id in deleteListDiet) { DietToRecipe n = con.DietToRecipe.FirstOrDefault(nut => nut.Diet.Name == id && nut.RecipeId == recipe.id); if (n != null) { con.DietToRecipe.Remove(n); } } foreach (Diet d in recipe.DietList) { Diet diet = con.Diet.FirstOrDefault(di => di.Name == d.Name); DietToRecipe dtor = null; if (diet != null) { dtor = con.DietToRecipe.FirstOrDefault(ctr => ctr.Diet.Name == d.Name && recipe.id == ctr.RecipeId); } if (dtor == null) { dtor = new DietToRecipe(); dtor.Diet = diet ?? d; dtor.Recipe = recipeObj; recipeObj.DietToRecipe.Add(dtor); } } } //recipeObj.CategoryToRecipe = null; //recipeObj.DietToRecipe = null; //recipeObj.MealTypeToRecipe = null; //recipeObj.CategoryToRecipe = CategoryToRList; //recipeObj.DietToRecipe = dietToRList; //recipeObj.MealTypeToRecipe = MealTypeToRList; if (recipe.id == 0) { con.Recipe.Add(recipeObj); } con.SaveChanges(); return(Ok()); }