Beispiel #1
0
        public void OnPost()
        {
            if (!ModelState.IsValid)
            {
                // TODO: Show error messages
                return;
            }

            // TODO: If the book has been borrowed for the current period, return an error message
            var borrower = this.Context.Borrowers.Find(this.BorrowerId);
            int bookId   = Convert.ToInt32(this.RouteData.Values["id"]);
            var book     = this.Context.Books.Find(bookId);

            if (borrower == null || book == null)
            {
                // TODO: ModelState.AddModelError()
                return;
            }

            var borrowedBook = new BorrowersBooks()
            {
                BookId     = book.Id,
                BorrowerId = borrower.Id,
                StartDate  = this.StartDate,
                EndDate    = this.EndDate
            };

            this.Context.BorrowedBooks.Add(borrowedBook);
            this.Context.SaveChanges();
        }
Beispiel #2
0
        public IActionResult OnPost(  )
        {
            if (ModelState.IsValid)
            {
                var borrowedBook = new BorrowersBooks()
                {
                    BookId     = Id,
                    BorrowerId = Borrower
                };

                context.BorrowedBooks.Add(borrowedBook);
                this.context.SaveChanges();
            }
            return(RedirectToPage("/"));
        }
Beispiel #3
0
        public IActionResult OnPost(int id)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.Page());
            }

            var book = this.context
                       .Books
                       .FirstOrDefault(b => b.Id == id);

            var borrower = this.context
                           .Borrowers
                           .FirstOrDefault(b => b.Name == this.BorrowBookModel.Borrower);

            var borrowerBook = new BorrowersBooks
            {
                BookId     = book.Id,
                BorrowerId = borrower.Id
            };

            borrower.BorrowedBooks.Add(borrowerBook);

            book.Status = "Borrowed";

            var bookRecord = new BooksRecords
            {
                BookId = id,
                Record = new Record
                {
                    StartDate = this.BorrowBookModel.StartDate,
                    EndDate   = this.BorrowBookModel.EndDate
                }
            };

            book.Records.Add(bookRecord);

            this.context.SaveChanges();

            return(this.RedirectToPage("/Index"));
        }
Beispiel #4
0
        public IActionResult OnPost(int id)
        {
            var book = this.Context.Books
                       .Include(b => b.Borrowers)
                       .FirstOrDefault(b => b.Id == id);


            var borrower = this.Context.Borrowers.FirstOrDefault(b => b.Id == this.BorrowerId);

            if (borrower == null)
            {
                return(Page());
            }

            var bookBorrowers = new BorrowersBooks()
            {
                Book     = book,
                Borrower = borrower
            };

            book.Borrowers.Add(bookBorrowers);
            borrower.BorrowedBooks.Add(bookBorrowers);

            var borrowHistory = new BorrowHistory()
            {
                Book       = book,
                Borrower   = borrower,
                BorrowDate = this.BorrowDate,
                ReturnDate = this.ReturnDate
            };

            this.Context.BorrowHistories.Add(borrowHistory);

            book.History.Add(borrowHistory);
            book.Status = "Borrowed";
            this.Context.SaveChanges();
            return(RedirectToPage("/Index"));
        }
        public IActionResult OnPost(int id)
        {
            if (ModelState.IsValid)
            {
                using (var context = new BookLibraryContext())
                {
                    this.Book = context.Books.Find(id);

                    if (context.Borrowers.Any(b => b.Name == this.Name && b.Address == this.Address))
                    {
                        Borrower borrower = context.Borrowers.FirstOrDefault(b => b.Name == this.Name && b.Address == this.Address);

                        BorrowersBooks borrowersBooks = new BorrowersBooks
                        {
                            Book       = this.Book,
                            BookId     = this.Book.Id,
                            BorrowerId = borrower.Id,
                            Borrower   = borrower,
                            StartDate  = this.StartDate,
                            EndDate    = this.EndDate
                        };

                        this.Book.Status = "Borrowed";
                        this.Book.Borrowers.Add(borrowersBooks);

                        context.Books.Update(this.Book);
                        context.SaveChanges();
                    }
                    else
                    {
                        Borrower borrower = new Borrower
                        {
                            Name    = this.Name,
                            Address = this.Address
                        };

                        context.Borrowers.Add(borrower);
                        context.SaveChanges();

                        BorrowersBooks borrowersBooks = new BorrowersBooks
                        {
                            Book       = this.Book,
                            BookId     = this.Book.Id,
                            BorrowerId = borrower.Id,
                            Borrower   = borrower,
                            StartDate  = this.StartDate,
                            EndDate    = this.EndDate
                        };

                        this.Book.Status = "Borrowed";
                        this.Book.Borrowers.Add(borrowersBooks);

                        context.Books.Update(this.Book);
                        context.SaveChanges();
                    }

                    return(RedirectToPage("/Index"));
                }
            }
            return(Page());
        }