/// <summary> /// Async method to record requester accessing link. /// </summary> /// <param name="longShortUrl">The long short url object</param> /// <param name="userAgent">User agent details of requester</param> /// <param name="ipAddress">IP address of requester</param> /// <returns></returns> private async Task addUserAccessingUrlAsync(LongShortUrl longShortUrl, string userAgent, string ipAddress) { longShortUrl.LongShortUrlUsers.Add(new LongShortUrlUser() { UserAgent = userAgent, IpAddress = ipAddress }); await _longShortUrlRepository.UpdateAsync(longShortUrl, true); }
/// <summary> /// Takes a long url and generate a new shortened url /// </summary> /// <param name="longUrl">The long url that will be shortened</param> /// <returns>A <see cref="LongShortUrl"/></returns> private LongShortUrl addLongShortUrl(string longUrl) { LongShortUrl longShortUrl = new LongShortUrl() { // Generate unquie short url id ShortUrlId = BaseGeneralHelper.GenerateRandomString(BaseConstants.MaxSizeShortUrlId), LongUrl = longUrl }; bool exitLoop = false; LongShortUrlRepository.InsertStatus insertStatus; // Retry logic for generating short url for (int i = 0; i < BaseConstants.RetryInsertAttempts; i++) { // Try to insert long url into database insertStatus = _longShortUrlRepository.Insert(longShortUrl, true); switch (insertStatus) { case LongShortUrlRepository.InsertStatus.Success: exitLoop = true; break; case LongShortUrlRepository.InsertStatus.DuplicateShortUrlId: // Duplicate short url id exists, create new id and try again longShortUrl.ShortUrlId = BaseGeneralHelper.GenerateRandomString(BaseConstants.MaxSizeShortUrlId); break; case LongShortUrlRepository.InsertStatus.DuplicateLongUrl: // Duplicate long url exists, get record for long url longShortUrl = _longShortUrlRepository.GetLongShortUrlByLongUrl(longUrl); exitLoop = true; break; } if (exitLoop) { break; } } return longShortUrl; }