public async Task <IActionResult> AddBadge([FromRoute] int id, [FromRoute] int badgeId) { Post post = await FullPosts .Where(p => p.Id == id) .FirstOrDefaultAsync(); Badge tag = await _context.Badges .Where(t => t.Id == badgeId) .FirstOrDefaultAsync(); if (post == null) { return(NotFound($"No post {id} found")); } if (tag == null) { return(NotFound($"No tag {badgeId} found")); } if (!await _rightsChecker.CheckRights(post, HttpContext)) { return(BadRequest("No rights to add badge to this post")); } if (tag.BadgePosts.Any(tp => tp.PostId == id)) { return(BadRequest($"Post {id} already contains tag {badgeId}")); } BadgePost badgePost = new BadgePost { PostId = id, BadgeId = badgeId }; _context.Add(badgePost); await _context.SaveChangesAsync(); int userId = (await _currentUser.GetCurrentUser(HttpContext)).Id; return(Ok(post.CommentsViewModel(userId))); }
public async Task <IActionResult> RemoveBadge([FromRoute] int id, [FromRoute] int badgeId) { Post post = await FullPosts .Where(p => p.Id == id) .FirstOrDefaultAsync(); Badge badge = await _context.Badges .Where(t => t.Id == badgeId) .FirstOrDefaultAsync(); if (post == null) { return(NotFound($"No post {id} found")); } if (badge == null) { return(NotFound($"No tag {badgeId} found")); } if (!await _rightsChecker.CheckRights(post, HttpContext)) { return(BadRequest("No rights to remove badge from this post")); } if (!badge.BadgePosts.Any(tp => tp.PostId == id)) { return(BadRequest($"Post {id} does not contain tag {badgeId}")); } BadgePost tagPost = post.BadgePosts.Where(tp => tp.BadgeId == badgeId).FirstOrDefault(); tagPost.IsDeleted = true; _context.Update(tagPost); await _context.SaveChangesAsync(); int userId = (await _currentUser.GetCurrentUser(HttpContext)).Id; return(Ok(post.CommentsViewModel(userId))); }