Ejemplo n.º 1
0
        public async Task <ActionResult> ShortenURL(string originalUrl)
        {
            if (string.IsNullOrEmpty(originalUrl))
            {
                return(Json(new { status = false, message = "Please provide URL." }));
            }
            else
            {
                if (!UrlRepository.HasHTTPProtocol(originalUrl))
                {
                    originalUrl = "http://" + originalUrl;
                }

                // check whether the URL entered exists
                if (!await UrlRepository.IsExistOriginalUrl(originalUrl))
                {
                    return(Json(new { status = false, message = "Could not reach the URL. Please ensure you have enter a valid URL." }));
                }

                // Check if original URL already exists in the database
                Url    existingURL = repository.Urls.Where(u => u.OriginalUrl.Equals(originalUrl, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                string urlPrefix   = Request.Scheme + "://" + Request.Host + "/";

                if (existingURL == null)
                {
                    // Check if original URL is existing shorten URL
                    Url processedURL = repository.Urls
                                       .Where(u => (urlPrefix + u.ShortUrl).Equals(originalUrl, StringComparison.OrdinalIgnoreCase))
                                       .FirstOrDefault();

                    if (processedURL == null)
                    {
                        Url newUrl = new Url()
                        {
                            OriginalUrl   = originalUrl,
                            GeneratedDate = DateTime.UtcNow
                        };

                        string randomUrl = "";

                        do // make sure the random short URL does not exist in db
                        {
                            randomUrl = UrlRepository.GenerateRandomShortUrl(appSettings.UrlLength);
                        } while (repository.Urls.Where(u => u.ShortUrl.Equals(randomUrl, StringComparison.OrdinalIgnoreCase)).Count() > 0);

                        newUrl.ShortUrl = randomUrl;

                        try
                        {
                            repository.SaveUrl(newUrl);
                            newUrl.ShortUrl = urlPrefix + newUrl.ShortUrl;

                            return(Json(new { status = true, url = newUrl }));
                        }
                        catch (Exception exc)
                        {
                            return(Json(new { status = false, message = exc.Message }));
                        }
                    }
                    else
                    {
                        processedURL.ShortUrl = originalUrl;
                        return(Json(new { status = true, url = processedURL }));
                    }
                }
                else
                {
                    existingURL.ShortUrl = urlPrefix + existingURL.ShortUrl;
                    return(Json(new { status = true, url = existingURL }));
                }
            }
        }