public ActionResult Create(PostTagFormViewModel postTagVM) { try { foreach (int TagId in postTagVM.TagsSelected) { PostTag newPostTag = new PostTag { PostId = postTagVM.PostId, TagId = TagId }; _postTagRepository.AddPostTag(newPostTag); } return(RedirectToAction("Details", "Post", new { id = postTagVM.PostId })); } catch { IEnumerable <Tag> tags = _tagRepository.GetAllTags(); List <int> tagsSelected = new List <int>(); PostTagFormViewModel vm = new PostTagFormViewModel() { PostTag = new PostTag(), Tag = tags, TagsSelected = tagsSelected, PostId = postTagVM.PostId }; return(View(vm)); } }
public IActionResult AddTagToPost(PostTag postTag) { PostTag tagCheck = _postTagRepository.CheckIfExists(postTag); if (tagCheck == null) { _postTagRepository.AddPostTag(postTag); return(Ok()); } return(Ok()); }
public IActionResult UpdatePostTags(List <int> tagIds, int postId) { _postTagRepository.DeletePostTag(postId); foreach (int tagId in tagIds) { _postTagRepository.AddPostTag(tagId, postId); } return(NoContent()); }
// Post method takes in postId from route parameter and list of selected tag ids from client side public IActionResult Post(int id, List <int> selectedTagIds) { // Getting all PostTags associated to post var previouslySelectedTags = _postTagRepository.GetTagsByPostId(id); var previouslySelectedTagIds = new List <int>(); // Extracting tag ids from previously selected PostTags foreach (PostTag postTag in previouslySelectedTags) { previouslySelectedTagIds.Add(postTag.TagId); } // Execute when list of selected tag ids from client side is NOT empty if (selectedTagIds != null) { // Iterating through new list of tag ids from client side foreach (int tagId in selectedTagIds) { // Add new PostTag when new tag id is not found in list of previously selected tag ids, eliminates duplicates if (!previouslySelectedTagIds.Contains(tagId)) { _postTagRepository.AddPostTag(new PostTag() { PostId = id, TagId = tagId }); } } // Iterating through list of previously selected tag ids associated with post foreach (int tagId in previouslySelectedTagIds) { // Removing PostTag when previous tag id is not found in new list of tag ids from client side if (!selectedTagIds.Contains(tagId)) { _postTagRepository.RemoveTagFromPost(tagId, id); } } } // Executing when new list of tag ids from client side is empty else { // Iterating through list of previously selected tag ids associated with post, meaning user wants to remove all associated tags in client side foreach (int tagId in previouslySelectedTagIds) { _postTagRepository.RemoveTagFromPost(tagId, id); } } return(NoContent()); }
public IActionResult Post(PostTag postTag) { _postTagRepository.AddPostTag(postTag); return(NoContent()); }