public async Task <DomainTrackingUrl> AddAsync(DomainTrackingUrl trackingUrl) { // Get subscription CompanySubscription subscription = await _subscriptionService.GetAsync(trackingUrl.SubscriptionId); // Check redirect url if (trackingUrl.RedirectUrl == null) { throw new ArgumentException("RedirectUrl required"); } if (!string.Equals(trackingUrl.RedirectUrl.Host, subscription.SiteHostname, StringComparison.InvariantCultureIgnoreCase)) { throw new BadRequestException( string.Format("Client subscription {0} allows only URLs starting with {1}: {2}", subscription.Id, subscription.SiteHostname, trackingUrl.RedirectUrl)); } // Add tracking url TrackingUrlEntity entity = _mappingEngine.Map <DomainTrackingUrl, TrackingUrlEntity>(trackingUrl); entity = await _trackingUrlRepository.AddAsync(entity); DomainTrackingUrl result = _mappingEngine.Map <TrackingUrlEntity, DomainTrackingUrl>(entity); result.Key = _urlShortenerService.Encode(Int64.Parse(result.Id)); return(result); }
public async Task DeleteAsync(TrackingUrlEntity entity) { try { await _collection.DeleteAsync(entity.Id); } catch (Exception e) { throw new ApplicationException(string.Format("Mongo driver failure: {0}", e)); } }
public async Task <TrackingUrlEntity> EditAsync(TrackingUrlEntity entity) { TrackingUrlEntity result; try { result = await _collection.UpdateAsync(entity); } catch (Exception e) { throw new ApplicationException(string.Format("Mongo driver failure: {0}", e)); } return(result); }
public async Task <TrackingUrlEntity> AddAsync(TrackingUrlEntity entity) { if (string.IsNullOrEmpty(entity.Id)) { // Generating new integer id entity.Id = _idGenerator.GenerateId().ToString(CultureInfo.InvariantCulture); } TrackingUrlEntity result; try { result = await _collection.AddAsync(entity); } catch (Exception e) { throw new ApplicationException(string.Format("Mongo driver failure: {0}", e)); } return(result); }
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); }
public Task <TrackingUrlEntity> GetAsync(TrackingUrlEntity entity) { return(_collection.GetAsync(entity.Id)); }