async Task <Film> IGhibliRepository.AddFilmAsync(Film film)
        {
            film.ID = Guid.NewGuid().ToString();
            await _db.AddAsync(film);

            await _db.SaveChangesAsync();

            return(film);
        }
Esempio n. 2
0
        public async Task <IActionResult> GetAll()
        {
            var responseHttp = await _client.GetAsync(BaseUrl);

            var cacheKey = $"Get_On_Film-{BaseUrl}";

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

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

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

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

                var film = new Film();

                foreach (var x in ff)
                {
                    if (!_context.Films.Any(f => f.ID == x.ID))
                    {
                        await _context.AddAsync(x);

                        await _context.SaveChangesAsync();
                    }
                }
                _memoryCache.Set(cacheKey, ff);
                return(Ok(ff));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }