コード例 #1
0
ファイル: PostsController.cs プロジェクト: hyhuu99/blog
 public ActionResult Create([Bind(Prefix = "post")] PostVM.PostCreateVM post, HttpPostedFileBase file)
 {
     if (ModelState.IsValid)
     {
         post.Email = User.Identity.GetUserName();
         PostDTO postDTO = new PostDTO();
         postDTO = _imapper.Map <PostVM.PostCreateVM, PostDTO>(post);
         if (file == null)
         {
             if (ConvertString.ConvertToBool(_iapiResponse.Post <PostDTO>("posts", postDTO)))
             {
                 return(RedirectToAction("Index", "Home"));
             }
         }
         else if (file != null && ImageFunction.IsImage(file.InputStream))
         {
             Image resizeImage = ImageFunction.ScaleImage(Image.FromStream(file.InputStream), (int)StatusCode.MAX_WIDTH, (int)StatusCode.MAX_HEIGHT);
             postDTO.Image = ImageFunction.imageToByteArray(resizeImage);
             if (ConvertString.ConvertToBool(_iapiResponse.Post <PostDTO>("posts", postDTO)))
             {
                 return(RedirectToAction("Index", "Home"));
             }
         }
         else
         {
             ModelState.AddModelError("", "Image is not correct file format");
             return(View(post));
         }
     }
     ModelState.AddModelError("", "Can't upload your post");
     return(View(post));
 }
コード例 #2
0
ファイル: PostsController.cs プロジェクト: hyhuu99/blog
        public ActionResult Edit([Bind(Prefix = "post")] PostVM.PostCreateVM post, HttpPostedFileBase file)
        {
            int result = 0;

            if (ModelState.IsValid)
            {
                post.Email = User.Identity.GetUserName();
                PostDTO postDTO = new PostDTO();
                postDTO = _imapper.Map <PostVM.PostCreateVM, PostDTO>(post);
                if (file == null)
                {
                    postDTO.Image = post.Image;
                    result        = ConvertString.ConvertToInt(_iapiResponse.Put <PostDTO>("posts", postDTO)).GetValueOrDefault();
                    if (result == (int)StatusCode.SUCCESS)
                    {
                        return(RedirectToAction("Details", "Posts", new { id = post.PostId, slug = TempData["SlugPost"] }));
                    }
                }
                else if (file != null && ImageFunction.IsImage(file.InputStream))
                {
                    Image resizeImage = ImageFunction.ScaleImage(Image.FromStream(file.InputStream), (int)StatusCode.MAX_WIDTH, (int)StatusCode.MAX_HEIGHT);
                    postDTO.Image = ImageFunction.imageToByteArray(resizeImage);
                    result        = ConvertString.ConvertToInt(_iapiResponse.Put <PostDTO>("posts", postDTO)).GetValueOrDefault();
                    if (result == (int)StatusCode.SUCCESS)
                    {
                        return(RedirectToAction("Details", "Posts", new { id = post.PostId, slug = TempData["SlugPost"] }));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Image is not correct file format");
                    return(View(post));
                }
                if (result == (int)StatusCode.TITLEWASCHANGED || result == (int)StatusCode.CONTENTWASCHANGED || result == (int)StatusCode.TAGWASCHANGED)
                {
                    ModelState.AddModelError("", "You need back to home page and refresh page again to edit");
                    return(View(post));
                }
            }
            ModelState.AddModelError("", "Can't upload your post");
            return(View(post));
        }
コード例 #3
0
ファイル: PostsController.cs プロジェクト: hyhuu99/blog
        public ActionResult Edit(int?id, string slug)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            bool IsMySelf = _iapiResponse.Get <bool>("posts/" + id + "?email=" + User.Identity.GetUserName());

            if (!IsMySelf)
            {
                return(RedirectToAction("Index", "Home"));
            }
            PostDTO postDTO = _iapiResponse.Get <PostDTO>("posts/" + id + "?slug=" + slug);

            PostVM.PostCreateVM postVM = _imapper.Map <PostDTO, PostVM.PostCreateVM>(postDTO);
            TempData["SlugPost"] = slug;
            if (postDTO == null)
            {
                return(HttpNotFound());
            }
            return(View(postVM));
        }