Exemple #1
0
        public async Task <IActionResult> Delete(
            int id,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <UserFollowInfo> result = TokenUtils.CheckToken <UserFollowInfo>(token, _context);

            if (result != null)
            {
                return(BadRequest(result));
            }

            Session sessionResult = await _context.Sessions
                                    .FirstOrDefaultAsync(s => s.SessionToken == token);

            UserFollow userFollowResult = await _context.UserFollows
                                          .FirstOrDefaultAsync(uf => uf.FollowingId == id &&
                                                               uf.FollowerId == sessionResult.SessionUserId);

            if (userFollowResult == null)
            {
                result = new ModelResult <UserFollowInfo>(400, null, "User Not Followed");
                return(BadRequest(result));
            }

            _context.Remove(userFollowResult);
            await _context.SaveChangesAsync();

            result = new ModelResult <UserFollowInfo>(201, null, "Remove Follow Success");
            return(Ok(result));
        }
        // DELETE api/comment?id={comment id}&token={token}
        public async Task <IActionResult> Delete(
            [FromQuery(Name = "id")] int id,
            [FromQuery(Name = "token")] string token)
        {
            ModelResult <CommentInfo> result = TokenUtils.CheckToken <CommentInfo>(token, _context);

            if (result != null)
            {
                return(BadRequest(result));
            }

            Session sessionResult = await _context.Sessions
                                    .FirstOrDefaultAsync(s => s.SessionToken == token);

            Comment commentResult = await _context.Comments
                                    .FirstOrDefaultAsync(c => c.CommentId == id);

            if (commentResult == null)
            {
                result = new ModelResult <CommentInfo>(404, null, "Comment Not Exists");
                return(BadRequest(result));
            }

            if (commentResult.UserId == sessionResult.SessionUserId)
            {
                _context.Remove(commentResult);
                await _context.SaveChangesAsync();

                result = new ModelResult <CommentInfo>(200, new CommentInfo(commentResult), "Comment Deleted");
                return(Ok(result));
            }

            result = new ModelResult <CommentInfo>(405, null, "Cannot Delete Others' Comment");
            return(BadRequest(result));
        }