public GoogleBook GetBookByTitleAndAuthor(string title, string authorName)
        {
            SearchResult searchResult = SearchByTitleAndAuthor(title, authorName).Result;

            if (searchResult.items != null)
            {
                string     bookId = searchResult.items[0].id;
                GoogleBook book   = GetBookByGoogleId(bookId).Result;
                return(book);
            }
            return(null);
        }
        public async Task <GoogleBook> GetBookByGoogleId(string id)
        {
            HttpResponseMessage response = _client.GetAsync($"https://www.googleapis.com/books/v1/volumes/{id}").Result;

            if (response.IsSuccessStatusCode)
            {
                GoogleBook book = await response.Content.ReadAsAsync <GoogleBook>();

                return(book);
            }
            return(null);
        }
        public List <GoogleBook> GetBooksByAuthor(string authorName)
        {
            SearchResult result = SearchBooksByAuthor(authorName).Result;

            if (result.items.Count > 0)
            {
                List <GoogleBook> authorsBooks = new List <GoogleBook>();
                foreach (searchItem item in result.items)
                {
                    GoogleBook book = GetBookByGoogleId(item.id).Result;
                    authorsBooks.Add(book);
                }
                return(authorsBooks);
            }
            return(null);
        }
示例#4
0
 public static Book Map(GoogleBook googleBook)
 {
     return(new Book
     {
         Title = googleBook.Title,
         SubTitle = googleBook.SubTitle,
         Authors = googleBook.Authors,
         Publisher = googleBook.Publisher,
         PublishedDate = googleBook.PublishedDate,
         Description = googleBook.Description,
         PageCount = googleBook.PageCount,
         PrintType = googleBook.PrintType,
         Categories = googleBook.Categories,
         AverageRating = googleBook.AverageRating,
         ThumbNail = googleBook.ThumbNail,
         Language = googleBook.Language,
         CanonicalVolumeLink = googleBook.CanonicalVolumeLink,
         WebReaderLink = googleBook.WebReaderLink,
         PdfLink = googleBook.PdfLink,
         Isbn10 = googleBook.ISBN10,
         Isbn13 = googleBook.ISBN13
     });
 }
        public static BookItem Adapt( GoogleBook.Item book )
        {
            if( book.VolumeInfo.Title == null ) {
                throw new ArgumentNullException();
            }

            var item = new BookItem {Title = book.VolumeInfo.Title, Author = ""};

            if( book.VolumeInfo.Authors != null ) {

                item.Author = string.Join( ", ", book.VolumeInfo.Authors );
            }

            item.Isbn = "";

            if( book.VolumeInfo.IndustryIdentifiers != null ) {

                foreach ( GoogleBook.Item._VolumeInfo.IndustryIdentifierItem id
                    in book.VolumeInfo.IndustryIdentifiers ) {
                    if( id.Type.StartsWith( "ISBN_10" ) ) {
                        item.Isbn = id.Identifier;
                    }
                }
            }

            if( item.Isbn.Length == 0 ) {
                throw new ArgumentNullException();
            }

            item.ThumbnailUrl = "";
            if( ( book.VolumeInfo.ImageLinks != null ) &&
                ( book.VolumeInfo.ImageLinks.Thumbnail != null ) ) {
                item.ThumbnailUrl = book.VolumeInfo.ImageLinks.Thumbnail;
            }

            return item;
        }
示例#6
0
        public bool AddBookByTitleAndAuthor(string title, string authorName)
        {
            GoogleBook googleBook = service.GetBookByTitleAndAuthor(title, authorName);
            Author     author     = _context.Authors.FirstOrDefault(a => a.FirstName == googleBook.AuthorFirstName &&
                                                                    a.LastName == googleBook.AuthorLastName);

            if (author == default)
            {
                throw new Exception($"Author with First name: {googleBook.AuthorFirstName}," +
                                    $"and Last Name: {googleBook.AuthorLastName} not found," +
                                    $" you must add author before book adding book.");
            }

            Book bookToAdd = new Book
            {
                Title       = googleBook.VolumeInfo.Title,
                Genre       = googleBook.Genre,
                Description = googleBook.Description,
                AuthorId    = author.AuthorId
            };

            _context.Books.Add(bookToAdd);
            return(_context.SaveChanges() == 1);
        }
