public void RentBook(RentBook source)
        {
            if (source == null)
            {
                throw new Exception("借用书籍出错");
            }

            using (var db = Heart.CreateBookDbContext())
            {
                var result = new RentBook()
                {
                    BookId           = source.BookId,
                    Id               = Guid.NewGuid(),
                    RentDate         = source.RentDate,
                    Comments         = source.Comments,
                    IsReturn         = false,
                    ReturnDate       = source.ReturnDate,
                    UserId           = source.UserId,
                    CreatedTime      = DateTime.Now,
                    ActualReturnDate = new DateTime(1900, 1, 1)
                };

                db.RentBooks.Add(result);
                db.SaveChanges();
            }
        }
Example #2
0
        public void Update(RentBook data)
        {
            using (var tx = new SafeTx(Configuration))
            {
                using (var cmd = tx.GetCommand())
                {
                    cmd.CommandText = @"EXEC SpUpdateRent @ID int,
	                                    @BookId 
	                                    @PricePerDay ,
	                                    @RentLength ,
	                                    @StartDate ,
	                                    @EndDate ,
	                                    @UserName ,
	                                    @Status;"    ;

                    cmd.Parameters.AddWithValue("@ID", data.ID);
                    cmd.Parameters.AddWithValue("@BookId", data.BookID);
                    cmd.Parameters.AddWithValue("@PricePerDay", data.PricePerDay);
                    cmd.Parameters.AddWithValue("@RentLength", data.RentLenght);
                    cmd.Parameters.AddWithValue("@StartDate", data.StartDate);
                    cmd.Parameters.AddWithValue("@EndDate", data.EndDate);
                    cmd.Parameters.AddWithValue("@UserName", data.UserName);
                    cmd.Parameters.AddWithValue("@Status", data.Status);
                    cmd.ExecuteNonQuery();
                }
            }
        }
Example #3
0
        public int Create(RentBook data)
        {
            int result = 0;

            using (var tx = new SafeTx(Configuration))
            {
                using (var cmd = tx.GetCommand())
                {
                    cmd.CommandText = @"INSERT INTO RentBook
                                            (BookID,PricePerDay,RentLenght,StartDate,EndDate,UserName,Status)
                                    VALUES  (@BookID,@PricePerDay,@RentLenght,@StartDate,@EndDate,@UserName,1);
                                        
                                    SELECT SCOPE_IDENTITY()";

                    cmd.Parameters.AddWithValue("@BookID", data.BookID);
                    cmd.Parameters.AddWithValue("@PricePerDay", data.PricePerDay);
                    cmd.Parameters.AddWithValue("@RentLenght", data.RentLenght);
                    cmd.Parameters.AddWithValue("@StartDate", data.StartDate);
                    cmd.Parameters.AddWithValue("@EndDate", data.EndDate);
                    cmd.Parameters.AddWithValue("@UserName", data.UserName);

                    object lastId = cmd.ExecuteScalar();
                    lastId = (lastId == DBNull.Value) ? null : lastId;
                    result = Convert.ToInt32(lastId);
                }
            }

            return(result);
        }
        public ActionResult Create(RentBook data)
        {
            var listBooks   = BookMgr.GetListBook();
            var bookOptions = new SelectList(listBooks.Select(org =>
            {
                return(new SelectListItem {
                    Text = org.BookTitle, Value = org.ID.ToString()
                });
            }), "Value", "Text");

            ViewData["BookOptions"] = bookOptions;

            var book = BookMgr.GetBookByID(data.BookID);

            data.RentLenght  = (data.EndDate - data.StartDate).Days;
            data.PricePerDay = book.RentPrice;
            data.UserName    = User.Identity.Name;

            RentMgr.Create(data);

            ModelState.Clear();
            TempData["Message"] = "rent successfully saved";

            return(View(data));
        }
