private async void addRecipe() { using (HttpClient client = new HttpClient()) { var category = allCategories.Where(c => c.DisplayName == cbCategory.Text).First(); var friendlyUrl = FriendlyUrlHelper.RemoveDiacritics(tbTitle.Text); RecipesHeaderData recipeHeader = new RecipesHeaderData() { HowToPrepare = tbHowToPrepare.Text, Ingredients = tbIngredients.Text, PrepareTime = tbPrepareTime.Text, Title = tbTitle.Text, Category_ID = category.ID, FriendlyUrl = friendlyUrl, PictureUrl = "default" }; var postResponse = await client.PostAsJsonAsync("https://localhost:44300/api/Recipes", recipeHeader); int recipeId = await postResponse.Content.ReadAsAsync <int>(); string pictureUrl = GetFileName(recipeId.ToString(), tbFilePath.Text); if (pictureUrl != null) { string webImagePath = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Web", "Upload", "Images", pictureUrl); File.Copy(tbFilePath.Text, webImagePath); RecipesHeaderData recipeHeaderUpdate = new RecipesHeaderData() { ID = recipeId, PictureUrl = pictureUrl, HowToPrepare = tbHowToPrepare.Text, Ingredients = tbIngredients.Text, PrepareTime = tbPrepareTime.Text, Title = tbTitle.Text, Category_ID = category.ID, FriendlyUrl = friendlyUrl }; var putResponse = await client.PutAsJsonAsync("https://localhost:44300/api/Recipes", recipeHeaderUpdate); bool returnValue = await putResponse.Content.ReadAsAsync <bool>(); } this.Close(); } }
public ActionResult Edit(CategoriesListData model, HttpPostedFileBase picture) { model.SelectedRecipe = new RecipeManager().GetRecipeHeaderData(model.RecipesDB.ID); model.AllCategory = new CategoryManager().GetAllCategory(); if (ModelState.IsValid) { var pictureUrl = FileHelper.GetFileName(model.RecipesDB.ID.ToString(), picture); if (pictureUrl != null) { model.RecipesDB.PictureUrl = pictureUrl; List <string> pictureUrlElements = new List <string>(pictureUrl.Split(new string[] { "_" }, StringSplitOptions.None)); try { var files = Directory.GetFiles(Path.Combine(Server.MapPath("~"), "Upload\\Images")).ToList(); foreach (string file in files) { string fileName = Path.GetFileName(file); List <string> fileElemets = new List <string>(fileName.Split(new string[] { "_" }, StringSplitOptions.None)); if (fileElemets.First().Equals(pictureUrlElements.First())) { System.IO.File.Delete(file); } } picture.SaveAs(Path.Combine(Server.MapPath("~"), "Upload\\Images", pictureUrl)); } catch { } } else if (picture != null) { if (picture.ContentLength > 0) { ModelState.AddModelError("", "Nem megfelelő fájlformátum."); return(View(model)); } } model.RecipesDB.FriendlyUrl = FriendlyUrlHelper.RemoveDiacritics(model.RecipesDB.Title.Replace(" ", "_").ToLower()); db.Entry(model.RecipesDB).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Comments", "Comments", new { recipeId = model.RecipesDB.ID })); } return(View(model)); }
public async Task <ActionResult> Create(CategoriesListData model, HttpPostedFileBase picture) { model.AllCategory = new CategoryManager().GetAllCategory(); if (!ModelState.IsValid) { return(View(model)); } var userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>(); var user = await userManager.FindByIdAsync(User.Identity.GetUserId()); var recipe = new Recipes { CategoryID = model.RecipesDB.CategoryID, Title = model.RecipesDB.Title, HowToPrepare = model.RecipesDB.HowToPrepare, Ingredients = model.RecipesDB.Ingredients, PrepareTime = model.RecipesDB.PrepareTime, UserID = user.Id, FriendlyUrl = FriendlyUrlHelper.RemoveDiacritics(model.RecipesDB.Title.Replace(" ", "_")) }; var pictureUrl = FileHelper.GetFileName("", picture); if (pictureUrl == null) { if (picture != null) { if (picture.ContentLength > 0) { ModelState.AddModelError("", "Nem megfelelő fájlformátum."); return(View(model)); } } else { ModelState.AddModelError("", "Nincs kép kiválasztva."); return(View(model)); } } else if (picture.ContentLength > 5000000) { ModelState.AddModelError("", "Túl nagy méretű kép."); return(View(model)); } db.Recipes.Add(recipe); db.SaveChanges(); int id = recipe.ID; Recipes rec = db.Recipes.Find(id); pictureUrl = FileHelper.GetFileName(rec.ID.ToString(), picture); if (pictureUrl != null) { rec.PictureUrl = pictureUrl; try { picture.SaveAs(Path.Combine(Server.MapPath("~"), "Upload\\Images", pictureUrl)); } catch { } } db.SaveChanges(); return(RedirectToAction("Details")); }