Esempio n. 1
0
        public async Task <IActionResult> Compress(string link)
        {
            if (LinkExtension.IsValidLink(link))
            {
                var entity = await _linkCollection.Find(_ => _.Link == link).FirstOrDefaultAsync();

                string entityId = entity?.Id;
                if (entity == null)
                {
                    entityId = Generator.GenerateUniqLink();
                    bool exists = true;
                    while (exists)
                    {
                        exists = await _linkCollection.Find(_ => _.Id == entityId).AnyAsync();

                        if (exists)
                        {
                            entityId = Generator.GenerateUniqLink();
                        }
                    }

                    var newEntity = new CompressedLinkEntity
                    {
                        Link        = link,
                        Id          = entityId,
                        OpenedCount = 0
                    };
                    await _linkCollection.InsertOneAsync(newEntity);
                }

                string compressedUrl = $"{this.Request.Scheme}://{this.Request.Host}/{entityId}";

                if (this.HttpContext.Request.Cookies.ContainsKey(Cookies.BrowserUserId))
                {
                    var browserUserId = Guid.Parse(this.HttpContext.Request.Cookies[Cookies.BrowserUserId]);

                    var noAuthUser = await _usersCollection.Find(_ => _.Id == browserUserId).FirstOrDefaultAsync();

                    noAuthUser.Links.Add(compressedUrl);
                    await _usersCollection.ReplaceOneAsync((p => p.Id == browserUserId), noAuthUser, new ReplaceOptions { IsUpsert = true });
                }
                else
                {
                    Guid browserUserId = Guid.NewGuid();
                    var  newUser       = new NoAuthUserLinkEntity
                    {
                        Id    = browserUserId,
                        Links = new List <string>()
                    };
                    newUser.Links.Add(compressedUrl);
                    await _usersCollection.InsertOneAsync(newUser);

                    this.HttpContext.Response.Cookies.Append(Cookies.BrowserUserId, browserUserId.ToString());
                }

                return(this.Ok(compressedUrl));
            }

            return(this.BadRequest("Link is not valid, please use Http or Https scheme"));
        }
Esempio n. 2
0
        public RedirectToRouteResult RedirectToAction(
            Expression <Action> action)
        {
            var routeValuesFromExpression =
                LinkExtension.GetRouteValuesFromExpression(action);

            return(RedirectToRoute(routeValuesFromExpression));
        }
Esempio n. 3
0
        public RedirectToRouteResult RedirectToAction <TController>(
            Expression <Action <TController> > action, bool isPermanent = false) where TController : Controller
        {
            var routeValuesFromExpression =
                LinkExtension.GetRouteValuesFromExpression(action);

            if (isPermanent)
            {
                return(RedirectToRoutePermanent(routeValuesFromExpression));
            }
            return(RedirectToRoute(routeValuesFromExpression));
        }