Ejemplo n.º 1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "clients")] HttpRequestMessage req,
            [Table("clients", Connection = "AzureWebJobsStorage")] CloudTable clientsTable,
            TraceWriter log)
        {
            log.Info("Clients function processed a request.");

            log.Info("token " + req.Headers.Authorization?.Parameter);

            var tokenHelper = new TokenHelper();

            if (!tokenHelper.AuthorizeUser(req.Headers.Authorization))
            {
                log.Info("user not authorzied");
                return(req.CreateResponse(HttpStatusCode.Forbidden));
            }

            var clients = new List <Client>();

            var detailQuery = new TableQuery <ClientDetailEntity>()
                              .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "detail"));

            var imageQuery = new TableQuery <ClientTagImageEntity>()
                             .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "tagimage"));

            var detailEntities = clientsTable.ExecuteQuery(detailQuery);

            foreach (var detail in detailEntities.OrderBy(de => de.Year).ThenBy(de => de.Month).ThenBy(de => de.Day))
            {
                clients.Add(new Client(
                                Guid.Parse(detail.PartitionKey),
                                detail.Name,
                                detail.Month,
                                detail.Year,
                                detail.Url));
            }

            var response = req.CreateResponse(HttpStatusCode.OK, clients, new JsonMediaTypeFormatter
            {
                SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                },
                UseDataContractJsonSerializer = false
            });

            return(response);
        }
Ejemplo n.º 2
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "value")] HttpRequestMessage req,
            [Table("valuecreated", Connection = "AzureWebJobsStorage")] CloudTable valueCreatedTable,
            TraceWriter log)
        {
            log.Info("Value function processed a request.");

            var tokenHelper = new TokenHelper();

            if (!tokenHelper.AuthorizeUser(req.Headers.Authorization))
            {
                log.Info("user not authorzied");
                return(req.CreateResponse(HttpStatusCode.Forbidden));
            }

            var query = new TableQuery <ValueCreatedEntity>();

            var valueCreated = valueCreatedTable.ExecuteQuery(query).Sum(v => v.Value);

            return(req.CreateResponse(HttpStatusCode.OK, valueCreated, "application/json"));
        }
Ejemplo n.º 3
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "projects/{id?}")] HttpRequestMessage req,
            [Table("projects", Connection = "AzureWebJobsStorage")] CloudTable projectsTable,
            [Table("peopleimpacted", Connection = "AzureWebJobsStorage")] CloudTable peopleImpactedTable,
            [Table("valuecreated", Connection = "AzureWebJobsStorage")] CloudTable valueCreatedTable,
            string id,
            TraceWriter log)
        {
            log.Info("Projects function processed a request.");

            var tokenHelper = new TokenHelper();

            if (!tokenHelper.AuthorizeUser(req.Headers.Authorization))
            {
                log.Info("user not authorzied");
                return(req.CreateResponse(HttpStatusCode.Forbidden));
            }

            var projects = new List <Project>();

            var detailQuery = new TableQuery <ProjectDetailEntity>()
                              .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "detail"));

            if (string.IsNullOrEmpty(id))
            {
                var detailEntities = projectsTable.ExecuteQuery(detailQuery);

                foreach (var detail in detailEntities.OrderByDescending(de => de.CompletionDate))
                {
                    projects.Add(GetProject(detail, peopleImpactedTable, valueCreatedTable));
                }

                return(req.CreateResponse(HttpStatusCode.OK, projects, new JsonMediaTypeFormatter
                {
                    SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
                    {
                        ContractResolver = new CamelCasePropertyNamesContractResolver()
                    },
                    UseDataContractJsonSerializer = false
                }));
            }

            detailQuery = detailQuery.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, id));

            var projectDetail = projectsTable.ExecuteQuery(detailQuery).FirstOrDefault();

            if (projectDetail == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound));
            }

            var project = GetProject(projectDetail, peopleImpactedTable, valueCreatedTable);

            return(req.CreateResponse(HttpStatusCode.OK, project, new JsonMediaTypeFormatter
            {
                SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                },
                UseDataContractJsonSerializer = false
            }));
        }