public IActionResult Post(AddPostDTO addPostDTO) { try { var CurrentUser = _context.Users.Find(userManager.GetUserId(User)); var existingAccount = _userStore.GetByIdentityUserId(CurrentUser.Id); if (existingAccount.AccountStatus.Equals(Status.Suspended)) { signInManager.SignOutAsync(); } if (ModelState.IsValid) { var Post = new Post { Content = addPostDTO.Content, UserId = CurrentUser.Id }; _postStore.AddPost(Post); existingAccount.Posts.Add(Post); return(RedirectToAction("AllPosts")); } return(View(addPostDTO)); } catch (Exception ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts)); } }
public async Task <IActionResult> Put(AddPostModel model) { var postType = await TypeSvc.GetByIdAsync(model.PostTypeId); if (postType == null) { return(new JsonResult(new APIResult <long> { ErrorMsg = "帖子类型不存在" }) { StatusCode = 400 }); } AddPostDTO dto = new AddPostDTO(); dto.Content = model.Content; dto.Title = model.Title; dto.UserId = model.UserId; dto.PostTypeId = model.PostTypeId; if (postType.Name == "分享") { dto.PostStatusId = 1; } else { dto.PostStatusId = 2; } return(new JsonResult(new APIResult <long> { Data = await PostSvc.AddNewAsync(dto) })); }
public async Task <long> AddNewAsync(AddPostDTO dto) { PostEntity entity = new PostEntity(); entity.Content = dto.Content; entity.PostStatusId = dto.PostStatusId; entity.PostTypeId = dto.PostTypeId; entity.Title = dto.Title; entity.UserId = dto.UserId; using (PostContext ctx = new PostContext()) { await ctx.Posts.AddAsync(entity); await ctx.SaveChangesAsync(); return(entity.Id); } }