Beispiel #1
0
        public ActionResult Add([Bind] UserPostAddViewModel post)
        {
            if (!(User.IsInRole("Admin") || User.IsInRole("Moderator")))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }


            if (!ModelState.IsValid && !HttpPostedFileBaseExtensions.IsImage(post.ImageFile))
            {
                var categories = _categoryRepo.GetAll();
                var model      = new UserPostAddViewModel();
                model.Categories = categories;
                return(RedirectToAction("Add"));
            }

            var mapedPost = _mapper.Map <UserPostAddViewModel, Post>(post);

            try
            {
                mapedPost.UserId     = User.Identity.GetUserId();
                mapedPost.CategoryId = post.SelectedCategory;
                string imagePath = ResizerImage.UploadImage(post.ImageFile);
                mapedPost.ImagePath = imagePath;
                _postRepo.AddPost(mapedPost);
                _postRepo.SaveChanges();
            }
            catch (Exception e)
            {
                return(RedirectToAction("Add"));
            }
            TempData["addError"] = "false";
            return(RedirectToRoute("PostDetails", new { id = mapedPost.Id, name = post.GetTitleAsUrl() }));
        }
Beispiel #2
0
        //[Route("post/add")]
        public ActionResult Add()
        {
            if (!(User.IsInRole("Admin") || User.IsInRole("Moderator")))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var categories = _categoryRepo.GetAll();
            var model      = new UserPostAddViewModel();

            model.Categories = categories;

            return(View(model));
        }