Example #1
0
        public async Task OnGetAsync()
        {
            var posts = await pcc.GetPostsAsync();

            foreach (var post in posts)
            {
                Posts.Add(PostCommentDTO.GetPostDTO(post));
            }
        }
Example #2
0
        public async Task <ActionResult <PostCommentDTO> > PostPostComment(PostCommentDTO newComment)
        {
            var comment = await _postComment.Create(newComment, UserClaimsGetters.GetUserId(User));

            if (comment != null)
            {
                return(comment);
            }

            return(BadRequest());
        }
Example #3
0
 public string PostComment(PostCommentDTO pcDTO)
 {
     if (RequestDatabase(string.Format("INSERT INTO Avis  VALUES (' ', '{0}','{1}','{2}','{3}')", pcDTO.Comment, pcDTO.Note, pcDTO.BarId, pcDTO.UserId)) != null)
     {
         return("Request Succeed");
     }
     else
     {
         return("error");
     }
 }
Example #4
0
        public PostCommentDTO TestDto4()
        {
            var userId = "5678";

            var dto = new PostCommentDTO()
            {
                Id     = 5,
                UserId = userId,
                Body   = "I am a comment number 4"
            };

            return(dto);
        }
Example #5
0
        public PostCommentDTO TestDto2()
        {
            var userId = "1234";

            var dto = new PostCommentDTO()
            {
                Id     = 3,
                UserId = userId,
                Body   = "I am a comment number 2"
            };

            return(dto);
        }
Example #6
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var result = await pcc.AddPostAsync(PostCommentDTO.GetPost(PostDTO));

            if (!result)
            {
                return(RedirectToAction("Error"));
            }
            return(RedirectToPage("./Index"));
        }
Example #7
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var post = await pcc.GetPostByIdAsync(id.Value);

            if (post != null)
            {
                PostDTO = PostCommentDTO.GetPostDTO(post);
                return(Page());
            }
            return(NotFound());
        }
Example #8
0
        public async void CanUpdateAComment()
        {
            var service = BuildService();

            var updatedComment = new PostCommentDTO()
            {
                Id     = 1,
                UserId = "1234",
                Body   = "I am updated"
            };

            var returnFromMethod = await service.Update(updatedComment, 1);

            Assert.NotNull(returnFromMethod);
            Assert.Equal(updatedComment.Body, returnFromMethod.Body);
        }
