예제 #1
0
        private static Book MapBook(WorkResource resource)
        {
            var book = new Book
            {
                ForeignBookId = resource.GoodreadsId.ToString(),
                Title         = resource.Title,
                TitleSlug     = resource.TitleSlug,
                CleanTitle    = Parser.Parser.CleanAuthorName(resource.Title),
                ReleaseDate   = resource.ReleaseDate,
            };

            book.Links.Add(new Links {
                Url = resource.Url, Name = "Goodreads Editions"
            });

            if (resource.Books != null)
            {
                book.Editions = resource.Books.Select(x => MapEdition(x)).ToList();

                // monitor the most rated release
                var mostPopular = book.Editions.Value.OrderByDescending(x => x.Ratings.Votes).FirstOrDefault();
                if (mostPopular != null)
                {
                    mostPopular.Monitored = true;

                    // fix work title if missing
                    if (book.Title.IsNullOrWhiteSpace())
                    {
                        book.Title = mostPopular.Title;
                    }
                }
            }
            else
            {
                book.Editions = new List <Edition>();
            }

            Debug.Assert(!book.Editions.Value.Any() || book.Editions.Value.Count(x => x.Monitored) == 1, "one edition monitored");

            book.AnyEditionOk = true;

            var ratingCount = book.Editions.Value.Sum(x => x.Ratings.Votes);

            if (ratingCount > 0)
            {
                book.Ratings = new Ratings
                {
                    Votes = ratingCount,
                    Value = book.Editions.Value.Sum(x => x.Ratings.Votes * x.Ratings.Value) / ratingCount
                };
            }
            else
            {
                book.Ratings = new Ratings {
                    Votes = 0, Value = 0
                };
            }

            return(book);
        }
예제 #2
0
 private int GetAuthorId(WorkResource b)
 {
     return(b.Books.First().Contributors.FirstOrDefault()?.GoodreadsId ?? 0);
 }