private static async Task GenerateDocuments(int investorId, int numberOfDocs) { var startDate = new DateTime(2018, 1, 1); var r = new Random(); int counter = 0; for (int i = 1; i <= numberOfDocs; ++i) { int j = r.Next(0, 3); int y = r.Next(0, 2); var corro = new InvestorDocumentFlat() { InvestorId = investorId, id = Guid.NewGuid(), RefId = investorId.ToString() + i.ToString("D4"), AccountNo = investorId.ToString() + r.Next(100, 999) + i.ToString("D4"), Type = _corroTypes[j], Date = startDate.AddMonths(i), ProductCode = "", ProductName = _prodNames[y] }; try { ItemResponse <InvestorDocumentFlat> response = await _container.CreateItemAsync(corro, new PartitionKey(corro.InvestorId)); ++counter; if (counter % 10 == 0) { Console.WriteLine("INFO: Inserted {0} records", counter); } } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex.Message); break; } } Console.WriteLine("TOTAL: Inserted {0} of lines", counter); }
public async Task <Guid> InsertUpdateDocument(InvestorDocumentFlat document) { using (var client = new CosmosClient(_functionSettings.CosmosDBEndpoint, _functionSettings.CosmosDBMasterKey)) { var container = client.GetContainer(_functionSettings.CosmosDBName, SQLStatements.InvestorDocuments.ContainerName); var sql = SQLStatements.InvestorDocuments.ListInvestorDocs; var query = new QueryDefinition(sql); query.WithParameter("@InvestorId", document.InvestorId); var iterator = container .GetItemQueryIterator <InvestorDocument>(query, requestOptions: new QueryRequestOptions() { MaxConcurrency = 1 }); var docDetails = new DocumentDetails() { RefId = document.RefId, Type = document.Type, AccountNo = document.AccountNo, Date = document.Date, ProductCode = document.ProductCode, ProductName = document.ProductName, Link = document.Link }; while (iterator.HasMoreResults) { var task = iterator.ReadNextAsync(); task.Wait(); FeedResponse <InvestorDocument> response = task.Result; if (response.Any()) { var item = response.FirstOrDefault(); if (item != null) { var d = item.Corro.Where(x => x.RefId == document.RefId).FirstOrDefault(); if (d == null) { item.Corro.Add(docDetails); } else { //Only update the link to document d.Link = document.Link; } await container.ReplaceItemAsync(item, item.id.ToString(), new PartitionKey(item.InvestorId)); return(item.id); } } else { break; } } var doc = new InvestorDocument() { InvestorId = document.InvestorId, id = Guid.NewGuid(), Corro = new List <DocumentDetails>() }; doc.Corro.Add(docDetails); var insertResponse = await container.CreateItemAsync(doc, new PartitionKey(doc.InvestorId)); document.id = insertResponse.Resource.id; } return(document.id); }