Exemplo n.º 1
0
        private Author GetAuthor(PostCreateVM vm)
        {
            int.TryParse(vm.SelectedAuthorId, out var AuthorId);
            var author = _blogContext.Authors.Find(AuthorId);

            return(author);
        }
        public IActionResult Create(int?id)
        {
            if (id != null)
            {
                var post = dbContext.Posts
                           .Include(p => p.PostCategories)
                           .Where(p => p.PostId == id).FirstOrDefault();
                if (post == null)
                {
                    return(View());
                }

                var PostVM = new PostCreateVM()
                {
                    PostId           = post.PostId,
                    Title            = post.Title,
                    Description      = post.Description,
                    EditImagePath    = post.DisplayImage,
                    Categories       = dbContext.Categories.ToList(),
                    SelectedCategory = post.PostCategories.Select(pc => pc.CategoryId).ToList()
                };
                ViewBag.Categories = new SelectList(dbContext.Categories, "CategoryId", "CategoryName");
                return(View(PostVM));
            }
            else
            {
                var post = new PostCreateVM();
                post.Categories    = dbContext.Categories.ToList();
                ViewBag.Categories = new SelectList(dbContext.Categories, "CategoryId", "CategoryName");
                return(View(post));
            }
        }
Exemplo n.º 3
0
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                Category category = UnitOfWork.CategoryManager.GetById(post.Category.Id);
                post.Category = category;
                var userId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                var user   = UnitOfWork.ApplicationUserManager.FindById(userId);
                post.User = user;
                post.Time = DateTime.Now;
                UnitOfWork.PostManager.Add(post);
                return(RedirectToAction("Index"));
            }

            PostCreateVM postCreateVm = new PostCreateVM()
            {
                Categories = UnitOfWork.CategoryManager.GetAll()
                             .Select(c => new SelectListItem()
                {
                    Text  = c.Name,
                    Value = c.Id.ToString()
                })
                             .ToList(),
            };

            return(View(postCreateVm));
        }
        public IActionResult Create([FromForm] PostCreateVM vm, int?id)
        {
            if (id != null)
            {
                var post = dbContext.Posts.Include(p => p.PostCategories)
                           .Where(p => p.PostId == id).FirstOrDefault();

                if (vm.DisplayImage != null)
                {
                    post.DisplayImage = fileService.Upload(vm.DisplayImage);
                    var fileName = fileService.Upload(vm.DisplayImage);
                    post.DisplayImage = fileName;
                    getpath           = fileName;
                }
                post.Title          = vm.Title;
                post.Description    = vm.Description;
                post.PostCategories = new List <PostCategory>();

                foreach (var CategoryId in vm.SelectedCategory)
                {
                    post.PostCategories.Add(new PostCategory {
                        CategoryId = CategoryId
                    });
                }
                dbContext.SaveChanges();
                TempData["Editmessage"] = "Edited Successfully";
                return(RedirectToAction("Index"));
            }
            else
            {
                Post post = new Post()
                {
                    PostId       = vm.PostId,
                    Description  = vm.Description,
                    Title        = vm.Title,
                    DisplayImage = vm.DisplayImage.FileName,
                };
                if (ModelState.IsValid)
                {
                    var fileName = fileService.Upload(vm.DisplayImage);
                    post.DisplayImage = fileName;
                    getpath           = fileName;

                    dbContext.Add(post);
                    dbContext.SaveChanges();
                }
                foreach (var cat in vm.SelectedCategory)
                {
                    PostCategory postCategory = new PostCategory();
                    postCategory.PostId     = post.PostId;
                    postCategory.CategoryId = cat;
                    dbContext.Add(postCategory);
                    dbContext.SaveChanges();
                }
                TempData["message"] = "Successfully Added";
                return(RedirectToAction("Index"));
            }
        }
Exemplo n.º 5
0
 private void GetAuthorNames(PostCreateVM vm)
 {
     vm.AuthorList = _blogContext.Authors
                     .Select(x => new SelectListItem {
         Value = x.AuthorId.ToString(),
         Text  = $"{x.FirstName} {x.LastName}"
     })
                     .ToList();
 }
