Пример #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"));
        }
Пример #2
0
        public async Task <ShortUrl> GetUrlByPathAsync(string path)
        {
            await EnsureSetupAsync();

            var documentCollectionUri = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId);

            var result = _client.CreateDocumentQuery <ShortUrl>(documentCollectionUri)
                         .Where(su => su.UrlId == ShortUrlHelper.Decode(path)).AsEnumerable().FirstOrDefault();

            return(result);
        }
        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));
        }
Пример #5
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));
        }
Пример #6
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));
        }
Пример #7
0
        public void OnBeginRequest(Object source, EventArgs e)
        {
            var context = (HttpApplication)source;

            if (context.Request.Url.AbsolutePath.Contains("/url/"))
            {
                var path = context.Request.Url.AbsolutePath.Remove(0, 1);
                var id   = ShortUrlHelper.Decode(path);
                using (var DBcontext = new UrlShortenerContext())
                {
                    var query = from item in DBcontext.ShortUrls
                                where item.Id == id
                                select item.OriginalUrl;

                    var redirectPath = query.FirstOrDefault();
                    if (redirectPath != null)
                    {
                        context.Response.Redirect(redirectPath);
                    }
                }
            }
        }
Пример #8
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"));
            }
        }
Пример #9
0
 public ShortenedUrl GetByPath(string path)
 {
     return(_context.ShortUrls.Find(ShortUrlHelper.Decode((path))));
 }