Esempio n. 1
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "Get", "Post")]
            HttpRequest req,
            ILogger log,
            ExecutionContext executionContext,
            CancellationToken token)
        {
            var runner = new FunctionRunner(ServiceResolver.Instance, executionContext, log);

            try
            {
                using var reader = new StreamReader(req.Body);
                var body = await reader.ReadToEndAsync().ConfigureAwait(false);

                var request = JsonConvert.DeserializeObject <UsersGetRequest>(body);

                var documents = runner.GetUsers(request);

                var json = JsonConvert.SerializeObject(documents, Formatting.Indented);
                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(
                        json,
                        Encoding.UTF8,
                        "application/json")
                });
            }
            catch (ContactServiceException e)
            {
                return(new HttpResponseMessage((HttpStatusCode)e.StatusCode)
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(e),
                        Encoding.UTF8,
                        "application/json")
                });
            }
            catch (Exception e)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(e),
                        Encoding.UTF8,
                        "application/json")
                });
            }
        }
Esempio n. 2
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "Post")]
            HttpRequest req,
            ILogger log,
            ExecutionContext executionContext,
            CancellationToken token)
        {
            var runner = new FunctionRunner(ServiceResolver.Instance, executionContext, log);

            try
            {
                await runner.DeleteContactsAsync(token).ConfigureAwait(false);

                return(new HttpResponseMessage(HttpStatusCode.OK));
            }
            catch (ContactServiceException e)
            {
                return(new HttpResponseMessage((HttpStatusCode)e.StatusCode)
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(e),
                        Encoding.UTF8,
                        "application/json")
                });
            }
            catch (Exception e)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(
                        JsonConvert.SerializeObject(e),
                        Encoding.UTF8,
                        "application/json")
                });
            }
        }