// Syncs the database with the cast of a show, also returns the cast so it can be used right away
        public async Task <IEnumerable <CastCredit> > SyncShowCast(int showId)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, "http://api.tvmaze.com/shows/" + showId + "/cast");

            request.Headers.Add("Accept", "application/json");

            var response = await HttpClient.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                string json = await response.Content.ReadAsStringAsync();

                var castDtoList = JsonConvert.DeserializeObject <IEnumerable <CastCreditDto> >(json);

                var castList = new List <CastCredit>();

                foreach (var castCreditDto in castDtoList)
                {
                    if (castCreditDto.Person == null)
                    {
                        continue;
                    }                                               // skip if there is no person

                    var castCredit = new CastCredit
                    {
                        ShowId   = showId,
                        PersonId = castCreditDto.Person.Id,
                        Person   = new Person
                        {
                            Id       = castCreditDto.Person.Id,
                            Birthday = castCreditDto.Person.Birthday,
                            Name     = castCreditDto.Person.Name,
                        }
                    };

                    // adding this as a new instance to avoid tracking by entity framework
                    await CastCreditRepository.AddCastCredit(
                        new CastCredit
                    {
                        ShowId   = showId,
                        PersonId = castCreditDto.Person.Id,
                    });

                    var personExists = await PersonRepository.PersonExists(castCredit.PersonId);

                    if (!personExists)
                    {
                        await PersonRepository.AddPerson(castCredit.Person);
                    }
                    castList.Add(castCredit);
                }

                return(castList);
            }

            // something went wrong at this point, returning empty list
            return(new List <CastCredit>());
        }
Beispiel #2
0
        public async Task <bool> AddCastCredit(CastCredit castCredit)
        {
            await DbContext.CastCredits.AddAsync(castCredit);

            return(true);
        }