Esempio n. 1
0
        public void Run(string[] args)
        {
            if (args is null)
            {
                throw new ArgumentNullException();
            }

            else if (args.Length != 1)
            {
                throw new ArgumentException("The number of arguments must be only one");
            }

            if (!int.TryParse(args[0], out int urlId))
            {
                Console.WriteLine("Attempted conversion of '{0}' failed.",
                                  args[0] ?? "<null>");
            }
            var encodedUrl = _urlShortenerService.Encode(urlId);

            Console.WriteLine($"Encoded URL: {encodedUrl}");
            var decodedUrl = _urlShortenerService.Decode(encodedUrl);

            Console.WriteLine($"Decoded URL : {decodedUrl}");
        }
Esempio n. 2
0
        public async Task <UrlTrackingResult> TrackAsync(DomainTrackingUrl trackingUrl)
        {
            if (string.IsNullOrEmpty(trackingUrl.Id))
            {
                trackingUrl.Id = _urlShortenerService.Decode(trackingUrl.Key).ToString(CultureInfo.InvariantCulture);
            }

            TrackingUrlEntity entity = _mappingEngine.Map <DomainTrackingUrl, TrackingUrlEntity>(trackingUrl);

            entity = await _trackingUrlRepository.GetAsync(entity);

            if (entity == null)
            {
                throw new NotFoundException(string.Format("Could not find tracking url with id {0} and key {1}",
                                                          trackingUrl.Id, trackingUrl.Key));
            }

            // Build portal url link
            string projectUrl = _projectUriProvider.GetUri(entity.ProjectId);

            // We should keep requested schema in redirect link
            var projectUri = new UriBuilder(projectUrl)
            {
                Scheme = trackingUrl.RequestUri.Scheme,
                Port   = trackingUrl.RequestUri.Port
            };

            // Default redirect to Portal
            var result = new UrlTrackingResult
            {
                ProjectId      = entity.ProjectId,
                SubscriptionId = entity.SubscriptionId,
                IsAccountable  = false,
                Redirect       = projectUri.Uri
            };

            DomainCompany       company;
            CompanySubscription subscription;

            try
            {
                company = await _companyService.FindBySubscriptionAsync(entity.SubscriptionId);

                subscription = await _subscriptionService.GetAsync(entity.SubscriptionId);
            }
            catch (ForbiddenException)
            {
                // blocked company or subscription - portal redirect
                return(result);
            }
            catch (NotFoundException)
            {
                // deleted or unexisting company or subscription - portal redirect
                return(result);
            }


            // Checking custom subscription type
            if (subscription.Type == SubscriptionType.Pro || subscription.Type == SubscriptionType.Custom)
            {
                // always redirect to client site
                result.Redirect      = new Uri(entity.RedirectUrl);
                result.IsAccountable = true;
            }
            else if (subscription.Type == SubscriptionType.Basic)
            {
                // checking company balance
                if (subscription.IsManuallyEnabled || subscription.HasTrialClicks || (await _balanceService.GetBalanceAsync(company.Id)) > 0)
                {
                    result.Redirect      = new Uri(entity.RedirectUrl);
                    result.IsAccountable = true;
                }
            }

            return(result);
        }