public ActionResult PostAdd(PostViewModel model) { var validator = new PostAddValidator(_uow).Validate(model.Post); if (validator.IsValid) { if (model.PostedPicture != null) { string dosyaYolu = Path.GetFileName(model.PostedPicture.FileName); var yuklemeYeri = Path.Combine(Server.MapPath("~/Images"), dosyaYolu); model.PostedPicture.SaveAs(yuklemeYeri); model.Post.PostPic = dosyaYolu; } model.Post.PostDate = DateTime.Now; model.Post.LikeCount = 0; model.Post.DislikeCount = 0; model.Post.UserId = 1; _uow.GetRepo <Post>().Add(model.Post); var cat = _uow.GetRepo <Category>().GetById(model.Post.CategoryId); cat.PostCount = cat.PostCount + 1; _uow.GetRepo <Category>().Update(cat); _uow.Commit(); ViewBag.Msg = "Gönderi başarılı bir şekilde kaydedildi"; return(RedirectToAction("Post", "Post")); } else { validator.Errors.ToList().ForEach(x => ModelState.AddModelError(x.PropertyName, x.ErrorMessage)); } ViewBag.Msg = "İşlem gerçekleştirilemedi"; return(View()); }
public ActionResult Ekle(PostViewModel model) { if (model.PostedPic != null) { #region UploadPhotoSaveToDatabase MediaUpload upload = new MediaUpload(); upload = UploadSaveToDatabase(model.PostedPic); _uow.GetRepo <MediaUpload>() .Add(upload); _uow.Commit(); model.Post.PostPic = upload.Path.ToString(); #endregion } if (model.Post.Tags != null) { char[] separators = { ',', '.', '!', '?', ';', ':', ' ' }; tags = model.Post.Tags.Split(separators); } IsSuccess = false; var validator = new PostAddValidator().Validate(model.Post); if (validator.IsValid) { if (model.Post.Tags != null) { foreach (var item in tags) { model.Post.Tags = item.ToString(); } } model.Post.UserId = 1; model.Post.PostDate = DateTime.Now; _uow.GetRepo <Post>() .Add(model.Post); #region KategoriGönderiSayısıKontrol _uow.GetRepo <Category>() .GetById(model.Post.CategoryId) .PostCount++; #endregion if (_uow.Commit() > 0) { IsSuccess = true; ViewBag.IsSuccess = IsSuccess; ViewBag.Msg = "Yazı başarıyla eklendi."; } else { ViewBag.IsSuccess = IsSuccess; ViewBag.Msg = "Yazı kaydedilirken bir hata oluştu!"; } } else { validator.Errors.ToList().ForEach(x => ModelState.AddModelError(x.PropertyName, x.ErrorMessage)); } CategoryFill(); return(View()); }