public async Task <DeviceDto> CreateAsync(DeviceForCreationDto deviceForCreation)
        {
            var device = _mapper.Map <Device>(deviceForCreation);

            device = await _userService.BindAssetWithUserAsync(CurrentUserId, device);

            _repositoryManager.Device.CreateDevice(device);
            await _repositoryManager.SaveAsync();

            return(_mapper.Map <DeviceDto>(device));
        }
        public IActionResult CreateDevice([FromBody] DeviceForCreationDto device)
        {
            try
            {
                // Check the input has been passed in
                if (device == null)
                {
                    return(BadRequest());
                }

                // Validate using the fluid validation rules
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                if (_deviceRepository.DeviceExists(device.DeviceId))
                {
                    return(StatusCode(409, "Device already registered with server"));
                }

                // Map the dto to a domain entity
                var finalDevice = _mapper.Map <Device>(device);

                // Create the school record in the repository
                _deviceRepository.AddDevice(finalDevice);


                if (!_deviceRepository.Save())
                {
                    return(StatusCode(500, "A problem happened while handling your request"));
                }

                var createdDeviceToReturn = _mapper.Map <DeviceDto>(finalDevice);

                return(CreatedAtRoute("GetDevice", new { id = createdDeviceToReturn.Id },
                                      createdDeviceToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while creating device.", ex);
                return(StatusCode(500, "A problem happened while handling your request"));
            }
        }
Esempio n. 3
0
        public IActionResult CreateDevice([FromBody] DeviceForCreationDto device)
        {
            // check if input data is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var newDevice = new Device
            {
                Name     = device.Name,
                Location = device.Location
            };

            _airQualityDataRepository.AddDevice(newDevice);

            _airQualityDataRepository.Save();

            return(Ok($"New Device has been Successfully created: {newDevice.Name} in {newDevice.Location}"));
        }
Esempio n. 4
0
        public async Task <IActionResult> PostDevice([FromBody] DeviceForCreationDto projectForCreation)
        {
            var projectDto = await _deviceService.CreateAsync(projectForCreation);

            return(CreatedAtAction("GetDevices", new { id = projectDto.Id }, projectDto));
        }