private async void LoadBookshelf()
        {
            Bookshelf.Children.Clear();

            var books = await _bookshelfService.LoadBooks();

            foreach (var book in books)
            {
                var bc = new BookCard(book);
                bc.OnOpenBook   += OpenBook;
                bc.OnDeleteBook += DeleteBook;
                Bookshelf.Children.Add(bc);
            }
        }
        public ActionResult Index(int bookId, string category, string searchString, int resultsPerPage = 10, int page = 1)
        {
            var      book = db.Books.Single(b => b.ID == bookId);
            BookCard bc;
            bool     conflict = false;

            //no one is reading it? borrow!
            if (book.Reader == null)
            {
                bc = new BookCard
                {
                    BookID     = bookId,
                    BorrowDate = DateTime.Now,
                    Reader     = User.Identity.Name
                };
                book.Reader = User.Identity.Name;
                db.Cards.Add(bc);
                db.Entry(book).State = EntityState.Modified;
            }
            //user is the reader? return!
            else if (book.Reader == User.Identity.Name)
            {
                bc = db.Cards.Single(c => c.BookID == bookId && c.ReturnDate == null);

                bc.ReturnDate        = DateTime.Now;
                book.Reader          = null;
                db.Entry(bc).State   = EntityState.Modified;
                db.Entry(book).State = EntityState.Modified;
            }
            else //this will probably only happen if the book was borrowed by someone else
                 //after the page was loaded but before the button click
            {
                conflict = true;
            }
            db.SaveChanges();

            if (Request.IsAjaxRequest())
            {
                return(Content((HtmlHelpers.GetButton(bookId, book.Reader, User.Identity.Name, conflict)).ToHtmlString()));
            }

            var books = db.Books.Where(b => b.Status == true).ToList();

            var model = BuildIndexViewModel(category, searchString, resultsPerPage, page, books);

            return(View(model));
        }
Exemple #3
0
 public async Task AddAsync(BookCard bookCard)
 {
     await Task.FromResult(_bookCards.Add(bookCard));
 }