public async Task <ActionResult> Refresh() { _cacheManager.RemoveCache("NewsItems"); await _fileUploader.SaveFileAsync(@"News/NewsItems.json", string.Empty); return(RedirectToAction("Index", "Home")); }
private async Task <bool> UpdateVisits(NewsModel model) { model.NumberOfVisits++; var jsonString = JsonSerializer.Serialize(model); var isSaved = await _fileUploader.SaveFileAsync($@"{newsItems}/{model.Id + ".json"}", jsonString) .ConfigureAwait(false); return(true); }
public async void SetNewsItem(NewsModel newsModel) { var newsFromCache = _cache.Get <List <NewsModel> >("NewsItems"); if (newsFromCache.Any(m => m.Id == newsModel.Id)) { newsFromCache.Remove(newsFromCache.First(n => n.Id == newsModel.Id)); } newsFromCache.Add(newsModel); newsFromCache = newsFromCache.OrderByDescending(n => n.DisplayDate).ToList(); await _fileUploader.SaveFileAsync("News/NewsItems.json", JsonConvert.SerializeObject(newsFromCache)); _cache.Set <List <NewsModel> >("NewsItems", newsFromCache); }
private async Task <List <NewsModel> > GetNewsFromS3() { var newsFromCache = _cache.Get <List <NewsModel> >("NewsItems"); if (newsFromCache != null && newsFromCache.Any()) { return(newsFromCache); } var newsItemsFromFile = await _fileUploader.GetFileFromS3(@"News/NewsItems.json"); // _fileUploader.ReadFile("NewsItems.json"); if (!string.IsNullOrWhiteSpace(newsItemsFromFile)) { // await _fileUploader.UpdateS3ReaderCount(true); var newsModel = JsonConvert.DeserializeObject <List <NewsModel> >(newsItemsFromFile); var newsItemsFile = newsModel.OrderByDescending(n => n.DisplayDate).ToList(); _cache.Set("NewsItems", newsItemsFile); return(newsItemsFile); } var newsCollection = new List <NewsModel>(); try { var req = new ListObjectsV2Request { BucketName = BucketName, Prefix = "News/NewsItems" }; var listing = await _s3Client.ListObjectsV2Async(req); // await _fileUploader.UpdateS3ReaderCount(false); foreach (var s3 in listing.S3Objects) { var responseBody = await _fileUploader.GetFileFromS3(s3.Key); if (string.IsNullOrEmpty(responseBody)) { continue; } var newsModel = JsonConvert.DeserializeObject <NewsModel>(responseBody); newsModel.MainImg = newsModel.Images.FirstOrDefault(); newsModel.DisplayDate = newsModel.CreatedDate.AddDays(newsModel.TopNewsForDays); if (newsModel.DisplayDate >= DateTime.Today) { newsModel.CreatedDate = DateTime.Today; } else { newsModel.DisplayDate = newsModel.CreatedDate; } newsCollection.Add(newsModel); } } catch (Exception ex) { //ignore } var newsItems = newsCollection.OrderByDescending(n => n.DisplayDate).ToList(); _cache.Set("NewsItems", newsItems); await _fileUploader.SaveFileAsync("News/NewsItems.json", JsonConvert.SerializeObject(newsItems)); return(_cache.Get <List <NewsModel> >("NewsItems")); }