コード例 #1
0
        public async Task <IActionResult> Create(WebUrlAddCommand webUrlAddCommand)
        {
            ViewBag.Message = null;
            if (ModelState.IsValid)
            {
                if (!webUrlAddCommand.Url.ToFix().UrlChecker())
                {
                    return(RedirectToAction("Create", new { message = URLShortener.Core.Resource.Resource_Fa.UrlIsNotValid }));
                }
                var url = _webUrlService.GetWebUrl(webUrlAddCommand.Url.ToFix());
                if (url != null)
                {
                    return(RedirectToAction("Create", new { message = URLShortener.Core.Resource.Resource_Fa.ThisFaNameIsAllready }));
                }
                ///حلقه تولید لینک کوتاه و بررسی موجود نبودن در دیتا بیس
                string urlshort = string.Empty;
                string isurl    = webUrlAddCommand.Url.ToFix();
                do
                {
                    urlshort = TextHelper.ToShortUrl(5, isurl);
                    isurl    = string.Empty;
                } while (_webUrlService.ExistsUrlShort(urlshort));

                Guid   userId = Guid.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                WebUrl webUrl = new WebUrl()
                {
                    Id         = Guid.Empty,
                    Url        = webUrlAddCommand.Url.ToFix(),
                    UrlShort   = urlshort,
                    CreateTime = DateTime.Now,
                    ClickCount = 0,
                    UserId     = userId
                };
                _webUrlService.Add(webUrl);
                return(RedirectToAction(nameof(Index)));
            }
            return(View(webUrlAddCommand));
        }