Ejemplo n.º 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            log.LogInformation("C# HTTP trigger function processed a request.");

            string shortCode = req.Query["shortCode"];

            AzureStorageAccount             = ShortnerHelper.AzureStorageAccount = config["AzureStorageAccount"];
            AzureStorageConnectionSecretKey = ShortnerHelper.AzureStorageConnectionSecretKey = config["AzureStorageConnectionSecretKey"];
            UrlShortnerTableName            = ShortnerHelper.UrlShortnerTableName = config["UrlShortnerTableName"];
            UrlReterivelTableName           = ShortnerHelper.UrlRetreivelTableName = config["UrlRetreivelTableName"];

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

            var obhfullUrl = shortcoderetreival.GetEntityByPartitionKeyAndRowKey("abc", shortCode);

            //string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            return(obhfullUrl != null
                ? (ActionResult) new OkObjectResult($"Url :, {obhfullUrl.Url}")
                : new BadRequestObjectResult("Please pass a valid short code in  the query string."));
        }
        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);
            }
        }
        //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);
            }
        }