Ejemplo n.º 1
0
        public async Task <IActionResult> Create(NewsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newsForSave = _mapper.Map <NewsViewModel, News>(model);

                var newsFromDb = _newsData.AddNews(newsForSave);

                var fileExt = model.Wallpaper.FileName.Split('.').Last();

                string path = $"{Path.DirectorySeparatorChar}images{Path.DirectorySeparatorChar}" +
                              $"newsWallpapers{Path.DirectorySeparatorChar}"
                              + newsFromDb.Id + $".{fileExt}";


                using (var fileStream = new FileStream(_env.WebRootPath + path, FileMode.Create))
                {
                    await model.Wallpaper.CopyToAsync(fileStream);
                }

                newsFromDb.ImageUrl    = "/images/newsWallpapers/" + newsFromDb.Id + $".{fileExt}";
                newsFromDb.ReleaseTime = DateTime.Now;

                newsFromDb.ImageLocalUrl = _env.WebRootPath + path;

                newsFromDb.Author = await _userManager.GetUserAsync(User);

                _newsData.UpdateNews(newsFromDb);
                return(RedirectToAction("Index", "News"));
            }
            return(View());
        }
Ejemplo n.º 2
0
        public JsonResult SaveNews()
        {
            NameValueCollection form = HttpContext.Request.Form;

            var news = new DuyuruHaber
            {
                DuyuruId     = Convert.ToInt32(form["DuyuruId"]),
                DuyuruBaslik = form["DuyuruBaslik"],
                Duyuru       = form["Duyuru"],
                DuyuruKisa   = form["DuyuruKisa"],
                Aktif        = Convert.ToBoolean(form["Aktif"]),
                Sira         = !string.IsNullOrEmpty(form["Sira"]) ? Convert.ToInt32(form["Sira"]) : 0
            };

            if (Session[Constants.SessionInformation] != null)
            {
                var user = (User)Session[Constants.SessionInformation];
                news.CreateUser = user.Id;
            }

            if (Request.Files != null && Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files[0];

                file.SaveAs(Server.MapPath("/Upload/NewsImage/" + file.FileName));

                news.ImageUrl = file.FileName;
            }

            if (news.DuyuruId <= 0)
            {
                _newsData.SaveNews(news);
            }
            else
            {
                _newsData.UpdateNews(news);
            }

            return(Json(new Response {
                ErrorMessage = "Kaydedilmiştir", IsSuccess = false
            }, JsonRequestBehavior.AllowGet));
        }