public DeviceUserBinding GetUserForDevice(string deviceId, ILogger log) { Uri collectionUri = UriFactory.CreateDocumentCollectionUri(dbName, collectionName); var options = new FeedOptions { EnableCrossPartitionQuery = false, MaxItemCount = 1 }; DeviceUserBinding userVehiclePairing = client.CreateDocumentQuery <DeviceUserBinding>(collectionUri, options) .Where(x => x.DeviceId == deviceId) .Take(1) .AsEnumerable() .FirstOrDefault(); return(userVehiclePairing); }
public static async Task <IActionResult> AddDeviceIdUserPairing( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "user/devices/")] HttpRequest req, [CosmosDB(databaseName: "devicedata", collectionName: "deviceusers", ConnectionStringSetting = "DeviceUserDBConnectionString")] IAsyncCollector <DeviceUserBinding> binding, ILogger log, ClaimsPrincipal claimsPrincipal) { var aadUserId = Shared.ValidateAuth(claimsPrincipal); if (!string.IsNullOrWhiteSpace(aadUserId)) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); DeviceUserBinding payload = JsonConvert.DeserializeObject <DeviceUserBinding>(requestBody); payload.AADUserId = aadUserId; await binding.AddAsync(payload); log.LogInformation("DeviceId/User paring added."); return(await Task.FromResult(new CreatedResult("user/device", $"Pairing created"))); } return(await Task.FromResult(new UnauthorizedResult())); }