コード例 #1
0
        public static string GenerateShortCode(string fullUrl)
        {
            string md5hashOfUrl = CalculateMD5Hash(fullUrl);

            Azure.Storage.TableStorage <ShortCodeGenrator> shortcodegen = new Azure.Storage.TableStorage <ShortCodeGenrator>(UrlShortnerTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

            ShortCodeGenrator existingShortCodeForUrl = shortcodegen.GetEntityByPartitionKeyAndRowKey("abc", md5hashOfUrl);

            if (existingShortCodeForUrl != null)
            {
                return(existingShortCodeForUrl.ShortCode);
            }
            else
            {
                var newShortCodeForurl = GenerateRandomShortCode();

                ShortCodeGenrator shortCodeGenratorobj = new ShortCodeGenrator()
                {
                    PartitionKey = "abc",
                    RowKey       = md5hashOfUrl,
                    ShortCode    = newShortCodeForurl
                };

                shortcodegen.CreateEntity(shortCodeGenratorobj);

                Azure.Storage.TableStorage <ShortCodeRetreival> shortcoderetreival = new Azure.Storage.TableStorage <ShortCodeRetreival>(UrlRetreivelTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

                ShortCodeRetreival insertCode = new ShortCodeRetreival()
                {
                    PartitionKey = "abc",
                    RowKey       = newShortCodeForurl,
                    Url          = fullUrl,
                    shortCode    = newShortCodeForurl,
                    Country      = "India",
                    IpAddress    = "10.112.25.63",
                    AccessCount  = 0
                };

                shortcoderetreival.CreateEntity(insertCode);

                return(newShortCodeForurl);
            }
        }
コード例 #2
0
        //public static string GenerateShortCode(ShortnerRequestJson ShortnerRequestJson)
        //        {
        //            string md5hashOfUrl = CalculateMD5Hash(ShortnerRequestJson.Url);

        //            Azure.Storage.TableStorage<ShortCodeGenrator> shortcodegen = new Azure.Storage.TableStorage<ShortCodeGenrator>(UrlShortnerTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

        //            ShortCodeGenrator existingShortCodeForUrl = shortcodegen.GetEntityByPartitionKeyAndRowKey("abc", md5hashOfUrl);

        //            if (existingShortCodeForUrl != null)
        //            {
        //                return existingShortCodeForUrl.ShortCode;
        //            }
        //            else
        //            {
        //                 var newShortCodeForurl = string.Empty;

        //                if(string.IsNullOrEmpty(ShortnerRequestJson.RequestedShortCode))

        //                {
        //                    newShortCodeForurl = GenerateRandomShortCode();
        //                }
        //                else
        //                {
        //                    newShortCodeForurl = ShortnerRequestJson.RequestedShortCode;
        //                }

        //                ShortCodeGenrator shortCodeGenratorobj = new ShortCodeGenrator()
        //                {
        //                    PartitionKey = "abc",
        //                    RowKey = md5hashOfUrl,
        //                    ShortCode = newShortCodeForurl
        //                };

        //                shortcodegen.CreateEntity(shortCodeGenratorobj);

        //                Azure.Storage.TableStorage<ShortCodeRetreival> shortcoderetreival = new Azure.Storage.TableStorage<ShortCodeRetreival>(UrlRetreivelTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

        //                ShortCodeRetreival insertCode = new ShortCodeRetreival()
        //                {
        //                    PartitionKey = "abc",
        //                    RowKey = newShortCodeForurl,
        //                    Url = ShortnerRequestJson.Url,
        //                    shortCode = newShortCodeForurl,
        //                    Country = ShortnerRequestJson.Country,
        //                    IpAddress = ShortnerRequestJson.IpAddress,
        //                    AccessCount = 0
        //                };

        //                shortcoderetreival.CreateEntity(insertCode);

        //                return newShortCodeForurl;
        //            }
        //        }

        public static operationResult GenerateShortCode(ShortnerRequestJson ShortnerRequestJson)
        {
            operationResult opr          = new operationResult();
            string          md5hashOfUrl = CalculateMD5Hash(ShortnerRequestJson.Url);

            Azure.Storage.TableStorage <ShortCodeGenrator> shortcodegen = new Azure.Storage.TableStorage <ShortCodeGenrator>(UrlShortnerTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

            ShortCodeGenrator existingShortCodeForUrl = shortcodegen.GetEntityByPartitionKeyAndRowKey("abc", md5hashOfUrl);

            if (existingShortCodeForUrl != null)
            {
                //  url is already registered  with some other source code

                opr.Url             = ShortnerRequestJson.Url;
                opr.ShortCode       = existingShortCodeForUrl.ShortCode;
                opr.OperationStatus = OperationStatus.UrlExistsWithOtherSourceCode;
                opr.Message         = $"Url {ShortnerRequestJson.Url} is already registered in system with shortcode : {existingShortCodeForUrl.ShortCode}, Please use this short code.";
                return(opr);
                // return existingShortCodeForUrl.ShortCode;
            }
            else
            {
                var newShortCodeForurl = string.Empty;

                if (string.IsNullOrEmpty(ShortnerRequestJson.RequestedShortCode))

                {
                    newShortCodeForurl = GenerateRandomShortCode();
                }
                else
                {
                    newShortCodeForurl = ShortnerRequestJson.RequestedShortCode;

                    // check if short code is already registerred and not unique
                    Azure.Storage.TableStorage <ShortCodeRetreival> checkuniqueShortCode = new Azure.Storage.TableStorage <ShortCodeRetreival>(UrlRetreivelTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

                    ShortCodeRetreival shortcodRetrival = checkuniqueShortCode.GetEntityByPartitionKeyAndRowKey("abc", ShortnerRequestJson.RequestedShortCode);

                    if (shortcodRetrival != null)
                    {
                        //  url is already registered  with some other source code

                        opr.Url             = ShortnerRequestJson.Url;
                        opr.ShortCode       = shortcodRetrival.shortCode;
                        opr.OperationStatus = OperationStatus.ShortCodeNotUnique;
                        opr.Message         = $"Short Code {ShortnerRequestJson.RequestedShortCode} and its already registered with url {shortcodRetrival.Url}, Please Try with new short code.";

                        return(opr);
                        // return existingShortCodeForUrl.ShortCode;
                    }
                }

                ShortCodeGenrator shortCodeGenratorobj = new ShortCodeGenrator()
                {
                    PartitionKey = "abc",
                    RowKey       = md5hashOfUrl,
                    ShortCode    = newShortCodeForurl
                };

                shortcodegen.CreateEntity(shortCodeGenratorobj);

                Azure.Storage.TableStorage <ShortCodeRetreival> shortcoderetreival = new Azure.Storage.TableStorage <ShortCodeRetreival>(UrlRetreivelTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

                ShortCodeRetreival insertCode = new ShortCodeRetreival()
                {
                    PartitionKey = "abc",
                    RowKey       = newShortCodeForurl,
                    Url          = ShortnerRequestJson.Url,
                    shortCode    = newShortCodeForurl,
                    Country      = ShortnerRequestJson.Country,
                    IpAddress    = ShortnerRequestJson.IpAddress,
                    AccessCount  = 0
                };

                shortcoderetreival.CreateEntity(insertCode);

                opr.Url             = ShortnerRequestJson.Url;
                opr.ShortCode       = newShortCodeForurl;
                opr.OperationStatus = OperationStatus.UrlShortned;
                opr.Message         = $"Short Code {newShortCodeForurl} is registered now for url {ShortnerRequestJson.Url}, Please use this short code.";

                return(opr);
            }
        }