public int Post([FromBody]RecipeHeaderData recipeHeader) { var userId = new UserManager().GetUserIdByName("Admin"); var recipe = new Recipes { CategoryID = (int)recipeHeader.Category_ID, Title = recipeHeader.Title, HowToPrepare = recipeHeader.HowToPrepare, Ingredients = recipeHeader.Ingredients, PrepareTime = recipeHeader.PrepareTime, UserID = userId, FriendlyUrl = recipeHeader.FriendlyUrl, PictureUrl = recipeHeader.PictureUrl }; db.Recipes.Add(recipe); db.SaveChanges(); return recipe.ID; }
public IHttpActionResult Put([FromBody]RecipeHeaderData recipeHeader) { var SelectedRecipe = new RecipeManager().GetRecipeHeaderData(recipeHeader.ID); var userId = new UserManager().GetUserIdByName("Admin"); var recipe = new Recipes { ID = SelectedRecipe.ID, CategoryID = (int)recipeHeader.Category_ID, Title = recipeHeader.Title, HowToPrepare = recipeHeader.HowToPrepare, Ingredients = recipeHeader.Ingredients, PrepareTime = recipeHeader.PrepareTime, UserID = userId, FriendlyUrl = SelectedRecipe.FriendlyUrl, PictureUrl = recipeHeader.PictureUrl }; db.Entry(recipe).State = EntityState.Modified; db.SaveChanges(); return Ok(); }
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"); }