Beispiel #1
0
        public ActionResult Create(PostViewModel postViewModel)
        {
            var userId   = User.Identity.GetUserId();
            var userName = User.Identity.GetUserName();

            string fileName  = Path.GetFileNameWithoutExtension(postViewModel.Image.FileName);
            string extension = Path.GetExtension(postViewModel.Image.FileName);
            var    fileNames = fileName + DateTime.Now.ToString("yy-mm-dd") + extension;
            string path      = fileName + DateTime.Now.ToString("yy-mm-dd") + extension;

            fileName = Path.Combine(Server.MapPath("~/PostImages/"), fileNames);
            postViewModel.Image.SaveAs(fileName);

            Post post = new Post()
            {
                Title       = postViewModel.Title,
                ImagePath   = path,
                Description = postViewModel.Description,
                Tags        = postViewModel.Tags,
                CategoryId  = postViewModel.CategoryId,
                Status      = "New",
                UserId      = Convert.ToInt32(userId),
                UserName    = userName,
                PostDate    = DateTime.Now
            };
            bool isSaved = postManager.Add(post);

            if (isSaved)
            {
                return(RedirectToAction("Index"));
            }

            return(View());
        }
        public async Task <IActionResult> Create(PostViewModel postVm)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    byte[] p1 = null;
                    using (var fs1 = files[0].OpenReadStream())
                    {
                        using (var ms1 = new MemoryStream())
                        {
                            fs1.CopyTo(ms1);
                            p1 = ms1.ToArray();
                        }
                    }
                    postVm.Image = p1;
                }

                var postDto = _postViewModelMapper.Map(postVm);
                await _postManager.Add(postDto);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(postVm));
        }
        public IActionResult Create(CreatePostModel model)
        {
            if (ModelState.IsValid)
            {
                Post post = new Post();
                if (model.Images != null)
                {
                    foreach (IFormFile image in model.Images)
                    {
                        Image  path           = new Image();
                        var    uploadFolder   = Path.Combine(environment.WebRootPath, "images");
                        string uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName;
                        string filePath       = Path.Combine(uploadFolder, uniqueFileName);
                        image.CopyTo(new FileStream(filePath, FileMode.Create));
                        path.FilePath = uniqueFileName;
                        post.Images.Add(path);
                    }
                }
                if (model.Videos != null)
                {
                    foreach (IFormFile video in model.Videos)
                    {
                        Video  path           = new Video();
                        var    uploadFolder   = Path.Combine(environment.WebRootPath, "videos");
                        string uniqueFileName = Guid.NewGuid().ToString() + "_" + video.FileName;
                        string filePath       = Path.Combine(uploadFolder, uniqueFileName);
                        video.CopyTo(new FileStream(filePath, FileMode.Create));
                        path.FilePath = uniqueFileName;
                        post.Videos.Add(path);
                    }
                }

                post.Title       = model.Title;
                post.Description = model.Description;
                post.DateTime    = System.DateTime.Now;
                post.Country     = model.Country;
                post.City        = model.City;
                post.OwnerID     = userService.GetUserId();

                bool isAdded = postManager.Add(post);
                if (isAdded)
                {
                    return(RedirectToAction(nameof(Index)));
                }
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(model));
        }
Beispiel #4
0
        public async Task <PostDTO> Add(PostDTO newPost)
        {
            var post = new Post
            {
                Title    = newPost.Title,
                Text     = newPost.Text,
                AuthorId = newPost.AuthorId
            };

            if (await postManager.Add(post) == null)
            {
                return(null);
            }
            newPost.Id       = post.Id;
            newPost.PostTime = post.PostTime;
            return(newPost);
        }
        public ActionResult Create(CreatePostViewModel _post)
        {
            if (ModelState.IsValid)
            {
                var _newPost = _mapper.Map <PostModel>(_post);
                _newPost.PublishDate    = DateTime.Now;
                _newPost.IsBlocked      = false;
                _newPost.AuthorId       = User.Identity.GetUserId();
                _newPost.UsersReadCount = 0;

                _postManager.Add(_newPost);
                _userService.UserAddPost(User.Identity.GetUserId());

                return(RedirectToAction("MyPosts"));
            }

            return(View(_post));
        }
 public ActionResult Create(PostViewModel postViewModel)
 {
     if (ModelState.IsValid)
     {
         Post post = new Post
         {
             Title      = postViewModel.Title,
             Details    = postViewModel.Details,
             Tags       = postViewModel.Tags,
             CatagoryId = postViewModel.CatagoryId
         };
         bool isSaved = _postManager.Add(post);
         if (isSaved)
         {
             TempData["msg"] = "Post Inserted Successfully";
             return(RedirectToAction("Create"));
         }
     }
     return(View(postViewModel));
 }