async Task <Film> IGhibliRepository.CreateAsync(Film film)
        {
            film.Id = Guid.NewGuid().ToString();

            await _db.Film.AddAsync(film);

            await _db.SaveChangesAsync();

            return(film);
        }
Beispiel #2
0
        public async Task <IActionResult> GetAll()
        {
            string baseUrl = "https://ghibliapi.herokuapp.com/films";

            var cacheKey = $"Get_Film";

            if (_memoryCache.TryGetValue(cacheKey, out string cachedValue))
            {
                return(Ok(cachedValue));
            }


            var res = await _client.GetAsync(baseUrl);

            if (!res.IsSuccessStatusCode)
            {
                throw new Exception("cannot read data!");
            }

            var content = await res.Content.ReadAsStringAsync();

            var f = JsonConvert.DeserializeObject <ICollection <Film> >(content);


            foreach (var x in f)
            {
                if (!db.Film.Any(f => f.Id == x.Id))
                {
                    await db.AddAsync(x);

                    await db.SaveChangesAsync();
                }
            }


            return(Ok(f));
        }