Exemple #1
0
        public ApiResponse <bool> Return(CommentBookModel model)
        {
            if (model == null)
            {
                throw new Exception("无效的借阅信息。");
            }
            if (!string.IsNullOrEmpty(model.Comment))
            {
                if (model.Rating == null || model.Rating == 0)
                {
                    throw new Exception("请添加评分");
                }
            }
            if (model.Rating != null && model.Rating != 0)
            {
                if (string.IsNullOrEmpty(model.Comment))
                {
                    throw new Exception("请添加评论");
                }
            }
            var response = new ApiResponse <bool>()
            {
                Result = this.BookService.Return(model, this.Member.Id)
            };

            return(response);
        }
        /// <summary>
        /// 归还图书
        /// </summary>
        /// <param name="model">图书评价信息</param>
        /// <param name="userId">借阅用户</param>
        /// <returns>true or false</returns>
        public bool Return(CommentBookModel model, int userId)
        {
            if (model == null)
            {
                throw new InvalidOperationException("图书评价信息无效。");
            }

            using (var dbContext = new MissionskyOAEntities())
            {
                var book = dbContext.Books.FirstOrDefault(it => it.Id == model.BookId);
                if (book == null)
                {
                    throw new InvalidOperationException("借阅图书不存在。");
                }

                var borrow =
                    dbContext.BookBorrows.FirstOrDefault(
                        it =>
                        it.UserId == userId && it.BookId == model.BookId &&
                        it.Status == (int)UserBorrowStatus.Borrowing);

                if (borrow == null)
                {
                    throw new InvalidOperationException("借阅记录不存在。");
                }

                if (!book.Reader.HasValue || book.Reader.Value != userId)
                {
                    throw new InvalidOperationException("图书未被当前用户借出。");
                }

                model.ValidRating(); //验证评分

                //添加评价
                var comment = new BookComment()
                {
                    BookId      = model.BookId,
                    UserId      = userId,
                    Comment     = model.Comment,
                    Rating      = model.Rating,
                    CreatedDate = DateTime.Now
                };

                dbContext.BookComments.Add(comment);

                //更新借书记录
                borrow.Status     = (int)UserBorrowStatus.Returned;
                borrow.ReturnDate = DateTime.Now; //实际归还时间

                //更新图书状态
                book.Status = (int)BookStatus.Stored;
                book.Reader = null;

                dbContext.SaveChanges();
            }

            return(true);
        }
Exemple #3
0
        /// <summary>
        /// 验证评分是否有效
        /// </summary>
        /// <param name="model">评论</param>
        /// <returns>true or false</returns>
        public static bool ValidRating(this CommentBookModel model)
        {
            if (model.Rating > 5)
            {
                throw new InvalidOperationException("最高评分5分。");
            }

            return(true);
        }
        /// <summary>
        /// 添加或修改用户图书评论
        /// </summary>
        /// <param name="model">评论信息</param>
        /// <param name="userId">借阅用户</param>
        /// <returns>是否评论成功</returns>
        public bool Comment(CommentBookModel model, int userId)
        {
            if (model == null)
            {
                throw new InvalidOperationException("图书评价信息无效。");
            }

            using (var dbContext = new MissionskyOAEntities())
            {
                var user = dbContext.Users.SqlQuery("select * from [user] where id = 2");

                if (user != null)
                {
                }

                var book = dbContext.Books.FirstOrDefault(it => it.Id == model.BookId);
                if (book == null)
                {
                    throw new InvalidOperationException("图书不存在。");
                }

                model.ValidRating(); //验证评分

                var comment =
                    dbContext.BookComments.FirstOrDefault(it => model.CommentId.HasValue && it.Id == model.CommentId); //获取评论

                if (comment != null)                                                                                   //编辑评论
                {
                    comment.Rating  = model.Rating;
                    comment.Comment = model.Comment;
                }
                else //添加评价
                {
                    comment = new BookComment()
                    {
                        BookId      = model.BookId,
                        UserId      = userId,
                        Comment     = model.Comment,
                        Rating      = model.Rating,
                        CreatedDate = DateTime.Now
                    };

                    dbContext.BookComments.Add(comment);
                }

                //dbContext.SaveChanges();
            }

            return(true);
        }
Exemple #5
0
        public ApiResponse <bool> Comment(CommentBookModel model)
        {
            if (model == null)
            {
                throw new Exception("无效的评论信息。");
            }

            var response = new ApiResponse <bool>()
            {
                Result = this.BookService.Comment(model, this.Member.Id)
            };

            return(response);
        }