示例#1
0
        /// <summary>
        /// Get all the series an author has written.
        /// </summary>
        /// <param name="authorId">The author to fetch the list of series for.</param>
        /// <returns>A list of series written by the author.</returns>
        public async Task<IReadOnlyList<Series>> GetListByAuthorId(int authorId)
        {
            var parameters = new List<Parameter>
            {
                new Parameter { Name = "id", Value = authorId, Type = ParameterType.QueryString }
            };

            try
            {
                var response = await Connection.ExecuteRaw("series/list", parameters);
                if (response != null && (int)response.StatusCode >= 200 && (int)response.StatusCode < 300)
                {
                    var content = response.Content;
                    if (!string.IsNullOrWhiteSpace(content))
                    {
                        var document = XDocument.Parse(content);
                        var series = document.XPathSelectElements("GoodreadsResponse/series_works/series_work/series");
                        if (series != null && series.Count() > 0)
                        {
                            var seriesModels = new List<Series>();
                            foreach (var seriesElement in series)
                            {
                                var seriesModel = new Series();
                                seriesModel.Parse(seriesElement);
                                seriesModels.Add(seriesModel);
                            }

                            // Goodreads returns way too many duplicates, group by them by id first.
                            var grouped = seriesModels.GroupBy(x => x.Id);
                            var uniqueSeries = grouped.Select(x => x.First()).ToList();

                            return uniqueSeries;
                        }
                    }
                }
            }
            catch
            {
                // Just ignore the error and return null...
            }

            return null;
        }
示例#2
0
        /// <summary>
        /// Get all the series that the given work is in.
        /// </summary>
        /// <param name="workId">The work id to fetch the list of series for.</param>
        /// <returns>A list of series that this work appears in.</returns>
        public async Task<IReadOnlyList<Series>> GetListByWorkId(int workId)
        {
            var parameters = new List<Parameter>
            {
                new Parameter { Name = "id", Value = workId, Type = ParameterType.UrlSegment }
            };

            try
            {
                var response = await Connection.ExecuteRaw("series/work/{id}", parameters);
                if (response != null && (int)response.StatusCode >= 200 && (int)response.StatusCode < 300)
                {
                    var content = response.Content;
                    if (!string.IsNullOrWhiteSpace(content))
                    {
                        var document = XDocument.Parse(content);
                        var series = document.XPathSelectElements("GoodreadsResponse/series_works/series_work/series");
                        if (series != null && series.Count() > 0)
                        {
                            var seriesModels = new List<Series>();
                            foreach (var seriesElement in series)
                            {
                                var seriesModel = new Series();
                                seriesModel.Parse(seriesElement);
                                seriesModels.Add(seriesModel);
                            }

                            return seriesModels;
                        }
                    }
                }
            }
            catch
            {
                // Just ignore the error and return null...
            }

            return null;
        }