public async Task <IActionResult> Create([Bind("MediaItemId,ExternalId,ApiEndpointId,Title,Description,ImageUrl,LargeImageUrl,VideoUrl,DateCreated,DateLastAccessed,KeyWords,MediaType")] MediaItem mediaItem) { if (ModelState.IsValid) { _context.Add(mediaItem); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(mediaItem)); }
public async Task <IActionResult> GetDailyImage(DateTime?date) { //search db var searchDate = (date.HasValue ? date.Value.ToString("yyyy-MM-dd") : DateTime.UtcNow.ToString("yyyy-MM-dd")); MediaItem mediaItemFromDb = _context.MediaItems.FirstOrDefault(x => x.DateCreated != null && x.DateCreated.ToString("yyyy-MM-dd") == searchDate); if (mediaItemFromDb != null) { return(new JsonResult(JsonConvert.SerializeObject(mediaItemFromDb))); } //search api DailyImageRequestHandler requestHandler = new DailyImageRequestHandler(_context, _clientFactory.CreateClient(), _config.GetValue <string>("ApiKey:NASA", ""), _config); DailyImage dailyImage = await requestHandler.OnGet(date); if (requestHandler.DailyImageRequestErrored) { throw new Exception("500: Internal Server Error - Unable to retrieve daily image."); } //update db var mediaItem = Mapper.MapToMediaItem(dailyImage); mediaItem.ApiEndpointId = requestHandler.Endpoint.ApiEndpointId; mediaItem.DateLastAccessed = DateTime.UtcNow; _context.MediaItems.Add(mediaItem); await _context.SaveChangesAsync(); return(new JsonResult(JsonConvert.SerializeObject(mediaItem))); }