예제 #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() }));
        }
예제 #2
0
 public ActionResult UploadFile(HttpPostedFileBase file)
 {
     try
     {
         if (file.ContentLength > 0 && HttpPostedFileBaseExtensions.IsImage(file))
         {
             ResizerImage.UploadImage(file);
             ViewBag.Message = "File uploaded successfully!";
             return(View());
         }
         else
         {
             ViewBag.Message = "File upload failed!";
             return(View());
         }
     } catch (Exception e)
     {
         ViewBag.Message = "File upload failed!";
         return(View());
     }
 }
예제 #3
0
        public async Task <ActionResult> Edit([Bind] UserPostEditViewModel postFromView)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Edit"));
            }

            try
            {
                var originalPost = await _postRepo.GetPostAsync(postFromView.Id);

                var path = originalPost.ImagePath;
                if (originalPost.User.Id == User.Identity.GetUserId() || User.IsInRole("Admin"))
                {
                    var maped = _mapper.Map <UserPostEditViewModel, Post>(postFromView, originalPost);
                    maped.CategoryId = postFromView.SelectedCategory;
                    if (postFromView.ImageFile != null)
                    {
                        string imagePath = ResizerImage.UploadImage(postFromView.ImageFile);
                        maped.ImagePath = imagePath;
                    }
                    else
                    {
                        maped.ImagePath = path;
                    }
                    _postRepo.Update(maped);
                    _postRepo.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return(View(postFromView));
            }
            TempData["editError"] = "false";
            return(RedirectToRoute("PostDetails", new { id = postFromView.Id, name = postFromView.GetTitleAsUrl() }));
        }