public async Task <IActionResult> PutCommunityPost([FromRoute] string id, [FromBody] CommunityPost communityPost) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != communityPost.PostId) { return(BadRequest()); } _context.Entry(communityPost).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CommunityPostExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PostCommunityPost([FromBody] CommunityPost communityPost) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.CommunityPostDbSet.Add(communityPost); await _context.SaveChangesAsync(); CommunityPost createdPost = await _context.CommunityPostDbSet.Include(u => u.PostedBy).FirstOrDefaultAsync(i => i.PostId == communityPost.PostId); return(CreatedAtAction("GetCommunityPost", new { id = communityPost.PostId }, createdPost)); }
public async Task <IActionResult> UnlinkePost([FromBody] CommunityPost communityPostLiked, [FromServices] SignInManager <ApplicationUser> signInManager) { var loggedUser = signInManager.Context.User.Claims.Where(c => c.Type.Equals("jti")).FirstOrDefault(); var reaction = _context.CommunityPostLikesDbSet.Where(c => c.PostId == communityPostLiked.PostId && c.UserId == loggedUser.Value).FirstOrDefault(); _context.CommunityPostLikesDbSet.Remove(reaction); await _context.SaveChangesAsync(); CommunityPost updatedPost = _context.CommunityPostDbSet.Include(l => l.Likes).FirstOrDefault(p => p.PostId == communityPostLiked.PostId); return(Ok(updatedPost)); }
public async Task <IActionResult> LikePost([FromBody] CommunityPost communityPostLiked, [FromServices] SignInManager <ApplicationUser> signInManager) { var loggedUser = signInManager.Context.User.Claims.Where(c => c.Type.Equals("jti")).FirstOrDefault(); CommunityPostLikes like = new CommunityPostLikes(); like.PostId = communityPostLiked.PostId; like.ReactionDate = DateTime.Now; like.UserId = loggedUser != null ? loggedUser.Value : string.Empty; _context.CommunityPostLikesDbSet.Add(like); await _context.SaveChangesAsync(); CommunityPost updatedPost = _context.CommunityPostDbSet.Include(l => l.Likes).FirstOrDefault(p => p.PostId == communityPostLiked.PostId); return(Ok(updatedPost)); }