public async Task Modify([FromBody] PostTag item) { try { await tagRepository.AddPostTag(item); } catch (Exception ex) { } }
public IActionResult Post(PostTag postTag) { _tagRepository.AddPostTag(postTag); return(CreatedAtAction("Get", new { id = postTag.Id }, postTag)); }
public ActionResult Edit(int id, string[] PostTagsChecks) { Post post = _postRepository.GetPublishedPostById(id); List <Tag> postTags = _tagRepository.GetPostTags(post.Id); List <Tag> allTags = _tagRepository.GetAll(); PostTagMgmtViewModel vm = new PostTagMgmtViewModel() { PostId = id, PostTags = postTags, AllTags = allTags }; try { // Original Post Tag ID's List <int> OrgTagIds = postTags.Select(tag => tag.Id).ToList(); // Newly-Set Post Tag ID's List <int> AdjTagIds = Array.ConvertAll(PostTagsChecks, int.Parse).ToList(); // Tag ID's to remove List <int> deprecatedPostTags = OrgTagIds.Except(AdjTagIds).ToList(); // Tag ID's to add List <int> newPostTags = AdjTagIds.Except(OrgTagIds).ToList(); if (deprecatedPostTags.Count > 0) { foreach (int tagId in deprecatedPostTags) { PostTag postTag = new PostTag() { PostId = id, TagId = tagId }; _tagRepository.DeletePostTag(postTag); } ; } if (newPostTags.Count > 0) { foreach (int tagId in newPostTags) { PostTag postTag = new PostTag() { PostId = id, TagId = tagId }; _tagRepository.AddPostTag(postTag); } ; } return(RedirectToAction("Details", "Post", new { id = vm.PostId })); } catch { return(View(vm)); } }