public async Task <IHttpActionResult> UnblockPost([FromUri] int postId) { DTOPost post = await _uow.PostService.GetPostByPosId(postId); if (post == null) { return(NotFound()); } if (!post.IsBlocked) { return(BadRequest("Post not blocked.")); } var userId = this.User.Identity.GetUserId(); if (userId == null || !User.IsInRole("admin")) { return(this.Unauthorized()); } await _uow.ModeratorService.UnblockPost(postId); return(Ok("Post unblocked")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (!User.Identity.IsAuthenticated) { throw new Exception(nameof(Unauthorized)); } var user = await userManager.GetUserAsync(User); var id = userManager.GetUserId(User); if (string.IsNullOrWhiteSpace(id)) { return(NotFound( $"Unable to load user with ID '{id}'.")); } DTOPost newPost = new DTOPost { Active = true, Title = PostModel.Title, Content = PostModel.Content, PostDate = PostModel.PostDate, SEOName = PostModel.Title.MakeSEOSafe(), OwnerId = id }; var r = await postStore.CreateAsync(newPost); return(RedirectToPage()); }
public async Task <IHttpActionResult> DeletePost([FromUri] int postId) { var authtor = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>()); if (authtor == null) { return(this.Unauthorized()); } if (authtor.IsBlocked) { return(BadRequest("Your account blocked.")); } DTOPost post = await _uow.PostService.GetPostByPosId(postId); if (post == null) { return(NotFound()); } if (post.UserInfo.Id != authtor.Id) { return(BadRequest("It is not your post.")); } if (!this.ModelState.IsValid) { return(BadRequest(this.ModelState)); } await _uow.PostService.DeletePost(postId); return(Ok("Post removed")); }
public async Task EditPost(DTOPost dtoPost) { var post = AutoMapper.Mapper.Map <DTOPost, Post>(dtoPost); if (post.UserInfoId != 0) { await _uow.Posts.Update(post); } else { throw new ArgumentException("Wrong data"); } }
public async Task AddPost(DTOPost dtoPost) { var post = AutoMapper.Mapper.Map <DTOPost, Post>(dtoPost); if (post.PostBody != null) { await _uow.Posts.Insert(post); } else { throw new ArgumentException("Wrong data"); } }
public async Task <IHttpActionResult> AddPost() { var authtor = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>()); if (authtor == null) { return(this.Unauthorized()); } if (authtor.IsBlocked) { return(BadRequest("Your account blocked.")); } var httpRequest = HttpContext.Current.Request; //get request object HttpPostedFile postedFile = httpRequest.Files["PostPicture"]; //Gets the collection of files uploaded by the client, in multipart MIME format. if (postedFile == null || postedFile.ContentLength < 2) { return(BadRequest("No image")); } string filename = AddImage(postedFile); //add image for post string postBodyCode = httpRequest.Params["Post"]; string postBody = Base64Decode(postBodyCode); //decoding string with html tag DTOPost pos = new DTOPost { PostBody = postBody, Hashtags = httpRequest.Params["Hashtags"], PostTitle = httpRequest.Params["Title"], DateCreate = DateTime.Now, UserInfoId = authtor.Id, PostPicture = filename }; if (!this.ModelState.IsValid) { return(BadRequest(this.ModelState)); } await _uow.PostService.AddPost(pos); return(Content(HttpStatusCode.Created, "Post added")); }
public async Task <IHttpActionResult> EditComment([FromUri] int postId, [FromBody] CommentEditViewModel newComment) { DTOPost post = await _uow.PostService.GetPostByPosId(postId); if (post == null) { return(NotFound()); } if (post.IsBlocked) { return(BadRequest("This post blocked")); } var userId = User.Identity.GetUserId(); if (userId == null) { return(this.Unauthorized()); } if (newComment.CommentBody.Length < 1) { return(this.BadRequest("Please write comment.")); } var comment = await _uow.CommentService.GetCommentByComId(newComment.Id); if (comment == null) { return(NotFound()); } if (comment.UserInfoId != Int32.Parse(userId)) { return(BadRequest("It is not your comment")); } comment.CommentBody = newComment.CommentBody; comment.UserInfo = null; comment.Post = null; await _uow.CommentService.EditComment(comment); return(Ok("Comment edited")); }
public async Task <IHttpActionResult> EditPost([FromUri] int postId, [FromBody] PostEditViewModel newPost) { var authtor = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>()); if (authtor == null) { return(this.Unauthorized()); } if (authtor.IsBlocked) { return(BadRequest("Your account blocked.")); } DTOPost post = await _uow.PostService.GetPostByPosId(postId); if (post == null) { return(NotFound()); } if (post.UserInfo.Id != authtor.Id) { return(BadRequest("It is not your post.")); } if (post.IsBlocked) { return(BadRequest("Post blocked.")); } post.PostBody = newPost.PostBody; post.Hashtags = newPost.Hashtags; post.LastEdit = DateTime.Now; post.UserInfo = null; post.Comments = null; await _uow.PostService.EditPost(post); return(Ok("Post edited")); }
public async Task <IHttpActionResult> AddComment([FromUri] int postId) { var httpRequest = HttpContext.Current.Request; string comment = httpRequest.Params["Coment"]; DTOPost post = await _uow.PostService.GetPostByPosId(postId); if (post == null) { return(NotFound()); } var userId = this.User.Identity.GetUserId(); if (userId == null) { return(this.Unauthorized()); } if (comment.Length < 1) { ModelState.AddModelError("Body", "Please write comment."); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } DTOUser user = await _uow.UserInfoService.GetUserById(User.Identity.GetUserId <int>()); DTOComment dtoComment = new DTOComment { DateCreation = DateTime.Now, PostId = postId, UserInfoId = user.Id, CommentBody = comment }; await _uow.CommentService.AddComment(dtoComment); return(Content(HttpStatusCode.Created, "Comment added.")); }
public async Task <IHttpActionResult> GetComments([FromUri] int postId) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } DTOPost post = await _uow.PostService.GetPostByPosId(postId); if (post == null) { return(NotFound()); } if (post.IsBlocked) { return(BadRequest("This post blocked")); } var userId = User.Identity.GetUserId(); if (userId == null) { return(this.Unauthorized()); } List <CommentViewModel> comments = AutoMapper.Mapper.Map <IEnumerable <DTOComment>, List <CommentViewModel> >( await _uow.CommentService.GetCommentsToPost(postId)); if (comments != null) { return(Ok(comments)); } else { return(NotFound()); } }
public async Task <bool> AddPost(DTOPost post) { try { var postEntity = new Posts(); postEntity.Autor = post.Autor; postEntity.FechaPublicacion = DateTime.Now; postEntity.Imagen = post.Imagen; postEntity.Resumen = post.Resumen; postEntity.Titulo = post.Titulo; postEntity.UrlVideo = post.UrlVideo; _context.Posts.Add(postEntity); await _context.SaveChangesAsync(); return(true); } catch (Exception) { return(false); } }
public async Task <IHttpActionResult> Delete([FromUri] int commentId) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (User.Identity.GetUserName() == null) { return(this.Unauthorized()); } var userId = User.Identity.GetUserId <int>(); var comment = await _uow.CommentService.GetCommentByComId(commentId); if (comment == null) { return(NotFound()); } DTOPost post = await _uow.PostService.GetPostByPosId(comment.PostId); if (post == null) { return(NotFound()); } if (comment.UserInfoId != userId) { return(BadRequest("It is not your comment")); } await _uow.CommentService.DeleteComment(commentId); return(Ok("Comment deleted")); }
public async Task <IHttpActionResult> AddPost(DTOPost post) { var result = await _postsManager.AddPost(post); return(Ok(result)); }