예제 #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "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();
            var connectionString = config.GetConnectionString("ConnectionString");

            //log.LogInformation("Connection String:" + connectionString);
            //Console.WriteLine(connectionString);
            LinkContext.connectionString = connectionString;

            string linkOriginal = req.QueryString.Value.Substring(6);

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);
            //linkOriginal = linkOriginal ?? data?.name;

            Link link = new Link();

            link.LinkOriginal = linkOriginal;

            string message = "";
            string responseMessage;

            if (link.ValidateOriginal(ref message))
            {
                LinkRepository linkRepository = new LinkRepository(new LinkContext());
                linkRepository.InsertLink(link);

                //Console.WriteLine(link.Status);

                responseMessage = JsonConvert.SerializeObject(new { Domain = linkOriginal, ShortenedLink = "https://www.linkmir.com/" + link.LinkShort, Status = link.Status });

                return(new OkObjectResult(responseMessage));
            }
            else
            {
                responseMessage = message;
                return(new BadRequestObjectResult(responseMessage));
            }
        }