public async Task <IActionResult> Create(HaberEntity haberEntity)
        {
            haberEntity.Detail    = Request.Form["Detail"].ToString();
            haberEntity.HeadLine  = Request.Form["HeadLine"].ToString();
            haberEntity.Title     = Request.Form["Title"].ToString();
            haberEntity.Id        = _user.GetUserId(User);
            haberEntity.TimeStamp = DateTime.Now;
            var file = Request.Form.Files[0];

            string haberImgUrl = Path.Combine(new string[] { _environment.WebRootPath, "haberimage" });

            if (!Directory.Exists(haberImgUrl))
            {
                Directory.CreateDirectory(haberImgUrl);
            }
            using (var fileStream = new FileStream(Path.Combine(haberImgUrl, file.FileName), FileMode.Create))
            {
                haberEntity.PrimaryImgURL = "/haberimage/" + file.FileName;
                await file.CopyToAsync(fileStream);
            }

            /*
             * if (true)
             * {
             *
             *  _context.Add(haberEntity);
             *  await _context.SaveChangesAsync();
             *  return RedirectToAction("Index", "Home");
             * }
             */
            ViewData["Id"]         = new SelectList(_context.Users, "Id", "Id", haberEntity.Id);
            ViewData["CategoryID"] = new SelectList(_context.Category, "CategoryID", "CategoryID", haberEntity.CategoryID);
            return(View(haberEntity));
        }
        public async Task <IActionResult> Edit(int id, [Bind("HaberID,CategoryID,Detail,HeadLine,Id,Latitude,Longitude,PrimaryImgURL,TimeStamp,Title")] HaberEntity haberEntity)
        {
            if (id != haberEntity.HaberID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(haberEntity);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!HaberEntityExists(haberEntity.HaberID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index", "Home"));
            }
            ViewData["CategoryID"] = new SelectList(_context.Category, "CategoryID", "CategoryID", haberEntity.CategoryID);
            ViewData["Id"]         = new SelectList(_context.Users, "Id", "Id", haberEntity.Id);
            return(View(haberEntity));
        }
Beispiel #3
0
        public async Task <JsonResult> CreateNews()
        {
            var    files    = Request.Form.Files;
            string title    = Request.Form["haberHeader"];
            string headline = Request.Form["haberHeadline"];
            string detail   = Request.Form["haberDetail"];
            string userId   = Request.Form["name"];

            float    latitude   = float.Parse(Request.Form["latitude"], CultureInfo.InvariantCulture);
            float    longitude  = float.Parse(Request.Form["longitude"], CultureInfo.InvariantCulture);
            int      categoryId = int.Parse(Request.Form["CategoryID"]);
            DateTime time       = DateTime.Now;

            if (String.IsNullOrEmpty(userId))
            {
                userId = _userManager.GetUserId(User);
            }
            HaberEntity haber = new HaberEntity {
                Id = userId, Title = title, HeadLine = headline, Detail = detail, Latitude = latitude, Longitude = longitude, TimeStamp = time, CategoryID = categoryId
            };

            _context.Haber.Add(haber);
            _context.SaveChanges();

            int haberId = haber.HaberID;

            string haberImgURL = Path.Combine(new string[] { _environment.WebRootPath, "images", "haber" + haberId });

            if (!Directory.Exists(haberImgURL))
            {
                Directory.CreateDirectory(haberImgURL);
            }
            for (int i = 0; i < files.Count; i++)
            {
                IFormFile file = files.ElementAt(i);
                if (i == 0 && file.Length > 0)
                {
                    using (var fileStream = new FileStream(Path.Combine(haberImgURL, file.FileName), FileMode.Create))
                    {
                        haber.PrimaryImgURL = "/images/" + "haber" + haberId + "/" + file.FileName;
                        await file.CopyToAsync(fileStream);
                    }
                }
                else
                {
                    using (var fileStream = new FileStream(Path.Combine(haberImgURL, file.FileName), FileMode.Create))
                    {
                        string imgURL = "/images/" + "haber" + haberId + "/" + file.FileName;
                        await file.CopyToAsync(fileStream);

                        ImageEntity image = new ImageEntity {
                            HaberID = haberId, UserID = userId, ImageURL = imgURL
                        };
                        _context.Image.Add(image);
                    }
                }
            }
            _context.SaveChanges();
            return(Json(new { }));
        }
        public async Task <IActionResult> Create([Bind("HaberID,CategoryID,Detail,HeadLine,Id,Latitude,Longitude,PrimaryImgURL,TimeStamp,Title")] HaberEntity haberEntity)
        {
            if (ModelState.IsValid)
            {
                _context.Add(haberEntity);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "HaberEntities"));
            }
            ViewData["CategoryID"] = new SelectList(_context.Category, "CategoryID", "CategoryID", haberEntity.CategoryID);
            ViewData["Id"]         = new SelectList(_context.Users, "Id", "Id", haberEntity.Id);
            return(View(haberEntity));
        }