Esempio n. 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));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(PostEditViewModel postEditViewModel)
        {
            if (ModelState.IsValid)
            {
                var slug = BlogUtils.CreateSlug(postEditViewModel.Slug);

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

                var slugIsUnique = await _postRepo.CheckIfSlugIsUnique(slug, postEditViewModel.PostId);

                if (!slugIsUnique)
                {
                    ModelState.AddModelError(ModelStateErrorMsgKey, MsgDuplicateSlug);
                    return(View(postEditViewModel));
                }
                postEditViewModel.Slug = slug;
                var updateResult = await _postRepo.UpdatePost(postEditViewModel);

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

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