예제 #1
0
 public JsonResult GetBooks(FilterCriteria criteria)
 {
     var bookList = new List<BookModel>();	// No need to actually properly cast this as it's being returned as a json result.
     if (criteria == null)
     {
         return Json(GetBooksFromGoogle(criteria), JsonRequestBehavior.AllowGet);
     }
     return Json(bookList, JsonRequestBehavior.AllowGet);
 }
예제 #2
0
        /// <summary>
        /// based off criteria will generate a filter string to return appropriate books.  If it's been run previously it will 
        /// apply the FilterString generated last time.  
        /// </summary>
        private string GetGoogleJson(FilterCriteria criteria, long startIndex = 0)
        {
            const string googleUriBaseString =
                @"https://www.googleapis.com/books/v1/volumes?q={2}&key={0}&fields=totalItems," +
                @"items(id,volumeInfo(title,averageRating,authors,publishedDate,categories))&startIndex={1}&maxResults=40&orderBy=newest";
            const string NULL_INPUT_CASE = "inpublisher:Chronicle";

            if(criteria == null || criteria.filters.Count == 0)
                return MakeGetRequest(String.Format(googleUriBaseString, API_KEY, startIndex, NULL_INPUT_CASE));
            if(!String.IsNullOrWhiteSpace(FilterString))
                return MakeGetRequest(String.Format(googleUriBaseString, API_KEY, startIndex, FilterString));

            string title, categories, authors;
            title = categories = authors = "";
            float minAvgRating = 0;

            foreach (var filter in criteria.filters)
            {
                if(filter.FilterTypes == FilterTypesEnum.Genre)
                {
                    if(String.IsNullOrWhiteSpace(categories))
                        categories = filter.FilterValue;
                    else
                        categories += "," + filter.FilterValue;
                }
                else if(filter.FilterTypes == FilterTypesEnum.Author)
                {
                    if(String.IsNullOrWhiteSpace(authors))
                        authors = filter.FilterValue;
                    else
                        authors = "," + filter.FilterValue;
                }
                else if (filter.FilterTypes == FilterTypesEnum.MinAvgRating)
                {
                    CustomFilterFlag = true;
                    CustomMinFilterValue = float.Parse(filter.FilterValue);
                }
            }
            string queryString = "";
            if(title != null)
                queryString = String.Format(SF_TITLE_QUERY, title);
            if (authors != null)
                queryString = String.Format(SF_AUTHOR_QUERY, authors);
            if (categories != null)
                queryString = String.Format(SF_CATEGORY_QUERY, categories);

            queryString = queryString.TrimStart(',');
            FilterString = queryString;

            return MakeGetRequest(String.Format(googleUriBaseString, API_KEY, startIndex, FilterString));
        }
예제 #3
0
 public JsonResult GetBookData(FilterCriteria criteria)
 {
     //var book = new BookModel();
     return Json(GetBooksFromGoogle(criteria), JsonRequestBehavior.AllowGet);
 }
예제 #4
0
        //private List<BookModel> GetBooksFromGoogle(FilterCriteria criteria)
        private List<BookModel> GetBooksFromGoogle(FilterCriteria criteria)
        {
            var books = new List<BookModel>();
            GoogleJsonModel jsonResult;
            long count = 0;
            do
            {
                string googJson = GetGoogleJson(criteria, count);
                jsonResult = JsonConvert.DeserializeObject<GoogleJsonModel>(GetGoogleJson(criteria, count));
                foreach (var result in jsonResult.items)
                {
                    if (result.Title == null) continue;		//Yes, This happened.

                    var newBook = new BookModel
                    {
                        Id = result.id,
                        Title = result.Title,
                    };
                    float averageRating = 0;
                    GenreTypesEnum genreType = GenreTypesEnum.All;
                    string author = "Unknown";

                    if (result.Authors != null) author = result.Authors.Length > 0 ? String.Join(",", result.Authors) : result.Authors[0];
                    if (result.averageRating != null)	float.TryParse(result.averageRating, out averageRating);

                    if (result.Genres != null && result.Genres.Length > 0)
                    {
                        //newBook.GenreType = getFirstGenre(result.Genres);
                        newBook.Genre = String.Join(",", result.Genres);
                    }
                    newBook.Author = author;
                    newBook.AvgRating = averageRating;

                    books.Add(newBook);
                }
                count += jsonResult.items.Length;

            //Differentiation between books.Count and count is on purpose.
            } while (jsonResult != null && count < jsonResult.totalItems && books.Count < MAX_BOOK_COUNT);

            return books;
        }