public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "alarms")] HttpRequest req,
            ILogger log)
        {
            string reqBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    alarm   = JsonConvert.DeserializeObject <Alarm>(reqBody);

            _alarmService.CreateAlarm(alarm);
            return(new OkResult());
        }
Ejemplo n.º 2
0
        public OperationResult <Guid?> CreateParameter(ParameterEditDto dto)
        {
            //Validazione argomenti
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }

            //Dichiaro la lista di risultati di ritorno
            IList <ValidationResult> vResults = new List <ValidationResult>();

            //Definisco l'entità
            Parameter entity = new Parameter();

            entity.Name = dto.Name;
            //entity.Device = dto.Device != null && dto.Device.Id.HasValue ? _deviceRepository.Load(dto.Device.Id): null;
            entity.Process       = dto.Process.HasValue ? _processRepository.Load(dto.Process) : null;
            entity.Frequency     = dto.Frequency;
            entity.FrequencyType = dto.FrequencyType?.Id;
            entity.IsEnabled     = !dto.IsEnabled.HasValue ? false : dto.IsEnabled;
            //entity.PositionMisure = dto.PositionMisure;

            //Eseguo la validazione logica
            vResults = ValidateEntity(entity);

            if (!vResults.Any())
            {
                //Salvataggio su db
                _parameterRepository.Save(entity);
            }

            if (dto.Alarms != null && dto.Alarms.Count > 0)
            {
                entity.Alarms.Clear();
                foreach (var item in dto.Alarms)
                {
                    item.Parameter = entity.Id;
                    var oResult = _alarmService.CreateAlarm(item);
                    if (oResult.HasErrors())
                    {
                        return(new OperationResult <Guid?>
                        {
                            ValidationResults = oResult.ValidationResults
                        });
                    }
                    var alarm = _alarmService.Load <Alarm, Guid?>(oResult.ReturnedValue);
                    entity.Alarms.Add(alarm);
                }
            }

            //Eseguo la validazione logica
            vResults = ValidateEntity(entity);

            if (!vResults.Any())
            {
                //Salvataggio su db
                _parameterRepository.Save(entity);
            }

            //Ritorno i risultati
            return(new OperationResult <Guid?>
            {
                ReturnedValue = entity.Id,
                ValidationResults = vResults
            });
        }