Example #5
0
        public IEnumerable <IEvent> GetEventsForReader(Reader reader)
        {
            List <IEvent> userEvents = new List <IEvent>();

            foreach (IEvent ievent in GetAllEvents())
            {
                if (ievent.GetEventType() == EventType.AddReader ||
                    ievent.GetEventType() == EventType.UpdateReader ||
                    ievent.GetEventType() == EventType.DeleteReader)
                {
                    EventReader eventReader = ievent as EventReader;
                    if (eventReader.Reader == reader)
                    {
                        userEvents.Add(eventReader);
                    }
                }
                else if (ievent.GetEventType() == EventType.RentBook)
                {
                    RentBook rentBook = ievent as RentBook;
                    if (rentBook.Reader == reader)
                    {
                        userEvents.Add(rentBook);
                    }
                }
                else if (ievent.GetEventType() == EventType.ReturnBook)
                {
                    ReturnBook returnBook = ievent as ReturnBook;
                    if (returnBook.Reader == reader)
                    {
                        userEvents.Add(returnBook);
                    }
                }
            }
            return(userEvents);
        }
Example #6
0
        public void Return_Successful_Return()
        {
            rentBookMock = bookRentalMock.RentReturnBook();
            var bookDal = new BookStoreDataMock(bookRentalMock.StoredBooks2());
            var result  = new BookRental(bookDal).ReturnBook(rentBookMock);

            Assert.True(result.Successful);
        }
Example #7
0
        public void Return_Unsuccessful_When_Book_Is_Not_Stored()
        {
            rentBookMock = bookRentalMock.RentReturnBook();
            var bookDal = new BookStoreDataMock(bookRentalMock.StoredBooks());
            var result  = new BookRental(bookDal).RentBook(rentBookMock);

            Assert.False(result.Successful);
        }
Example #8
0
        public async Task <RentBook> CreateAsync(RentBook entity)
        {
            await _applicationDbContext.RentBook.AddAsync(entity);

            await _applicationDbContext.SaveChangesAsync();

            return(entity);
        }
Example #9
0
        public async Task <IActionResult> Create(RentBook rentBook)
        {
            if (ModelState.IsValid)
            {
                await _rentBookRepository.CreateAsync(rentBook);
            }

            return(RedirectToAction(nameof(Index)));
        }
