Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
 // 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();
 }
Ejemplo n.º 3
0
        public static async Task <Models.ShortLink> GetHashIDItemAsync(string hashID)
        {
            if (String.IsNullOrEmpty(hashID) || String.IsNullOrWhiteSpace(hashID))
            {
                return(null);
            }
            Database  database        = cosmosClient.GetDatabase(databaseId);
            Container hashIDContainer = database.GetContainer(hashIDContainerID);
            int       seedValue       = HashIDGenerator.DecodeSeedValue(hashID);

            Models.ShortLink shortLinkResponse;
            try
            {
                shortLinkResponse =
                    await hashIDContainer.ReadItemAsync <Models.ShortLink>(seedValue.ToString(), new PartitionKey(seedValue.ToString()));
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                shortLinkResponse = null;
            }
            return(shortLinkResponse);
        }