public IActionResult Redirect(Redirect redirect)
 {
     redirect.CreateDate = DateTime.UtcNow;
     if (string.IsNullOrWhiteSpace(redirect.EndPoint))
     {
         return(new StatusCodeResult(StatusCodes.Status400BadRequest));
     }
     if (string.IsNullOrWhiteSpace(redirect.EntryPoint))
     {
         redirect.CreateEntryPoint();
     }
     else if (_context.Redirects.Any(y => String.Equals(y.EntryPoint, redirect.EntryPoint, StringComparison.CurrentCultureIgnoreCase)))
     {
         return(new StatusCodeResult(StatusCodes.Status400BadRequest));
     }
     _context.Redirects.Add(redirect);
     _context.SaveChangesAsync();
     return(Json(redirect));
 }
Esempio n. 2
0
        public async Task <ActionResult <UrlDTO> > Create(UrlDTO urlDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var url = new Url
            {
                LongUrl = urlDto.Url
            };

            await _dbContext.Urls.AddAsync(url);

            await _dbContext.SaveChangesAsync();

            var urlService = new UrlService();
            var shortUrl   = urlService.Encode(url.IdHash);

            return(new UrlDTO()
            {
                Url = shortUrl
            });
        }
Esempio n. 3
0
 /// <summary>
 /// Insert UrlData
 /// </summary>
 /// <param name="urlData"></param>
 /// <returns></returns>
 public async Task Save(UrlData urlData)
 {
     _context.Add(urlData);
     await _context.SaveChangesAsync();
 }