public static async Task CreateFollowNotice(UserFollow userFollow, BSDNContext context) { User follower = await context.Users.FirstOrDefaultAsync(u => u.UserId == userFollow.FollowerId); User following = await context.Users.FirstOrDefaultAsync(u => u.UserId == userFollow.FollowingId); Notice notice = new Notice(following.UserId, $@"New Follower|{follower.Nickname}|{follower.UserId}", null); await context.AddAsync(notice); await context.SaveChangesAsync(); }
public async Task <IActionResult> Post( string type, int id, [FromBody] Comment comment, [FromQuery(Name = "token")] string token) { // 先评论是否有正文 // 再检查Token是否有效 // 再检查评论类型 // 再检查文章/评论是否存在 int replyCommentId = 0; Comment commentResult = null; ModelResult <CommentInfo> result = TokenUtils.CheckToken <CommentInfo>(token, _context); if (result != null) { return(BadRequest(result)); } if (comment.Content == null || comment.CommentId != 0) { result = new ModelResult <CommentInfo>(400, new CommentInfo(comment), "Invalid Comment"); return(BadRequest(result)); } comment.PublishDate = DateTime.Now; if (type == "article" || type == "reply") { Article articleResult; if (type == "article") { if (comment.ArticleId == 0) { comment.ArticleId = id; } if (id == 0) { id = comment.ArticleId; } articleResult = await _context.Articles .FirstOrDefaultAsync(a => a.ArticleId == id); } else // if (type == "reply") { commentResult = await _context.Comments .FirstOrDefaultAsync(c => c.CommentId == id); if (commentResult == null) { result = new ModelResult <CommentInfo>(404, null, "Comment to Reply Not Exists"); return(BadRequest(result)); } replyCommentId = commentResult.CommentId; articleResult = await _context.Articles .FirstOrDefaultAsync(a => a.ArticleId == commentResult.ArticleId); comment.ArticleId = articleResult.ArticleId; } Session sessionResult = await _context.Sessions .FirstOrDefaultAsync(s => s.SessionToken == token); if (articleResult == null) { result = new ModelResult <CommentInfo>(404, null, "Article Not Exists"); return(BadRequest(result)); } comment.UserId = sessionResult.SessionUserId; comment.User = await _context.Users .FirstOrDefaultAsync(u => u.UserId == comment.UserId); comment.ArticleId = id; comment.Article = articleResult; await _context.AddAsync(comment); await _context.SaveChangesAsync(); result = new ModelResult <CommentInfo>(201, new CommentInfo(comment), "Commented"); } else { result = new ModelResult <CommentInfo>(405, null, "Undefined Comment Type"); return(BadRequest(result)); } if (type == "reply" && replyCommentId != 0) { await _context.AddAsync(new CommentReply { CommentId = comment.CommentId, RepliedCommentId = replyCommentId }); await _context.SaveChangesAsync(); result.Message = "Replied"; } int noticeUserId = 0; if (type == "article") { noticeUserId = comment.UserId; } else { if (commentResult != null) { noticeUserId = commentResult.UserId; } } await NoticeUtils.CreateCommentNotice(comment, _context, noticeUserId, type); return(Ok(result)); }