示例#1
0
        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());
        }
示例#2
0
        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));
        }
示例#3
0
        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));
        }
示例#4
0
        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));
        }