コード例 #1
0
        private int CreateComment()
        {
            using var context = new ScreamDB(contextOptions);

            ScreamBackend.DB.Tables.Scream fakeScream = new ScreamBackend.DB.Tables.Scream
            {
                Content       = "TEST: FAKE SCREAM",
                ContentLength = 17,
                AuthorId      = FakeUser.Id,
            };

            ScreamBackend.DB.Tables.Comment fakeComment = new ScreamBackend.DB.Tables.Comment
            {
                Content  = "TEST: FAKE COMMENT",
                AuthorId = FakeUser.Id,
                Scream   = fakeScream
            };
            context.Screams.Add(fakeScream);
            context.Comments.Add(fakeComment);
            context.SaveChanges();
            return(fakeComment.Id);
        }
コード例 #2
0
ファイル: Scream.cs プロジェクト: ChenDvorak/ScreamBackend
        /// <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");
        }
コード例 #3
0
 internal Comment(ScreamBackend.DB.Tables.Comment model, ScreamBackend.DB.ScreamDB db)
 {
     Model = model;
     _db   = db;
 }