示例#1
0
        // GET: Book/Index/The Hobbit
        public async Task <ActionResult> Index(string title)
        {
            var works = new List <Work>();
            var books = new List <Library.Models.Book>();


            // if a search, search goodreads api and local repository.
            // otherwise simply return local repository.
            if (!string.IsNullOrEmpty(title))
            {
                // read authentication information from configuration file.
                _token = JsonConvert.DeserializeObject <AuthenticationToken>(System.IO.File.ReadAllText(Server.MapPath(configDataLocation)));

                works = await GoodReadsApiInterface.GetBookAsync(title, _token);

                books = _context.Books.Where(item => item.Title.Contains(title)).ToList();
            }
            else
            {
                // get local library.
                books = _context.Books.Where(item => item.IsDeleted == false).ToList();
            }

            // get list of authors.
            var authors = _context.Authors.Where(item => item.IsDeleted == false).ToList();

            // attach authors to books.
            foreach (var book in books)
            {
                book.Author = authors.FirstOrDefault(a => a.ID == book.AuthorID);
            }

            var viewModel = new BookViewModel
            {
                Works   = works,
                Books   = books,
                Authors = authors
            };

            return(View(viewModel));
        }
示例#2
0
        public async Task Can_Retrieve_Book_By_Api_Search()
        {
            List <Work> books = await GoodReadsApiInterface.GetBookAsync("Hobbit", _token);

            Assert.IsTrue(books.Count > 0);
        }