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."));
        }
        async public Task Init(string initialParameter)
        {
            try
            {
                _tableStorage = new Azure.Storage.TableStorage();
                _tableStorage.Init(initialParameter);
                await _tableStorage.CreateTableAsync(tableName);

                await _tableStorage.CreateTableAsync(tableNameAggregated);
            }
            catch /*(Exception ex)*/
            {
                //Console.WriteLine("Can't init AzureTableResourceLogger:");
                //Console.WriteLine(ex.Message);
                //Console.WriteLine(ex.StackTrace);
                _tableStorage = null;
            }
        }
        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);
            }
        }
Beispiel #5
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", 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();

            AzureStorageAccount             = config["AzureStorageAccount"];
            AzureStorageConnectionSecretKey = config["AzureStorageConnectionSecretKey"];
            emailQueue           = config["EmailQueue"];
            sendgridApiKey       = config["SendgridApiKey"];
            contactUsDbTableName = config["ContactUsDbTable"];

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



            // string name = req.Query["name"];

            string           requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            RegistrationJson regjson     = JsonConvert.DeserializeObject <RegistrationJson>(requestBody);

            // name = name ?? data?.name;
            try
            {
                Azure.Storage.TableStorage <RegistrationTable> logTableStorage = new Azure.Storage.TableStorage <RegistrationTable>(contactUsDbTableName, AzureStorageAccount, AzureStorageConnectionSecretKey);

                // Create Azure Queue Storage connection
                //  Azure.Storage.QueueStorage sliceQueuesStorage = new Azure.Storage.QueueStorage(logStorageAccountName, logStorageAccountSecret);

                Guid obj = Guid.NewGuid();

                RegistrationTable regtable = new RegistrationTable
                {
                    FirstName            = regjson.FirstName,
                    LastName             = regjson.LastName,
                    Country              = regjson.Country,
                    Message              = regjson.Message,
                    EmailNewsLetterOptIn = regjson.EmailNewsLetterOptIn,

                    EmailAddress        = regjson.ContactDetails.EmailAddress,
                    EmailContactOptIn   = regjson.ContactDetails.EmailContactOptIn,
                    MobileNo            = regjson.ContactDetails.MobileNo,
                    MobileContactOptIn  = regjson.ContactDetails.MobileContactOptIn,
                    SlackChannelAddress = regjson.ContactDetails.SlackChannelAddress,
                    SlackChannelOptIn   = regjson.ContactDetails.SlackChannelOptIn,
                    PartitionKey        = "abccd",
                    RowKey = obj.ToString(),
                };

                logTableStorage.CreateEntity(regtable);

                // create a datatable class here and insert data in azure data table

                Azure.Storage.QueueStorage registrationQueue = new Azure.Storage.QueueStorage(AzureStorageAccount, AzureStorageConnectionSecretKey, emailQueue);

                queuemessage registrationdetails = new queuemessage();
                registrationdetails.EmailAddress = regjson.ContactDetails.EmailAddress;
                registrationdetails.FirstName    = regjson.FirstName;
                registrationdetails.LastName     = regjson.LastName;

                string regmessage = JsonConvert.SerializeObject(registrationdetails);

                registrationQueue.EnQueue(regmessage, emailQueue);
                return(new OkObjectResult($"Thanks, {regjson.FirstName} {regjson.LastName} for registering with us, {System.Environment.NewLine} We have sent you an email for confirmation. please confirm your subscription. "));
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message.ToString());

                return(new OkObjectResult($"Thanks, {regjson.FirstName} {regjson.LastName} for trying registering with us, Registrations system is facing some issue, Please after some time. "));
            }
        }