Example #10
0
        private void submit_btn_Click(object sender, EventArgs e)
        {
            try
            {
                var book = BizManager.BooksBiz.GetBookByBookNumber(this.bookNumber_txt.Text);

                if (book == null)
                {
                    MessageBox.Show("未找到可借阅的书籍");
                    return;
                }

                if (DateTime.Parse(this.bookRentDate_dp.Text) >= DateTime.Parse(this.bookReturn_dp.Text))
                {
                    MessageBox.Show("借书日期不能小于或等于还书日期");
                    return;
                }

                if (string.IsNullOrEmpty(this.userId_txt.Text))
                {
                    MessageBox.Show("用户信息不能为空");
                    return;
                }


                var rentDate         = DateTime.Parse(this.bookRentDate_dp.Text);
                var returnDate       = DateTime.Parse(this.bookReturn_dp.Text);
                var actualReturnDate = new DateTime(1900, 1, 1);

                if (!BizManager.UserRentBiz.CanRentBook(Guid.Parse(this.userId_txt.Text)))
                {
                    MessageBox.Show("用户借阅超过五本,无法再借");
                    return;
                }

                RentBook rb = new RentBook()
                {
                    Id               = Guid.NewGuid(),
                    BookId           = book.Id,
                    UserId           = Guid.Parse(this.userId_txt.Text),
                    IsReturn         = false,
                    RentDate         = rentDate,
                    ReturnDate       = returnDate,
                    ActualReturnDate = actualReturnDate,
                    Comments         = string.Empty
                };

                BizManager.UserRentBiz.RentBook(rb);
                this.Hide();
                successCallback();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public IList <UsersReadedBooks> GetAllReadedBooks()
        {
            /*
             *      select u.Id, u.FirstName, u.LastName,
             *                  (
             *                          Select count(od.BookId) from OrderDetails od
             *                          inner join [Order] o on od.OrderId = o.Id
             *                          where u.Id = o.UserId
             *                  ) as 'Total Books'
             *
             *          from [User] u
             *          Order by 'Total Books' desc
             */
            UsersReadedBooks usersReadedAllias     = null;
            RentBook         rentAlias             = null;
            RentBookDetails  rentBookDetailsAllias = null;
            User             userAlias             = null;

            //var allReadedBooks = QueryOver.Of(() => rentAlias)
            //    .JoinAlias(() => rentAlias.RentDetails, () => rentBookDetailsAllias)
            //    .JoinAlias(() => rentAlias.User, () => userAlias);
            //    //.Where(() => rentAlias.User.Id == userAlias.Id);

            //var usersWithBooks = _session.QueryOver(() => rentAlias)
            //    .JoinAlias(() => rentAlias.User, () => userAlias)

            //    .JoinQueryOver(() => rentAlias.RentDetails, () => rentBookDetailsAllias)
            //    .WithSubquery.WhereProperty(x => x.Book).In(allReadedBooks)
            //    .SelectList(list => list
            //        .Select(u => u.User.FirstName).WithAlias(() => usersReadedAllias.userName)
            //        .Select(() => userAlias).WithAlias(() => usersReadedAllias.bookName));

            //return usersWithBooks.List<UsersReadedBooks>();

            var usersWithBooks = _session.QueryOver <RentBookDetails>()
                                 .JoinQueryOver(x => x.RentBook, () => rentAlias)
                                 .JoinQueryOver(() => rentAlias.User, () => userAlias)

                                 .SelectList(x => x
                                             .SelectGroup(u => userAlias.FirstName).WithAlias(() => usersReadedAllias.FirstName)
                                             .SelectGroup(u => userAlias.LastName).WithAlias(() => usersReadedAllias.LastName)
                                             .SelectCount(c => rentAlias.Id).WithAlias(() => usersReadedAllias.BookCount)
                                             )
                                 .TransformUsing(Transformers.AliasToBean <UsersReadedBooks>());

            return(usersWithBooks.List <UsersReadedBooks>());
        }
        public ActionResult Create()
        {
            RentBook data = new RentBook();

            var listBooks   = BookMgr.GetListBook();
            var bookOptions = new SelectList(listBooks.Select(org =>
            {
                return(new SelectListItem {
                    Text = org.BookTitle, Value = org.ID.ToString()
                });
            }), "Value", "Text");

            ViewData["BookOptions"] = bookOptions;


            return(View(data));
        }
Example #13
0
        private RentBook CreateObject(SqlDataReader reader)
        {
            RentBook result = new RentBook();

            result.ID                 = reader.GetInt32(reader.GetOrdinal("ID"));
            result.BookID             = reader.GetInt32(reader.GetOrdinal("BookID"));
            result.PricePerDay        = reader.GetDecimal(reader.GetOrdinal("PricePerDay"));
            result.PricePerDayDisplay = String.Format("{0:N0}", result.PricePerDay);
            result.RentLenght         = reader.GetInt32(reader.GetOrdinal("RentLenght"));
            result.StartDate          = reader.GetDateTime(reader.GetOrdinal("StartDate"));
            result.EndDate            = reader.GetDateTime(reader.GetOrdinal("EndDate"));
            result.UserName           = reader.GetString(reader.GetOrdinal("UserName"));
            result.Book               = BookMgr.GetBookByID(result.BookID);
            result.Total              = String.Format("{0:N0}", (result.RentLenght * result.PricePerDay));
            result.Status             = reader.GetInt16(reader.GetOrdinal("Status"));

            return(result);
        }
Example #14
0
        public ActionResult RentBooks(int bookID)
        {
            var email = HttpContext.Request.Cookies["user"].Value.ToString();

            using (var context = new LibraryContext())
            {
                var user = context.Users.SingleOrDefault(u => u.user_mail.ToLower() == email.ToLower());
                var data = new User
                {
                    user_id = user.user_id
                };

                if (HttpContext.Request.Cookies.AllKeys.Contains("user"))
                {
                    RentBook.RentingBook(user.user_id, bookID);
                }
            }


            return(RedirectToAction("Index"));
        }
        public ActionResult Rent(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("Books"));
            }
            Book book = booksList.Single(m => m.index == id);

            if (book == null)
            {
                return(RedirectToAction("Books"));
            }
            if (book.copies == 0)
            {
                return(RedirectToAction("Books"));
            }
            RentBook en = new RentBook();

            en.book = book;
            return(View(en));
        }
