public async Task <IActionResult> AddHashTag(int linkId, [FromBody] HashTag tag) { var link = await _context.SiteLinks .Include(l => l.SiteLinkHashTags) .FirstOrDefaultAsync(l => l.Id == linkId); if (link is null) { return(NotFound()); } // TODO: maybe it's better to match tag by name rather then id. MR var tagAlreadyExists = link.SiteLinkHashTags .Select(lt => lt.HashTagId) .Contains(tag.Id); if (tagAlreadyExists) { return(Ok()); } _context.Attach(link); _context.Update(new SiteLinkHashTag { SiteLinkId = linkId, HashTag = tag, }); await _context.SaveChangesAsync(); return(Ok()); }