public override async Task <ScreamResult> RemoveAsync(int screamId) { if (!Scream.IsValidId(screamId)) { throw new NullReferenceException("invalid scream Id"); } var scream = await DB.Screams.AsNoTracking().Where(s => s.Id == screamId).SingleOrDefaultAsync(); var comments = await DB.Comments.AsNoTracking().Where(c => c.ScreamId == screamId).ToListAsync(); DB.Comments.RemoveRange(comments); if (scream == null) { throw new NullReferenceException("scream not exist"); } DB.Screams.Remove(scream); int effects = await DB.SaveChangesAsync(); if (effects == comments.Count + 1) { await _redis.KeyDeleteAsync(Scream.GetCacheKey(screamId)); return(QuickResult.Successful()); } return(QuickResult.Unsuccessful("Delete scream fail")); }
public override async Task <ScreamResult <int> > PostScreamAsync(Models.NewScreamtion model) { const int NOT_DATA = 0; if (string.IsNullOrWhiteSpace(model.Content)) { return(QuickResult.Unsuccessful(NOT_DATA, "内容不能为空")); } if (!await DB.Users.AnyAsync(user => user.Id == model.AuthorId)) { return(QuickResult.Unsuccessful(NOT_DATA, "该作者不存在")); } var newScream = new ScreamBackend.DB.Tables.Scream { AuthorId = model.AuthorId, Content = model.Content, ContentLength = model.Content.Length, CreateDateTime = DateTime.Now, State = (int)Scream.Status.WaitAudit }; await DB.Screams.AddAsync(newScream); int effects = await DB.SaveChangesAsync(); if (effects == 1) { return(QuickResult.Successful(newScream.Id)); } throw new Exception("发布失败"); }
/// <summary> /// Post comment for scream /// </summary> /// <param name="scream"></param> /// <param name="comment"></param> /// <returns></returns> public async Task <ScreamResult> PostCommentAsync(Models.NewComment comment) { if (comment.Author == null) { throw new NullReferenceException("scream or model can't be null"); } if (string.IsNullOrWhiteSpace(comment.Content)) { return(QuickResult.Unsuccessful("评论内容不能为空")); } if (comment.Content.Length < Comments.AbstractCommentsManager.COMMENT_MIN_LENGTH) { return(QuickResult.Unsuccessful($"评论内容必须大于{Comments.AbstractCommentsManager.COMMENT_MIN_LENGTH}个字")); } if (comment.Content.Length > Comments.AbstractCommentsManager.COMMENT_MAX_LENGTH) { return(QuickResult.Unsuccessful($"评论内容必须小于{Comments.AbstractCommentsManager.COMMENT_MAX_LENGTH}个字")); } var newComment = new ScreamBackend.DB.Tables.Comment { ScreamId = Model.Id, Content = comment.Content, AuthorId = comment.Author.Id, State = (int)Comments.Comment.Status.WaitAudit }; await _db.Comments.AddAsync(newComment); int effects = await _db.SaveChangesAsync(); if (effects == 1) { return(QuickResult.Successful()); } throw new Exception("post comment fail"); }
public async Task <ScreamResult> RemoveCommentAsync(int commentId) { if (!Comments.AbstractCommentsManager.IsValidCommentId(commentId)) { throw new ArgumentOutOfRangeException("comment Id is not a valid integer"); } var comment = await _db.Comments.AsNoTracking() .Where(c => c.Id == commentId) .SingleOrDefaultAsync(); if (comment == null) { return(QuickResult.Unsuccessful("该评论不存在")); } _db.Comments.Remove(comment); int effected = await _db.SaveChangesAsync(); if (effected == 1) { return(QuickResult.Successful()); } throw new Exception($"remove comment fail: Id: {commentId}"); }