Ejemplo n.º 1
0
 public RedirectTargetWithAddress(string address, RedirectTarget target)
 {
     Address      = address;
     Target       = target.Target;
     Permanent    = target.Permanent;
     QueryProcess = target.QueryProcess;
 }
Ejemplo n.º 2
0
        public async Task <ActionResult <ShortUrl> > ShortenUrlAsync([FromForm] string targetUrl, [FromForm] string name = null, [FromForm] bool generateName = false)
        {
            if (!Uri.TryCreate(targetUrl, UriKind.Absolute, out _))
            {
                return(UnprocessableEntity("Invalid url!"));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = GeneratorService.GenerateNextName();
            }
            else
            {
                bool nameUsed = await DbContext.ShortUrls.AnyAsync(x => x.Name == name);

                if (nameUsed)
                {
                    if (!generateName)
                    {
                        return(Conflict("this name is already being used"));
                    }

                    name = GeneratorService.GenerateNextName();
                }
            }

            var target = await DbContext.RedirectTargets.Where(x => x.TargetUrl == targetUrl).FirstOrDefaultAsync();

            if (target == null)
            {
                target = new RedirectTarget(targetUrl);
                DbContext.RedirectTargets.Add(target);
            }

            var userId   = Guid.Parse(User.FindFirstValue("UserId"));
            var shortUrl = new ShortUrl(userId, target.Id, name);

            DbContext.ShortUrls.Add(shortUrl);
            await DbContext.SaveChangesAsync();

            WebhookService.QueueShortUrl(shortUrl);

            return(Ok(shortUrl));
        }
 protected AttributionController(
     IRFProcessingContext context,
     RFEngineDefinition engineConfig,
     Func <IRFProcessingContext, string, A> activityFunc,
     RedirectTarget applyRedirect,
     RedirectTarget errorRedirect = null) : base(context, engineConfig)
 {
     _activityFunc  = activityFunc;
     _applyRedirect = applyRedirect;
     _errorRedirect = errorRedirect;
     if (_errorRedirect == null)
     {
         _errorRedirect = new RedirectTarget
         {
             Action     = "Index",
             Area       = "",
             Controller = "Home"
         };
     }
 }