Esempio n. 1
0
 public static AnswerAM FromEntity(AnswerEntity entity)
 {
     return(new AnswerAM
     {
         Name = entity.Name,
         SelectedDates = entity.SelectedDatesString?.Split(',').Select(d => DateTime.Parse(d)).ToList(),
         AdditionalMessage = entity.AdditionalMessage
     });
 }
Esempio n. 2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            try
            {
                var partition = req.Query["partition"];

                var service = new EntriesBL();
                var config  = await service.GetEntryConfig(partition);

                if (config == null)
                {
                    return(new BadRequestResult());
                }

                string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                var    model       = JsonConvert.DeserializeObject <AnswerAM>(requestBody);

                if (!model.IsValid)
                {
                    return(new BadRequestObjectResult("Model is invalid! Name or dates missing."));
                }

                var entity = new AnswerEntity(partition, model.Name)
                {
                    Name = model.Name,
                    AdditionalMessage   = model.AdditionalMessage,
                    SelectedDatesString = string.Join(',', model.SelectedDates.Select(sd => sd.ToString("yyyy-MM-dd")))
                };

                var table = await StorageService.GetStorageTableReference();

                await table.ExecuteAsync(TableOperation.Insert(entity));

                if (!string.IsNullOrEmpty(config.WebHookUrl))
                {
                    using (var client = new HttpClient()) {
                        await client.PostAsync(config.WebHookUrl, null);
                    }
                }

                return(new OkObjectResult(entity));
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message, ex);
                return(new BadRequestObjectResult(ex.Message));
            }
        }