public async Task <IActionResult> CreateHashIDPostedJson([FromBody] Models.UrlString longUrl) { if (ModelState.IsValid && URLValidator.IsUrlValid(longUrl.url)) { Models.ShortLink hashIDItem = await HashIDDBRepository.CreateHashIDItemIfNotExistsAsync(longUrl.url); Models.PublicShortLink response = new Models.PublicShortLink { Url = hashIDItem.Url, HashID = hashIDItem.HashID, CreatedTime = hashIDItem.CreatedTime }; // Determine if hashID exists in cache. // Move hashID to front if hashID exists in cache. if (ReadUrlCache.Get(response.HashID) == null) { // Add new hashID to cache. ReadUrlCache.Add(response.HashID, response.Url); } return(Ok(response)); } else { Models.ErrorResponse invalidURLResponse = new Models.ErrorResponse { Details = "Enter a valid URL." }; return(BadRequest(invalidURLResponse)); } }
public async Task <IActionResult> Get(string hashID) { string redirectUrl = ReadUrlCache.Get(hashID); if (redirectUrl != null) { return(Redirect(redirectUrl)); } Models.ShortLink hashIDItem = await HashIDDBRepository.GetHashIDItemAsync(hashID); if (hashIDItem == null) { Models.ErrorResponse notFoundResponse = new Models.ErrorResponse { Details = "Not found." }; return(NotFound(notFoundResponse)); } else { return(Redirect(hashIDItem.Url)); } }