Beispiel #1
0
        public async Task <IActionResult> ShortenUrl(UrlViewModel model)
        {
            if (ModelState.IsValid)
            {
                Result <string> result;
                var             userId = GetUserId();
                if (string.IsNullOrEmpty(userId))
                {
                    result = await _urlShortService.CreateUrl(model.LongUrl);
                }
                else
                {
                    result = await _urlShortService.CreateUrl(model.LongUrl, userId);
                }
                if (!result.Success)
                {
                    model.Error = result.Error;
                }
                model.ShortUrl = $"{_settings.ShortUrlDomain}/{result.Data}";
                return(View("Index", model));
            }

            model.Error = string.Join(Environment.NewLine,
                                      ModelState.Values.SelectMany(m => m.Errors).Select(x => x.ErrorMessage));
            return(View("Index", model));
        }
Beispiel #2
0
 public ActionResult <string> CreateUrl(string url)
 {
     try
     {
         var resp = _service.CreateUrl(url);
         return(Ok(resp.ToString()));
     }
     catch (Exception e)
     {
         return(BadRequest(e));
     }
 }