Example #16
0
        public RentBook GetByID(int id)
        {
            RentBook result = new RentBook();

            using (var tx = new SafeTx(Configuration))
            {
                using (var cmd = tx.GetCommand())
                {
                    cmd.CommandText = @"SELECT ID,BookID,PricePerDay,RentLenght,StartDate,EndDate,UserName,Status FROM RentBook WHERE ID = @ID";
                    cmd.Parameters.AddWithValue("@ID", id);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            result = CreateObject(reader);
                        }
                    }
                }
            }

            return(result);
        }
Example #17
0
        public List <RentBook> GetList()
        {
            List <RentBook> result = new List <RentBook>();

            using (var tx = new SafeTx(Configuration))
            {
                using (var cmd = tx.GetCommand())
                {
                    cmd.CommandText = @"SELECT ID,BookID,PricePerDay,RentLenght,StartDate,EndDate,UserName,Status FROM RentBook WHERE Status = 1";

                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            RentBook data = CreateObject(reader);
                            result.Add(data);
                        }
                    }
                }
            }

            return(result);
        }
        public IList <Book> GetCurrentBooks(long userId)
        {
            Book     book     = null;
            RentBook rentBook = null;

            var currentBooks = _session.QueryOver <RentBookDetails>()
                               .JoinAlias(x => x.RentBook, () => rentBook)
                               .Where(x => x.EndDate > DateTime.Now && rentBook.User.Id == userId)
                               .Select(Projections.Conditional(Restrictions.Where(() => book.Id > 0),
                                                               Projections.Constant("", NHibernateUtil.String),
                                                               Projections.Constant("", NHibernateUtil.String)))
                               .List <Book>();

            return(currentBooks);


            /*
             *           string query = @"select b.Name from [Book] b
             *              join [RentBookDetails] od on b.Id = od.BookId
             *              join [RentBook] o on od.OrderId = o.Id
             *                  where od.EndDate > Getdate() and o.UserId = Id";
             */
        }
Example #19
0
        private Result <string> RentReturnBook(RentBook rent, Func <int, int, int> delegateOpeBook)
        {
            var result = new Result <string>();

            try
            {
                ruleContext.RuleValidation(rent);

                var booksStored = bookData.GetStoredBooks();

                var bookToRent = booksStored.Find(b => b.Book.Title == rent.BookStore.Book.Title);

                ruleContext.RuleValidation(bookToRent);

                foreach (var item in booksStored)
                {
                    if (item.Book.Title == bookToRent.Book.Title)
                    {
                        item.Quantity = delegateOpeBook(item.Quantity, rent.BookStore.Quantity);
                    }
                }

                var bookInStock = booksStored.Find(b => b.Book.Title == rent.BookStore.Book.Title);

                result.Message    = "Successful";
                result.Successful = true;
                result.Content    = $"the store has {bookInStock.Quantity} units of a book titled {bookInStock.Book.Title}";
            }
            catch (Exception ex)
            {
                result.Successful = false;
                result.Message    = ex.Message;
            }

            return(result);
        }
Example #20
0
        public Result <string> ReturnBook(RentBook rent)
        {
            Func <int, int, int> delegateReturnBook = (currentStock, rentQuantity) => currentStock + rentQuantity;

            return(RentReturnBook(rent, delegateReturnBook));
        }
Example #21
0
 public Task <RentBook> UpdateAsync(RentBook entity)
 {
     throw new NotImplementedException();
 }
Example #22
0
 public RentViewModel( )
 {
     rentBook       = new RentBook(this);
     SelectRentBook = new RentEntity();
 }
        public IActionResult Edit(int id)
        {
            RentBook data = RentMgr.GetByID(id);

            return(View(data));
        }