Exemplo n.º 6
0
        public IActionResult Create(PostCreateVM vm, Guid[] categories)
        {
            var model = _createValidator.Validate(vm);

            if (model.IsValid)
            {
                _mappedPost.AddMappedPost(vm, categories);
                _mappedPost.Save();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(vm));
        }
Exemplo n.º 7
0
        public IActionResult Create()
        {
            /*   var authors = _blogContext.Authors
             *    .Select(x => new SelectListItem {
             *        Value = $"{x.AuthorId}",
             *        Text = $"{x.FirstName} {x.LastName}"
             *    });*/
            // ViewBag.Authors = authors;
            var vm = new PostCreateVM();

            GetAuthorNames(vm);
            return(View(vm));
        }
Exemplo n.º 8
0
        public ActionResult Create()
        {
            PostCreateVM postCreateVm = new PostCreateVM()
            {
                Categories = UnitOfWork.CategoryManager.GetAll()
                             .Select(c => new SelectListItem()
                {
                    Text  = c.Name,
                    Value = c.Id.ToString()
                })
                             .ToList(),
            };

            return(View(postCreateVm));
        }
Exemplo n.º 9
0
        public IActionResult Create([FromBody] PostCreateVM model)
        {
            Post post = new Post
            {
                PostId      = model.PostId,
                Header      = model.Header,
                Preview     = model.Preview,
                Body        = model.Body,
                Image       = model.Image,
                Author      = model.Author,
                CreatedDate = DateTime.Now
            };

            _context.Posts.Add(post);
            _context.SaveChanges();
            return(Ok(post));
        }
Exemplo n.º 10
0
        public IActionResult Comment([FromRoute] int id, PostCreateVM vm)
        {
            var comment = vm.Comment;
            var author  = GetAuthor(vm);

            if (ModelState.IsValid)
            {
                comment.Content = vm.Comment.Content;
                comment.PostId  = id;
                comment.Author  = author;
                _blogContext.Comments.Add(comment);
                _blogContext.SaveChanges();
                return(RedirectToAction("Details"));
            }
            GetAuthorNames(vm);
            return(View());
        }
Exemplo n.º 11
0
        public IActionResult Create(PostCreateVM vm)
        {
            var post   = vm.Post;
            var author = GetAuthor(vm);

            if (ModelState.IsValid)
            {
                post.Date       = DateTime.Now;
                post.AuthorName = author;
                post.Content    = vm.Post.Content;
                post.Title      = vm.Post.Title;
                _blogContext.Posts.Add(post);
                _blogContext.SaveChanges();
                return(RedirectToAction("Index"));
            }
            GetAuthorNames(vm);
            return(View(vm));
        }
Exemplo n.º 12
0
        public IActionResult Details([FromRoute] int id)
        {
            var post = _blogContext.Posts.Find(id);

            if (post != null)
            {
                var author   = _blogContext.Authors.Find(post.AuthorId);
                var comments = _blogContext.Comments.Where(x => x.PostId == post.PostId).ToList();
                var vm       = new PostCreateVM()
                {
                    Post     = post,
                    Comments = comments,
                };
                GetAuthorNames(vm);
                return(View(vm));
            }
            return(RedirectToAction("Index"));  // this could be an error page
        }
Exemplo n.º 13
0
        public IActionResult Create()
        {
            PostCreateVM postCreateVM = new PostCreateVM()
            {
                Posts = new Post(),
                CategoriesSelectList = _appDbContext.Category.Select(item => new SelectListItem
                {
                    Text  = item.Title,
                    Value = item.Id.ToString()
                }),
                TagsSelectList = _appDbContext.Tag.Select(item => new SelectListItem
                {
                    Text  = item.Title,
                    Value = item.Id.ToString()
                })
            };

            return(View(postCreateVM));
        }
Exemplo n.º 14
0
        public void AddMappedPost(PostCreateVM vm, Guid[] categories)
        {
            Post post = _mapper.Map <Post>(vm);

            _postRepository.Add(post, categories);
        }
Exemplo n.º 15
0
 public IActionResult Create(PostCreateVM postCreateVM)
 {
     _appDbContext.Posts.Add(postCreateVM.Posts);
     _appDbContext.SaveChanges();
     return(View(postCreateVM));
 }