Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody]  MeasurementCreateDTO measurementCreateDTO)
        {/*
          *
          * var location = GetControllerActionNames();
          *
          * try
          * {
          *     this.logger.LogInfo($"{location}: Measurement creation  Attempted");
          *     if (measurementListCreateDTO == null)
          *     {
          *         this.logger.LogWarn($"{location}: Empty request was submitted");
          *         return BadRequest(ModelState);
          *     }aaaaaaaaaaaaaaaaaaaaaaaaawdw
          *     if (!ModelState.IsValid)
          *     {
          *         this.logger.LogWarn($"{location}: Measurement Data was Incomplete");
          *         return BadRequest(ModelState);
          *
          *     }
          *     //measurementListCreateDTO.DateCreated = DateTime.Now.ToString();
          *
          *     var isGood = false;
          *
          *     for (int i = 0; i < measurementListCreateDTO.Count; i++)
          *     {
          *         MeasurementCreateDTO measurementCreateDTO = new MeasurementCreateDTO();
          *
          *         var measurement = this.mapper.Map<Measurement>(measurementCreateDTO);
          *         isGood = await measurementRepository.Create(measurement);
          *
          *         if (!isGood)
          *         {
          *
          *             break;
          *         }
          *         this.logger.LogInfo($"{location}: Measurement creation was created");
          *         this.logger.LogInfo($"{location}: {measurement}");
          *
          *     }
          *     //  var measurement = this.mapper.Map<Measurement>(measurementListCreateDTO);
          *     if (!isGood)
          *     {
          *
          *         return InternalError($"{location}: Measurement creation failed");
          *     }
          *     return Created("Create", new { measurementListCreateDTO });
          *
          * }
          * catch (Exception e)
          * {
          *     return InternalError($"{location}: {e.Message} - {e.InnerException}");
          *
          * }*/
            var location = GetControllerActionNames();

            try
            {
                this.logger.LogInfo($"{location}: Measurement creation  Attempted");
                if (measurementCreateDTO == null)
                {
                    this.logger.LogWarn($"{location}: Empty request was submitted");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    this.logger.LogWarn($"{location}: Measurement Data was Incomplete");
                    return(BadRequest(ModelState));
                }
                measurementCreateDTO.DateCreated = DateTime.Now;//.ToString();
                var measurement = this.mapper.Map <Measurement>(measurementCreateDTO);
                var isGood      = await measurementRepository.Create(measurement);

                if (!isGood)
                {
                    return(InternalError($"{location}: Measurement creation failed"));
                }

                this.logger.LogInfo($"{location}: Measurement creation was created");
                this.logger.LogInfo($"{location}: {measurement}");
                return(Created("Create", new { measurement }));
            }
            catch (Exception e)
            {
                return(InternalError($"{location}: {e.Message} - {e.InnerException}"));
            }
        }