Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="longUrl"></param>
        /// <returns></returns>
        public UrlData CreateShortUrl(string longUrl)
        {
            _logger.LogDebug("CreateShortUrl() called");

            if (Uri.IsWellFormedUriString(longUrl, UriKind.Absolute))
            {
                //if we already computed for this url, get it from the db
                var urlExist = GetByLongUrl(longUrl);
                if (urlExist != null && urlExist.Status == "ok")
                {
                    return(urlExist);
                }

                //create a new entry as we don't have this in the db
                var db       = DataDB.ShortUrls;
                var id       = db.Count > 0 ? db[db.Count - 1].Id + 1 : DataDB.StartId;
                var shortUrl = _config.UrlBase + ShortUrlHelper.Encode(id);

                var urlData = new UrlData
                {
                    LongUrl  = longUrl,
                    Id       = id,
                    ShortUrl = shortUrl,
                };
                db.Add(urlData);

                return(urlData);
            }

            //we have an invalid url string. Return an error UrlData
            return(getErrorUrlData(longUrl, "invalid url string format"));
        }
        public int Save(ShortUrl shortUrl)
        {
            //insert
            shortUrl.CreateDate = DateTime.UtcNow;
            _context.ShortUrls.Add(shortUrl);
            _context.SaveChanges();

            //update path
            shortUrl.Path = ShortUrlHelper.Encode(shortUrl.Id);
            _context.ShortUrls.Update(shortUrl);
            _context.SaveChanges();

            return(shortUrl.Id);
        }
        public IActionResult Create(string originalUrl)
        {
            var shortUrl = new ShortUrl
            {
                OriginalUrl = originalUrl,
                Key         = ShortUrlHelper.Encode(maxLength)
            };

            TryValidateModel(shortUrl);
            if (ModelState.IsValid)
            {
                _service.Save(shortUrl);

                return(RedirectToAction(actionName: nameof(Show), routeValues: new { id = shortUrl.Id }));
            }

            return(View(shortUrl));
        }
Пример #4
0
        public IActionResult Show(int?id)
        {
            if (!id.HasValue)
            {
                return(NotFound());
            }

            var shortUrl = _service.GetById(id.Value);

            if (shortUrl == null)
            {
                return(NotFound());
            }

            ViewData["Path"] = ShortUrlHelper.Encode(shortUrl.Id);

            return(View(shortUrl));
        }
Пример #5
0
        public IActionResult Show(int?id)
        {
            if (!id.HasValue)
            {
                return(NotFound());
            }

            var shortUrl = _service.GetByIdAsync(id.Value);

            if (shortUrl == null)
            {
                return(NotFound());
            }

            var result = shortUrl.Result;

            ViewData["Path"] = ShortUrlHelper.Encode(result.UrlId);

            return(View(result));
        }
Пример #6
0
        // [ValidateAntiForgeryToken]
        public IActionResult Create([FromBody] string originalUrl)
        {
            if (UrlValidation.IsValidUrl(originalUrl))
            {
                var shortUrl = new ShortenedUrl
                {
                    OriginalUrl = originalUrl
                };

                TryValidateModel(shortUrl);
                if (ModelState.IsValid)
                {
                    _service.Save(shortUrl);
                }

                var returnedUrl = ShortUrlHelper.Encode(shortUrl.Id);
                return(Ok("http://shrturl.keith-pearce.co.uk/url/" + returnedUrl));
            }
            else
            {
                return(BadRequest("Please enter a valid URL"));
            }
        }