public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } bool isUri = Uri.IsWellFormedUriString(LongUrl, UriKind.Absolute); if (!isUri) { return(Page()); } var shortUrl = new ShortUrl { LongUrl = LongUrl, Expiration = DateTime.UtcNow.AddMinutes(2) }; HttpStatusCode code = HttpStatusCode.BadRequest; (code, shortUrl) = await _urlShortenerService.UpsertShortUrlAsync("1", shortUrl); if (code == HttpStatusCode.OK) { this.ShortUrl = $"{Request.Scheme}://{Request.Host}/s/{shortUrl.Id}"; } else { this.ShortUrl = $"Error:{code}"; } return(Page()); }
public async Task <IActionResult> CreateRecord([FromBody] ShortUrlRequest shortUrlRequest) { try { Guard.ArgumentNotNullOrEmpty(nameof(shortUrlRequest.LongUrl), shortUrlRequest.LongUrl); Guard.ArgumentNotNullOrEmpty(nameof(shortUrlRequest.ExpiredKey), shortUrlRequest.ExpiredKey); Guard.ArgumentNotNull(nameof(shortUrlRequest.ttl), shortUrlRequest.ttl); var tenant = FetchTenantFromUser(); bool isUri = Uri.IsWellFormedUriString(shortUrlRequest.LongUrl, UriKind.Absolute); if (!isUri) { throw new Exception($"Failed IsWellFormedUriString:{shortUrlRequest.LongUrl}"); } var shortUrl = new ShortUrl { LongUrl = shortUrlRequest.LongUrl, Tenant = tenant, Expiration = DateTime.UtcNow.AddSeconds((double)shortUrlRequest.ttl) }; HttpStatusCode code = HttpStatusCode.BadRequest; (code, shortUrl) = await _urlShortenerService.UpsertShortUrlAsync(shortUrlRequest.ExpiredKey, shortUrl); var jsonResult = new JsonResult(shortUrl) { StatusCode = (int)code }; return(jsonResult); } catch (Exception e) { _logger.LogError(e.Message); } return(new BadRequestObjectResult("Invalid Request")); }