Ejemplo n.º 1
0
        public async Task <ShortUrlModel> GetData(string urlId)
        {
            ShortUrlModel ret = null;

            lock (_cache)
                _cache.TryGetValue(urlId, out ret);
            return(ret);
        }
Ejemplo n.º 2
0
        public async Task <bool> LogAccess(string urlId, IPAddress connectionRemoteIpAddress)
        {
            ShortUrlModel ret = null;

            lock (_cache)
                _cache.TryGetValue(urlId, out ret);
            //if (!ret.FirstVisitUtc.HasValue)
            //    ret.FirstVisitUtc = DateTime.Now;

            return(true);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Key,Url")] ShortUrlModel shortUrlModel)
        {
            if (ModelState.IsValid)
            {
                await _httpClient.AddAsync(shortUrlModel);

                return(RedirectToAction(nameof(Index)));
            }

            return(View(shortUrlModel));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> CreateUrl(ShortUrlModel shortUrlModel)
        {
            try
            {
                await _repository.AddUrl(shortUrlModel.Key, shortUrlModel.Url);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }

            return(Created(shortUrlModel.Key, shortUrlModel));
        }
        private async Task <string> CreateShortUrl(ShortUrlCreateRequest shortUrlCreateRequest, string key)
        {
            var shortUrlModel = new ShortUrlModel
            {
                Key        = key,
                CreatedUtc = DateTime.UtcNow,
                CreatorId  = shortUrlCreateRequest.UserId,
                ExpiresUtc = DateTime.UtcNow.AddDays(ApplicationVariable.ShortUrlExpireDays),
                UpdateDate = DateTime.UtcNow,
                Url        = shortUrlCreateRequest.LongUrl
            };
            await _shortUrlRepository.Insert(shortUrlModel);

            return(shortUrlModel.Key);
        }
Ejemplo n.º 6
0
 public async Task <(bool Success, string Key)> AddData(ShortUrlModel data)
 {
     for (int i = 0; i < 100; i++)
     {
         lock (_cache)
         {
             var key = Utils.KeyGenerator.Generate(_managedConfig.KeyLength);
             if (!_cache.ContainsKey(key))
             {
                 data.Key = key;
                 _cache.Add(key, data);
                 return(Success : true, Key : key);
             }
         }
     }
     return(Success : false, Key : null);
 }
Ejemplo n.º 7
0
        public async Task <(bool Success, string Key)> AddData(ShortUrlModel data)
        {
            // Try to find an available random key
            for (int i = 0; i < 100; i++)
            {
                var key      = Utils.KeyGenerator.Generate(_managedConfig.KeyLength);
                var existing = await GetData(key);

                if (existing == null)
                {
                    data.Key = key;
                    // Save to database
                    _shortUrlDbContext.Add(data);
                    await _shortUrlDbContext.SaveChangesAsync();

                    // No further processing
                    return(Success : true, Key : key);
                }
            }
            return(Success : false, Key : null);
        }
 public async Task <string> GetOriginalUrl(ShortUrlModel shortUrl)
 {
     return(await UrlShorteningService.GetOriginalUrl(shortUrl.ShortUrl));
 }