示例#1
0
        public ActionResult <SingleRecordDto> CreateRecord([FromBody, Required] RecordForCreationDto record)
        {
            try
            {
                var recordEntity = _mapper.Map <Record>(record);

                if (!_recordsRepository.EmployeeExists(recordEntity.EmployeeId))
                {
                    return(StatusCode(StatusCodes.Status403Forbidden));
                }

                if (_recordsRepository.RecordExists(recordEntity.Id))
                {
                    return(StatusCode(StatusCodes.Status403Forbidden));
                }

                _recordsRepository.AddRecord(recordEntity);

                var recordToReturn = _mapper.Map <SingleRecordDto>(recordEntity);
                return(CreatedAtRoute("GetRecordv2",
                                      new
                {
                    recordId = recordToReturn.Id
                },
                                      recordToReturn));
            }
            catch
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <IActionResult> PostSensorDatas([FromBody] RecordForCreationDto record)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var dataForAzure = Mapper.Map <SensorDatas>(record);

            _context.SensorDatas.Add(dataForAzure);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                _logger.LogError(e.Message, e.InnerException);

                if (SensorDatasExists(dataForAzure.Id))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    // For the logger file
                    throw new Exception("Creating a record failed on save.");
                }
            }

            var modelToReturn = Mapper.Map <RecordDto>(dataForAzure);

            _logger.LogInformation(100, $"Data with {dataForAzure.Id} ID has been created at UTC time {DateTime.UtcNow}");

            // Status code 201 with the location (route) in the response header
            return(CreatedAtRoute("GetById", new { id = modelToReturn.LocationIdentifier }, CreateLinksForRecord(modelToReturn)));
        }