public static async Task <Models.ShortLink> CreateHashIDItemIfNotExistsAsync(string url) { if (String.IsNullOrEmpty(url) || String.IsNullOrWhiteSpace(url)) { throw new ArgumentException("Input URL cannot be null, empty or whitespaces"); } Database database = cosmosClient.GetDatabase(databaseId); Container hashIDContainer = database.GetContainer(hashIDContainerID); int currentSeedValue = await SeedValueDBRepository.GetCurrentSeedValueAsync(); string hashID = HashIDGenerator.EncodeSeedValue(currentSeedValue); Models.ShortLink shortLinkResponse = hashIDContainer.GetItemLinqQueryable <Models.ShortLink>(true) .Where(shortLink => shortLink.Url == url) .AsEnumerable() .FirstOrDefault(); if (shortLinkResponse == null) { Models.ShortLink newShortlink = new Models.ShortLink { Id = currentSeedValue.ToString(), Url = url, HashID = hashID, CreatedTime = DateTime.UtcNow, }; shortLinkResponse = await hashIDContainer.CreateItemAsync(newShortlink, new PartitionKey(newShortlink.Id)); currentSeedValue = await SeedValueDBRepository.GetCurrentSeedValueAsync(); SeedValueDBRepository.UpdateSeedValueItemAsync(currentSeedValue + 1); } return(shortLinkResponse); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); SeedValueDBRepository.Initialize(); HashIDGenerator.Initialize(); HashIDDBRepository.Initialize(); }