diff --git a/.gitignore b/.gitignore index 3ab9570..9a7db08 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ obj .suo .user .vs +appsettings.json diff --git a/DataLayer/DataLayer.csproj b/DataLayer/DataLayer.csproj index f208d30..dfdf56c 100644 --- a/DataLayer/DataLayer.csproj +++ b/DataLayer/DataLayer.csproj @@ -1,7 +1,21 @@ - + net5.0 + + + False + 3.19.0 + Content + Microsoft.Azure.Cosmos + false + + + + + + + diff --git a/DataLayer/PeopleData.cs b/DataLayer/PeopleData.cs index 8813e33..0c88416 100644 --- a/DataLayer/PeopleData.cs +++ b/DataLayer/PeopleData.cs @@ -1,8 +1,42 @@ -using System; +using Microsoft.Azure.Cosmos; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; namespace DataLayer { + /// + /// A Data Access Layer (DAL) object for People. + /// public class PeopleData { + private readonly Container _container; + private string ContainerId { get; set; } + + public PeopleData(CosmosClient client, string databaseId, string containerId = "People") + { + ContainerId = containerId; + _container = client.GetContainer(databaseId, containerId); + } + + /// + /// Gets all people. + /// + /// + public async Task> GetAll() + { + // Define a Query + var query = new QueryDefinition($"SELECT * FROM {ContainerId}"); + + // Get an Iterator + var iterator = _container.GetItemQueryIterator(query); + + // Get the first page of results + var response = await iterator.ReadNextAsync(); + + //TODO: Paging + + return response.Resource; + } } } diff --git a/DataLayer/Person.cs b/DataLayer/Person.cs new file mode 100644 index 0000000..157610a --- /dev/null +++ b/DataLayer/Person.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataLayer +{ + /// + /// A Person Model + /// + /// AKA DTO (data transfer object), POCO (plain old c# object) + public class Person + { + //TODO: Properties + } +} diff --git a/DataLayerTests/CosmosTest.cs b/DataLayerTests/CosmosTest.cs new file mode 100644 index 0000000..deb9961 --- /dev/null +++ b/DataLayerTests/CosmosTest.cs @@ -0,0 +1,30 @@ +using Microsoft.Extensions.Configuration; +using System; +using System.IO; + +namespace DataLayerTests +{ + /// + /// Test helper base class + /// + public abstract class CosmosTest + { + // load configuration from appsettings.json file + protected static readonly IConfiguration _config = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false) + .Build(); + + // Constants for App Settings keys + protected const string CosmosDataConnectionStringKey = "Cosmos_ConnectionString"; + protected const string CosmosDataDatabaseIdKey = "Cosmos_DatabaseId"; + + public CosmosTest() + { + // Ensure app settings exist + if (string.IsNullOrEmpty(_config[CosmosDataConnectionStringKey])) + throw new InvalidOperationException($"{CosmosDataConnectionStringKey} must be set"); + if (string.IsNullOrEmpty(_config[CosmosDataDatabaseIdKey])) + throw new InvalidOperationException($"{CosmosDataDatabaseIdKey} must be set"); } + } +} diff --git a/DataLayerTests/DataLayerTests.csproj b/DataLayerTests/DataLayerTests.csproj index 1b67bb0..f1008bb 100644 --- a/DataLayerTests/DataLayerTests.csproj +++ b/DataLayerTests/DataLayerTests.csproj @@ -7,14 +7,28 @@ + + + + + + + + Always + + + Always + + + diff --git a/DataLayerTests/PeopleDataTests.cs b/DataLayerTests/PeopleDataTests.cs index eb37c40..dfc88bf 100644 --- a/DataLayerTests/PeopleDataTests.cs +++ b/DataLayerTests/PeopleDataTests.cs @@ -1,4 +1,5 @@ using DataLayer; +using Microsoft.Azure.Cosmos; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Linq; using System.Threading.Tasks; @@ -6,14 +7,20 @@ namespace DataLayerTests { [TestClass] - [TestCategory("Integration")] - public class PeopleDataTests + public class PeopleDataTests : CosmosTest { [TestMethod] + // Marks this test as an Integration test to that it can be excluded from CI Builds. + [TestCategory("Integration")] public async Task GetAll() { // arrange - var data = new PeopleData(); + + // Create a CosmosClient + var client = new CosmosClient(_config["Cosmos_ConnectionString"]); + + // Inject into PeopleData. IRL we would do this with IoC + var data = new PeopleData(client, _config["Cosmos_DatabaseId"]); // act var people = await data.GetAll(); diff --git a/DataLayerTests/appsettings.example.json b/DataLayerTests/appsettings.example.json new file mode 100644 index 0000000..6d32c34 --- /dev/null +++ b/DataLayerTests/appsettings.example.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug" + } + }, + "CosmosData_ConnectionString": "", + "CosmosData_DatabaseId": "" +} \ No newline at end of file