public DeviceDetailDto Add(CreateDeviceInput input) { var entity = Mapper.Map <CreateDeviceInput, Device>(input); // Validation: Each gateway can have up to 10 devices. var devicesQty = _deviceRepo.FindAll().Count(w => w.GatewayId == input.GatewayId); if (devicesQty >= 10) { return(new DeviceDetailDto { StatusCode = HttpStatusCode.BadRequest, StatusMessage = "No more than 10 peripheral devices are allowed for a gateway.", }); } using (_uowFactory.Create()) { _deviceRepo.Add(entity); } var dto = Mapper.Map <Device, DeviceDetailDto>(entity); dto.StatusCode = HttpStatusCode.Created; dto.StatusMessage = "Device created sucessfully."; return(dto); }
public IHttpActionResult PostDevice([FromBody] CreateDeviceInput input) { DeviceDetailDto response = null; try { response = _service.Add(input); } catch (Exception e) { Helpers.ExceptionHelper.ThrowException(e); } return(Ok(response)); }