Esempio n. 1
0
        public async Task <IActionResult> Create(PostCreateViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var post = new Post()
                {
                    Title           = viewModel.Title,
                    Text            = viewModel.Text,
                    Author          = await _accountService.GetCurrentUser(User),
                    CreatedDateTime = DateTime.Now,
                    UpdatedDateTime = DateTime.Now,
                    Blog            = await _blogService.GetBlogById((int)TempData["blogId"])
                };

                if (viewModel.ImageFile?.FileName != null)
                {
                    post.ImagePath = await _imageService.SavePostImage(viewModel.ImageFile);
                }

                if (viewModel.StringTags != null)
                {
                    List <Tag> postTags = viewModel.StringTags.Select(m => new Tag {
                        Name = m, PostId = post.PostId
                    }).ToList();
                    post.Tags = postTags;
                    await _postService.AddPostTags(postTags);
                }

                await _postService.AddPost(post);

                return(RedirectToAction(
                           nameof(Details),
                           "Blog",
                           new RouteValueDictionary(new { controller = "Blog", action = "Details", id = post.Blog.BlogId }
                                                    )));
            }
            return(View(viewModel));
        }
        public async Task <IActionResult> AddPostTags(int postId, string tags)
        {
            AppUser user = await userManager.GetUserAsync(User);

            try
            {
                if (tags != null && tags != string.Empty)
                {
                    await postService.AddPostTags(postId, tags, user.Alias);
                }
                else
                {
                    await postService.RemovePostTags(postId, user.Alias);
                }
                return(Ok());
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }
        }