Beispiel #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)

        {
            try
            {
                var config = new ConfigurationBuilder()
                             .SetBasePath(context.FunctionAppDirectory)
                             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                             .AddEnvironmentVariables()
                             .Build();

                var cstr     = config.GetConnectionString("SqlConnectionString");
                var setting1 = config["Setting1"];

                log.LogInformation(cstr);
                log.LogInformation(setting1);

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

                //  log.Info(cstr);
                // log.Info(setting1);

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

                string requestJson = await new StreamReader(req.Body).ReadToEndAsync();
                global::ShortnerHelper.ShortnerRequestJson shortnerRequestData = JsonConvert.DeserializeObject <global::ShortnerHelper.ShortnerRequestJson>(requestJson);

                //Generate the short code

                operationResult opr    = ShortnerHelper.GenerateShortCode(shortnerRequestData);
                string          output = JsonConvert.SerializeObject(opr);

                return((ActionResult) new OkObjectResult($" {output}"));
            }
            catch (Exception ex)
            {
                return((ActionResult) new OkObjectResult($" asdasda"));
            }
        }
        //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);
            }
        }