public IActionResult GetPostModalPostParts([FromQuery] PostIdModel model) { ICollection <PostPartDisplay> ret; ret = _postDataService.GetPostParts(model.PostId); return(Ok(Json(ret))); }
public async Task <ResultData <PostIdModel> > EditPost(int postId, string title, string content, string category) { if (postId == 0) { return(new ResultData <PostIdModel>(Constants.PostNotFoundMessage, false, null)); } if (string.IsNullOrEmpty(title)) { return(new ResultData <PostIdModel>(Constants.InvalidTitleMessage, false, null)); } if (string.IsNullOrEmpty(content)) { return(new ResultData <PostIdModel>(Constants.InvalidContentMessage, false, null)); } if (string.IsNullOrEmpty(category)) { return(new ResultData <PostIdModel>(Constants.InvalidCategoryMessage, false, null)); } var post = await this.db.Posts.Include(p => p.Author).Include(p => p.Comments).FirstOrDefaultAsync(p => p.Id == postId); if (post == null) { return(new ResultData <PostIdModel>(Constants.PostNotFoundMessage, false, null)); } var posts = await this.db.Posts.Where(p => p.Id != postId).ToArrayAsync(); if (posts.Any(p => p.Title == title)) { return(new ResultData <PostIdModel>(Constants.PostExistsMessage, false, null)); } Category postCategory; var tryParseCategory = Enum.TryParse(category, true, out postCategory); if (!tryParseCategory) { return(new ResultData <PostIdModel>(Constants.InvalidCategoryMessage, false, null)); } post.Title = title; post.Content = content; post.Category = postCategory; this.db.Posts.Update(post); await this.db.SaveChangesAsync(); var model = new PostIdModel { Id = post.Id }; return(new ResultData <PostIdModel>(string.Empty, true, model)); }
public IActionResult DeletePost([FromBody] PostIdModel model) { DeletePostReturnModel ret; try { Claim idClaim = User.FindFirst("sub"); if (idClaim == null) { return(Unauthorized()); } string userId = idClaim.Value; ret = _postActionService.DeletePost(model.PostId, userId); return(Ok(ret)); } catch (Exception) { return(BadRequest()); } }
public IActionResult LikePost([FromBody] PostIdModel model) { var user = HttpContext.User; LikePostReturn ret = new LikePostReturn(); try { Claim idClaim = User.FindFirst("sub"); Claim userNameClaim = User.FindFirst("nickname"); if (idClaim == null) { ret.ErrorInformation.ErrorType = ErrorType.RedirectAuth; ret.ErrorInformation.RedirectUrl = ""; ret.IsActionSucceed = false; return(Ok(Json(ret))); } string userId = idClaim.Value; ret = _postActionService.LikePost(model.PostId, userId); if (ret.IsActionSucceed) { _eventBus.Publish <UserPostFavouritedAction>(new UserPostFavouritedAction() { UserId = userId, PostId = model.PostId, PostAuthorId = ret.PostAuthorId, DateUtcAction = DateTime.UtcNow, Username = userNameClaim.Value }, "#"); } return(Ok(Json(ret))); } catch (Exception) { ret.ErrorInformation.ErrorType = ErrorType.NoAction; ret.ErrorInformation.UserInformation = "Sorry, an error occured!"; ret.IsActionSucceed = false; return(Ok(Json(ret))); } }