public async Task <ActionResult> CreateNewDevice([FromBody] AuthData authData) { try { if (authData == null || !authData.IsModelValid()) { return(BadRequest($"Error in device create: {nameof(AuthData)} body is malformed: {authData}")); } // Find the user created when interacting with Alexa AlexaUser alexaUser = await context.LoadAsync <AlexaUser>(authData.OneTimePasscode); if (alexaUser == null || alexaUser.TimeToLive <= DateTimeOffset.UtcNow.ToUnixTimeSeconds()) { return(NotFound("Could not locate user.")); } Device newDevice = new Device { AlexaUserId = alexaUser.AlexaUserId, DeviceId = Guid.NewGuid().ToString(), FirebaseToken = authData.FirebaseToken, DeviceName = authData.DeviceName, ModifiedDate = DateTime.UtcNow, CreatedDate = DateTime.UtcNow }; // Save the new device, and delete the old AlexaUser entry Task newDeviceSaveResult = context.SaveAsync(newDevice); Task deleteAlexaAuthResult = context.DeleteAsync <AlexaUser>(alexaUser.AlexaUserId); await Task.WhenAll(newDeviceSaveResult, deleteAlexaAuthResult); return(CreatedAtAction(nameof(CreateNewDevice), newDevice)); } catch (Exception ex) { return(BadRequest($"{authData}\n{ex}")); } }