public async Task <IActionResult> CreateDevice([FromRoute] string SerialNumber, [FromBody] Device device) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!await _context.GatewayExists(SerialNumber)) { _logger.LogError($"Gateway with serial number {SerialNumber} wasn't found!"); return(NotFound($"Gateway with serial number {SerialNumber} wasn't found!")); } try { await _context.CreateDevice(SerialNumber, device); } catch (Exception ex) { _logger.LogError(ex.Message, ex); return(StatusCode(500, "A problem happened while handling you request.")); } return(CreatedAtAction("GetDevice", new { DeviceId = device.DeviceId }, device)); }