示例#7
0
        public async Task <JsonResult <Object> > PlaceOrder(string BuyerID, string BooksIDs)
        {
            string results = string.Empty;

            decimal bookpoolCharges = Global.Globals.GetBookpoolCharges();

            try
            {
                using (var db = new BookPoolEntities())
                {
                    decimal    totalPrice     = 0;
                    List <int> booksIDsInCart = BooksIDs.Split(',').Select(int.Parse).ToList();
                    foreach (var bookID in booksIDsInCart)
                    {
                        AvailableBook availableBook = db.AvailableBooks.FirstOrDefault(x => x.ID == bookID);
                        totalPrice += (availableBook.Price + bookpoolCharges);
                    }

                    OrderHeader orderHeader = new OrderHeader();
                    orderHeader.ClientUserID = BuyerID;
                    orderHeader.OrderedOn    = DateTime.Now;
                    orderHeader.Status       = Globals.OrderStatusPending;
                    orderHeader.TotalPrice   = totalPrice;


                    db.OrderHeaders.Add(orderHeader);

                    db.SaveChanges();

                    results = orderHeader.ID.ToString() + orderHeader.OrderedOn.ToString("ddMMyy");

                    UserCart userCart = db.UserCarts.FirstOrDefault(x => x.UserID == BuyerID);

                    foreach (var bookID in booksIDsInCart)
                    {
                        AvailableBook availableBook = db.AvailableBooks.FirstOrDefault(x => x.ID == bookID);
                        if (availableBook != null)
                        {
                            availableBook.SellingStatus = Global.Globals.BookSellingStatus_NotAvailable;

                            OrderDetail orderDetail = new OrderDetail();
                            orderDetail.BookName      = availableBook.BookName;
                            orderDetail.BookPrice     = availableBook.Price + bookpoolCharges;
                            orderDetail.OrderHeaderID = orderHeader.ID;
                            orderDetail.Status        = Globals.OrderStatusPending;
                            orderDetail.SellerUserID  = availableBook.OwnerUserID;
                            orderDetail.BookID        = availableBook.ID;

                            GoogleBook googleResult = new GoogleBook();
                            using (var client = new HttpClient())
                            {
                                client.BaseAddress = new Uri(DataObjects.Global.Globals.googleBaseAPI_SearchByID);
                                StringBuilder httpRoute = new StringBuilder();
                                httpRoute.Append(availableBook.GoogleID);

                                var response = await client.GetAsync(httpRoute.ToString());

                                if (response.IsSuccessStatusCode)
                                {
                                    googleResult = await response.Content.ReadAsAsync <GoogleBook>();
                                }
                            }

                            orderDetail.BookImage = googleResult.volumeInfo.imageLinks.thumbnail.Replace("http:", "https:");
                            orderDetail.Authors   = string.Join(",", googleResult.volumeInfo.authors);
                            orderDetail.GoogleID  = googleResult.id;

                            db.OrderDetails.Add(orderDetail);

                            db.SaveChanges();
                        }


                        if (userCart != null)
                        {
                            List <int> userCartBooks = userCart.BooksIDsCSV.Split(',').Select(int.Parse).ToList();
                            userCartBooks.Remove(bookID);
                            userCart.BooksIDsCSV = string.Join(",", userCartBooks);
                            db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(Json((object)new { results }));
        }
示例#8
0
        public async Task <ActionResult> Index()
        {
            using (var db = new BookPoolEntities())
            {
                ViewBag.Users = db.AspNetUsers.ToList();
            }


            //Books For Sale
            GoogleBook        googleResult      = new GoogleBook();
            List <GoogleBook> googleBooksResult = new List <GoogleBook>();

            List <BookPoolResult> dbResult = new List <BookPoolResult>();

            List <string> myGoogleBooksIDs = new List <string>();

            using (var db = new BookPoolEntities())
            {
                myGoogleBooksIDs = db.AvailableBooks.Select(x => x.GoogleID).ToList();
            }

            foreach (var googleID in myGoogleBooksIDs)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(BookPool.DataObjects.Global.Globals.googleBaseAPI_SearchByID);
                    StringBuilder httpRoute = new StringBuilder();
                    httpRoute.Append(googleID);

                    var response = await client.GetAsync(httpRoute.ToString());

                    if (response.IsSuccessStatusCode)
                    {
                        googleResult = await response.Content.ReadAsAsync <GoogleBook>();

                        googleBooksResult.Add(googleResult);
                    }
                }
            }

            using (var db = new BookPoolEntities())
            {
                ViewBag.AvailableBooks = (from googleBooks in googleBooksResult
                                          join availableBooks in db.AvailableBooks on googleBooks.id equals availableBooks.GoogleID
                                          join ownerUser in db.AspNetUsers on availableBooks.OwnerUserID equals ownerUser.Id

                                          select new BookPoolResult
                {
                    ID = availableBooks.ID,
                    Academic = availableBooks.Academic,
                    BookConditionID = availableBooks.BookConditionID,
                    BookLanguageID = availableBooks.BookLanguageID,
                    BookName = availableBooks.BookName,
                    CategoryID = availableBooks.CategoryID,
                    GoogleID = availableBooks.GoogleID,
                    OwnerUserID = availableBooks.OwnerUserID,
                    Price = availableBooks.Price,
                    Authors = googleBooks.volumeInfo.authors,
                    AverageRating = googleBooks.volumeInfo.averageRating,
                    Categories = googleBooks.volumeInfo.categories,
                    Description = googleBooks.volumeInfo.description,
                    ImageURL = googleBooks.volumeInfo.imageLinks?.thumbnail?.Replace("http:", "https:"),
                    PageCount = googleBooks.volumeInfo.pageCount,
                    PreviewLink = googleBooks.volumeInfo.previewLink,
                    PrintType = googleBooks.volumeInfo.printType,
                    PublishedDate = googleBooks.volumeInfo.publishedDate,
                    Publisher = googleBooks.volumeInfo.publisher,
                    Subtitle = googleBooks.volumeInfo.subtitle,
                    SellingStatus = availableBooks.SellingStatus,
                    PostedOn = availableBooks.PostedOn,
                    OwnerUserName = ownerUser.Email,
                    OwnerPhoneNumber = ownerUser.PhoneNumber
                }).DistinctBy(x => x.ID).ToList();
            }



            //Books For Search
            GoogleBook        googleSearchResult      = new GoogleBook();
            List <GoogleBook> googleSearchBooksResult = new List <GoogleBook>();

            List <BookPoolResult> dbSearchResult = new List <BookPoolResult>();

            List <string> myGoogleSearchBooksIDs = new List <string>();

            using (var db = new BookPoolEntities())
            {
                myGoogleSearchBooksIDs = db.SearchForBooks.Select(x => x.GoogleID).ToList();
            }

            foreach (var googleID in myGoogleSearchBooksIDs)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(BookPool.DataObjects.Global.Globals.googleBaseAPI_SearchByID);
                    StringBuilder httpRoute = new StringBuilder();
                    httpRoute.Append(googleID);

                    var response = await client.GetAsync(httpRoute.ToString());

                    if (response.IsSuccessStatusCode)
                    {
                        googleSearchResult = await response.Content.ReadAsAsync <GoogleBook>();

                        googleSearchBooksResult.Add(googleSearchResult);
                    }
                }
            }

            using (var db = new BookPoolEntities())
            {
                ViewBag.SearchBooks = (from googleBooks in googleSearchBooksResult
                                       join searchBooks in db.SearchForBooks on googleBooks.id equals searchBooks.GoogleID
                                       join userToSearch in db.AspNetUsers on searchBooks.AspNetUserID equals userToSearch.Id

                                       select new BookPoolResult
                {
                    ID = searchBooks.ID,
                    BookName = googleBooks.volumeInfo.title,
                    GoogleID = googleBooks.id,
                    Authors = googleBooks.volumeInfo.authors,
                    AverageRating = googleBooks.volumeInfo.averageRating,
                    Categories = googleBooks.volumeInfo.categories,
                    Description = googleBooks.volumeInfo.description,
                    ImageURL = googleBooks.volumeInfo.imageLinks?.thumbnail?.Replace("http:", "https:"),
                    PageCount = googleBooks.volumeInfo.pageCount,
                    PreviewLink = googleBooks.volumeInfo.previewLink,
                    PrintType = googleBooks.volumeInfo.printType,
                    PublishedDate = googleBooks.volumeInfo.publishedDate,
                    Publisher = googleBooks.volumeInfo.publisher,
                    Subtitle = googleBooks.volumeInfo.subtitle,
                    OwnerUserName = userToSearch.Email,
                    OwnerPhoneNumber = userToSearch.PhoneNumber
                }).DistinctBy(x => x.ID).ToList();
            }

            using (var db = new BookPoolEntities())
            {
                ViewBag.Orders = (from orders in db.OrderHeaders
                                  join details in db.OrderDetails on orders.ID equals details.OrderHeaderID
                                  join userSeller in db.AspNetUsers on details.SellerUserID equals userSeller.Id
                                  join userBuyer in db.AspNetUsers on orders.ClientUserID equals userBuyer.Id
                                  select new OrderResult
                {
                    BookName = details.BookName,
                    BookPrice = details.BookPrice,
                    BuyerAddress = "",
                    BuyerPhoneNumber = userBuyer.PhoneNumber,
                    BuyerUserName = userBuyer.UserName,
                    SellerUserName = userSeller.UserName,
                    SellerPhoneNumber = userSeller.PhoneNumber,
                    OrderID = orders.ID,
                    TotalPrice = orders.TotalPrice
                }).ToList();
            }



            return(View());
        }