Example #9
0
        public async Task <ResponceModel> Update([FromRoute] int id, [FromBody] PostCommentDTO model)
        {
            var identifier = User.Claims.FirstOrDefault(p => p.Type == "id");

            if (identifier == null)
            {
                return(new ResponceModel(401, "FAILED", null, new string[] { "Yetkilendirme Hatası." }));
            }
            if (id == 0)
            {
                return(new ResponceModel(404, "ERROR", null, new string[] { "Güncellenecek veri bulunamadı." }));
            }
            try
            {
                var user        = (await _userService.GetById(int.Parse(identifier.Value)));
                var postComment = await _postCommentService.Get(id);

                if (user.Id != postComment.UserId)
                {
                    return(new ResponceModel(401, "FAILED", null, new string[] { "Yetkilendirme Hatası." }));
                }
                if (postComment == null)
                {
                    return(new ResponceModel(404, "ERROR", null, new string[] { "Güncellenecek veri bulunamadı." }));
                }
                postComment = model.Adapt <PostComment>();
                _postCommentService.Update(postComment);
                if (await _postCommentService.SaveChangesAsync())
                {
                    return(new ResponceModel(200, "OK", postComment, null));
                }
                else
                {
                    return(new ResponceModel(400, "ERROR", null, new string[] { "Veri güncellenirken bir sorun oluştu." }));
                }
            }
            catch (Exception ex)
            {
                await _logService.Add(new SystemLog()
                {
                    Content = ex.Message, CreateDate = DateTime.Now, UserId = 0, EntityName = _postCommentService.GetType().Name
                });

                return(new ResponceModel(500, "ERROR", null, new string[] { "Veri güncellenirken bir sorun oluştu." }));
            }
        }
        /// <summary>
        /// Get a single specified comment
        /// </summary>
        /// <param name="commentId">The Id of the comment</param>
        /// <returns>A DTO of the specified comment</returns>
        public async Task <PostCommentDTO> GetASpecificComment(int commentId)
        {
            var comment = await _context.PostComments.Where(x => x.ID == commentId)
                          .FirstOrDefaultAsync();

            var commentDTO = new PostCommentDTO()
            {
                Id           = comment.ID,
                UserId       = comment.UserId,
                Body         = comment.Body,
                Created      = comment.Created,
                Modified     = comment.Modified,
                CommentLikes = await GetCommentsLikes(commentId, comment.UserId)
            };

            return(commentDTO);
        }
        /// <summary>
        /// Creates a new Comment and puts it into the database
        /// </summary>
        /// <param name="postComment">The DTO to create the comment</param>
        /// <param name="userId">The userId who made the comment</param>
        /// <returns>If successful returns the DTO to the caller</returns>
        public async Task <PostCommentDTO> Create(PostCommentDTO postComment, string userId)
        {
            DateTime timeNow = DateTime.UtcNow;

            PostComment newComment = new PostComment()
            {
                ID       = postComment.Id,
                UserId   = postComment.UserId,
                Body     = postComment.Body,
                Created  = timeNow,
                Modified = timeNow
            };

            _context.Entry(newComment).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(postComment);
        }
        /// <summary>
        /// Updates a comments in the database
        /// </summary>
        /// <param name="postComment">The PostCommentDTO to be used to update the comment</param>
        /// <returns>If successful returns the updated PostCommentDTO</returns>
        public async Task <PostCommentDTO> Update(PostCommentDTO postComment, int commentId)
        {
            var comment = await _context.PostComments.Where(x => x.ID == commentId)
                          .FirstOrDefaultAsync();

            if (comment != null)
            {
                comment.ID       = comment.ID;
                comment.UserId   = postComment.UserId == null ? comment.UserId : postComment.UserId;
                comment.Body     = postComment.Body == null ? comment.Body : postComment.Body;
                comment.Modified = DateTime.UtcNow;

                _context.Entry(comment).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(await GetASpecificComment(commentId));
            }

            throw new Exception("The comment does not exist in the database.");
        }
Example #13
0
        /// <summary>
        /// Add a comment in a post
        /// </summary>
        /// <param name="postComment"></param>
        /// <returns></returns>
        public ResponseResult <dynamic> Add(PostCommentDTO postComment)
        {
            try
            {
                var comment = new PostComment()
                {
                    IdPerson = postComment.IdPerson,
                    Comment  = postComment.Comment,
                    IdPost   = postComment.IdPost
                };
                comment = CommentRepository.Add(comment);
                CommentRepository.Save();

                return(ResponseResult <dynamic> .Success(postComment));
            }
            catch (Exception ex)
            {
                return(ResponseResult <dynamic> .Error(ex.Message));
            }
        }
Example #14
0
        public async Task <ActionResult <PostCommentDTO> > PutPostComment(PostCommentDTO updateComment, int commentId)
        {
            // Test to see if claim == post.UserId or policy is admin
            // if so allow the update
            // if not don't allow it
            var comment = await _postComment.GetASpecificComment(commentId);

            var usersRoles = UserClaimsGetters.GetUserRoles(User, _userManager);

            if (UserClaimsGetters.GetUserId(User) == comment.UserId || usersRoles.Contains("Admin") || usersRoles.Contains("Owner"))
            {
                var commentUpdate = await _postComment.Update(updateComment, commentId);

                if (commentUpdate != null)
                {
                    return(commentUpdate);
                }

                return(BadRequest());
            }

            throw new Exception("You are not authorized to Update that Comment.");
        }