Ejemplo n.º 1
0
        public async Task <ActionResult <GiphyItem> > GetGiphyItem(string searchTerm)
        {
            GiphyItem giphyItem = new GiphyItem();

            try
            {
                //giphyItem = await _context.GiphyItems.FindAsync(searchTerm);
                if (!_cache.TryGetValue(searchTerm, out giphyItem))
                {
                    GifResult gifResult = await _giphyTools.GifFetch(searchTerm);

                    if (gifResult != null)
                    {
                        giphyItem = new GiphyItem()
                        {
                            SearchTerm = searchTerm,
                            Url        = gifResult.Data.First().Url
                        };

                        var cacheEntryOptions = new MemoryCacheEntryOptions();

                        _cache.Set(searchTerm, giphyItem, cacheEntryOptions);

                        _context.GiphyItems.Add(giphyItem);
                    }
                }
            }
            catch (Exception ex)
            {
                string exc = ex.Message;
            }

            return(giphyItem);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <GiphyItem> > PostGiphyItem(GiphyItem item)
        {
            _context.GiphyItems.Add(item);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetGiphyItem), new { id = item.SearchTerm }, item));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutGiphyItem(string searchTerm, GiphyItem item)
        {
            if (searchTerm != item.SearchTerm)
            {
                return(BadRequest());
            }

            _context.Entry(item).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <GiphyItem> > GetGiphyItem(string searchTerm)
        {
            _logger.LogInformation($"GetGiphyItem search for {searchTerm} at {DateTime.UtcNow.ToLongTimeString()}");

            GiphyItem giphyItem = new GiphyItem();

            try
            {
                if (!_cache.TryGetValue(searchTerm, out giphyItem))
                {
                    GifResult gifResult = await _giphyTools.GifFetch(searchTerm);

                    if (gifResult != null)
                    {
                        giphyItem = new GiphyItem()
                        {
                            SearchTerm = searchTerm,
                            Url        = gifResult.Data.First().Images.Original.Url
                        };

                        var cacheEntryOptions = new MemoryCacheEntryOptions();

                        _cache.Set(searchTerm, giphyItem, cacheEntryOptions);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"GetGiphyItem Exception: {ex.Message}");
                throw;
            }

            _logger.LogInformation($"GetGiphyItems find gif at url={giphyItem.Url}");

            return(giphyItem);
        }