public ActionResult AddPost() { if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account"); IPostsContext postsContext = new PostsContext(); IPostImageContext postImageContext = new PostImageContext(); IUsersContext usersContext = new UsersContext(); IImagesContext imageContext = new ImagesContext(); ICategoriesContext categoriesContext = new CategoriesContext(); User currentUser = new User(); currentUser = usersContext.GetUserByLogin((string)Session["login"]); Post newPost = new Post(); newPost.id_user = currentUser.id; newPost.title = Request.Form["posttitle"]; newPost.text = Request.Form["posttext"]; if (Request.Form["menu-val"] == "newCat") { Categories cat = new Categories(); cat.value = Request.Form["catText"]; newPost.category_id = categoriesContext.CreateCategory(cat).id; } else if(Convert.ToInt32(Request.Form["menu-val"]) == -1) { if (categoriesContext.GetCategoryByValue("Общая") == null) { Categories cat = new Categories(); cat.value = "Общая"; newPost.category_id = categoriesContext.CreateCategory(cat).id; } else { Categories cat = new Categories(); cat = categoriesContext.GetCategoryByValue("Общая"); newPost.category_id = cat.id; } } else { newPost.category_id = Convert.ToInt32(Request.Form["menu-val"]); } newPost = postsContext.AddPost(newPost); var file = Request.Files["post_image"]; if (file.ContentLength != 0) { string path = AppDomain.CurrentDomain.BaseDirectory + "images/posts/"; string filename = newPost.id.ToString() + Path.GetExtension(file.FileName); if (filename != null) file.SaveAs(path + filename); Image image = new Image(); image.image_path = filename; image = imageContext.AddImage(image.image_path); postImageContext.AddPostImage(newPost.id, image.id); } return RedirectToAction("userProfile", "Account"); }
// Возвращает только что добавленную запись public Image AddImage(string image_path) { Image image = new Image(); if (image_path != null && image_path.Trim() != "") { ImagesContext _db = new ImagesContext(); image.image_path = image_path; _db.images.Add(image); _db.SaveChanges(); var imId = _db.images.Select(id => id.id).Max(); image.id = imId; return image; } else return null; }
public ActionResult ChangeProfile() //Обработка данных с формы изменения профиля пользователя { if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account"); IUsersContext _user = new UsersContext(); IImagesContext _image = new ImagesContext(); IUserImageContext _userImage = new UserImageContext(); User user = new User(); user = _user.GetUserByLogin((string)Session["login"]); ViewBag.user = user; user.name = Request.Form["name"]; user.last_name = Request.Form["last_name"]; user.gender = Request.Form["gender"]; user.e_mail = Request.Form["e_mail"]; var file = Request.Files["image"]; if (file.ContentLength != 0) { string path = AppDomain.CurrentDomain.BaseDirectory + "images/users/"; string filename = user.id.ToString() + Path.GetExtension(file.FileName); if (filename != null) file.SaveAs(path + filename); if (_userImage.GetImageByUserId(user.id) == null) { Image image = new Image(); image.image_path = filename; image = _image.AddImage(image.image_path); _userImage.AddUserImage(user.id, image.id); } else { Image image = new Image(); image = _userImage.GetImageByUserId(user.id); image.image_path = filename; image = _image.EditImage(image); } } /*-------------- Обращение к БД ------------------------*/ try { _user.EditUser(user); return RedirectToAction("userProfile", "Account"); } catch { return View(); } }
public Image EditImage(Image image) { if (image != null && image.id != 0 && image.image_path.Trim() != "") { ImagesContext _db = new ImagesContext(); _db.Entry(image).State = EntityState.Modified; _db.SaveChanges(); return image; } return null; }
public ActionResult EditPostF() { if (Session == null || Session["isAuth"] == null || (bool)Session["isAuth"] == false) return RedirectToAction("Login", "Account"); IPostsContext postsContext = new PostsContext(); IPostImageContext postImageContext = new PostImageContext(); IUsersContext usersContext = new UsersContext(); IImagesContext imageContext = new ImagesContext(); string url = "~/Post/PostPage?post="; User currentUser = new User(); currentUser = usersContext.GetUserByLogin((string)Session["login"]); Post newPost = new Post(); newPost.id = Convert.ToInt32(Request.Form["postId"]); newPost.id_user = currentUser.id; newPost.title = Request.Form["posttitle"]; newPost.text = Request.Form["posttext"]; newPost.category_id = Convert.ToInt32(Request.Form["menu-val"]); newPost = postsContext.EditPost(newPost); var file = Request.Files["post_image"]; if (file.ContentLength != 0) { string path = AppDomain.CurrentDomain.BaseDirectory + "images/posts/"; string filename = newPost.id.ToString() + Path.GetExtension(file.FileName); if (filename != null) file.SaveAs(path + filename); if(postImageContext.GetImageByPostId(newPost.id) == null) { Image image = new Image(); image.image_path = filename; image = imageContext.AddImage(image.image_path); postImageContext.AddPostImage(newPost.id, image.id); } else { Image image = new Image(); image = postImageContext.GetImageByPostId(newPost.id); image.image_path = filename; image = imageContext.EditImage(image); } } return Redirect(url + newPost.id); }