示例#1
0
        public async Task <IActionResult> AddPost(PostCreateViewModel createPostViewModel)
        {
            if (ModelState.IsValid)
            {
                var slug = BlogUtils.CreateSlug(createPostViewModel.Slug);

                if (string.IsNullOrEmpty(slug))
                {
                    ModelState.AddModelError(ModelStateErrorMsgKey, MsgInvalidSlug);
                    return(View(createPostViewModel));
                }

                var slugIsUnique = await _postRepo.CheckIfSlugIsUnique(slug);

                if (!slugIsUnique)
                {
                    ModelState.AddModelError(ModelStateErrorMsgKey, MsgDuplicateSlug);
                    return(View(createPostViewModel));
                }

                createPostViewModel.Slug = slug;
                var user = await GetLoggedInUser();

                var result = await _postRepo.AddPost(createPostViewModel, user);

                if (result)
                {
                    return(Redirect(Url.Action("AnyPost", "Blog", new { slug })));
                }
            }

            ModelState.AddModelError(ModelStateErrorMsgKey, MsgSomethingIsWrong);
            return(View(createPostViewModel));
        }