Skip to content
This repository was archived by the owner on Jul 12, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cosmonaut.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cosmonaut.System", "tests\C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cosmonaut.Extensions.Microsoft.DependencyInjection", "src\Cosmonaut.Extensions.Microsoft.DependencyInjection\Cosmonaut.Extensions.Microsoft.DependencyInjection.csproj", "{D8538294-44A5-4A32-A110-39A4A4BB83A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cosmonaut.NetCore2WebApi", "samples\Cosmonaut.NetCore2WebApi\Cosmonaut.NetCore2WebApi.csproj", "{AEE4D100-8AE8-4C59-BCAE-BD4F0CCA56D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{D8538294-44A5-4A32-A110-39A4A4BB83A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8538294-44A5-4A32-A110-39A4A4BB83A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8538294-44A5-4A32-A110-39A4A4BB83A5}.Release|Any CPU.Build.0 = Release|Any CPU
{AEE4D100-8AE8-4C59-BCAE-BD4F0CCA56D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AEE4D100-8AE8-4C59-BCAE-BD4F0CCA56D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AEE4D100-8AE8-4C59-BCAE-BD4F0CCA56D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AEE4D100-8AE8-4C59-BCAE-BD4F0CCA56D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,6 +68,7 @@ Global
{C53E8DF6-9172-499E-9919-10A049A234C6} = {42392F64-A623-4C18-BF93-A5181AEE30C1}
{87BB5EC9-0781-476E-8336-C3288ABCB591} = {1900E4D6-A0A0-4675-A049-AA2843143E01}
{D8538294-44A5-4A32-A110-39A4A4BB83A5} = {42392F64-A623-4C18-BF93-A5181AEE30C1}
{AEE4D100-8AE8-4C59-BCAE-BD4F0CCA56D4} = {E50F9511-8F2F-4753-8D4B-B0CAD4E5D573}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CBCF8635-B781-4296-9756-2D461FD0AE27}
Expand Down
2 changes: 1 addition & 1 deletion samples/Cosmonaut.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static async Task Main(string[] args)
settings.ConnectionPolicy = connectionPolicy;
settings.DefaultCollectionThroughput = 5000;
});

serviceCollection.AddCosmosStore<Car>(cosmosSettings);

var provider = serviceCollection.BuildServiceProvider();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Cosmonaut.NetCore2WebApi.Contract
{
public class CreatePersonRequest
{
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Cosmonaut.NetCore2WebApi.Contract
{
public class EditPersonRequest
{
public string Name { get; set; }
}
}
10 changes: 10 additions & 0 deletions samples/Cosmonaut.NetCore2WebApi/Contract/GetPersonResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Cosmonaut.NetCore2WebApi.Contract
{
public class GetPersonResponse
{
public string Identifier { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Cosmonaut.NetCore2WebApi.Contract
{
public class UpsertPersonRequest
{
public string Id { get; set; }
public string Name { get; set; }
}
}
169 changes: 169 additions & 0 deletions samples/Cosmonaut.NetCore2WebApi/Controllers/SampleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Cosmonaut.Extensions;
using Cosmonaut.NetCore2WebApi.Contract;
using Cosmonaut.NetCore2WebApi.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Documents.SystemFunctions;
using Microsoft.EntityFrameworkCore;

namespace Cosmonaut.NetCore2WebApi.Controllers
{
public class SampleController : Controller
{
private readonly ICosmosStore<Person> _peopleCosmosStore;

public SampleController(ICosmosStore<Person> peopleCosmosStore)
{
_peopleCosmosStore = peopleCosmosStore;
}

[HttpGet("/api/people")]
public async Task<IActionResult> GetAllPaged([FromQuery] string continuationToken,
CancellationToken cancellationToken, [FromQuery] int pageSize = 10)
{
if (pageSize > 50 || pageSize <= 0)
{
return BadRequest("Page Size Invalid! Must be 0 < pageSize < 50");
}

if (continuationToken == null || continuationToken.Trim().Length == 0)
{
var initialPage = await _peopleCosmosStore.Query().OrderBy(person => person.Name)
.WithPagination(1, pageSize).ToPagedListAsync(cancellationToken: cancellationToken);

return Ok(initialPage);
}

var page = await _peopleCosmosStore.Query().OrderBy(person => person.Name)
.WithPagination(continuationToken, pageSize).ToPagedListAsync(cancellationToken: cancellationToken);

return Ok(page);
}

[HttpGet("/api/people/{id:Guid}")]
public async Task<IActionResult> GetById(Guid id, CancellationToken cancellationToken)
{
if (id == Guid.Empty)
{
return BadRequest("Invalid Person Id");
}

var person =
await _peopleCosmosStore.FindAsync(id.ToString(), cancellationToken: cancellationToken);

if (person == null)
{
return NotFound();
}

var response = new GetPersonResponse
{
Identifier = person.Id,
Name = person.Name,
};

return Ok(response);
}

[HttpPost("/api/people")]
public async Task<IActionResult> Create([FromBody] CreatePersonRequest request,
CancellationToken cancellationToken)
{
var personToCreate = new Person
{
Name = request.Name
};

var result = await _peopleCosmosStore.UpsertAsync(personToCreate, cancellationToken: cancellationToken);

var personId = result.Entity.Id;

if (personId == null)
{
return BadRequest();
}

return Ok(personId);
}

[HttpPut("/api/people/{id:Guid}")]
public async Task<IActionResult> Update(Guid id, [FromBody] EditPersonRequest request,
CancellationToken cancellationToken)
{
if (id == Guid.Empty)
{
return BadRequest("Invalid Person Id");
}

if (request.Name.Trim().Length == 0)
{
return BadRequest("Person Name can not be empty");
}

var existingPerson =
await _peopleCosmosStore.FindAsync(id.ToString(), cancellationToken: cancellationToken);

if (existingPerson == null)
{
return NotFound();
}

var personToSave = new Person
{
Id = id.ToString(),
Name = request.Name
};

var result = await _peopleCosmosStore.UpdateAsync(personToSave, cancellationToken: cancellationToken);

var personId = result.Entity.Id;

if (personId == null)
{
return BadRequest();
}

return Ok(personId);
}

[HttpDelete("/api/people/{id:Guid}")]
public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationToken)
{
if (id == Guid.Empty)
{
return BadRequest();
}

await _peopleCosmosStore.RemoveByIdAsync(id.ToString(), cancellationToken: cancellationToken);

return Ok();
}

//Alternative to having 1 endpoint for Creation and 1 for Update -> Upsert
[HttpPut("/api/people")]
public async Task<IActionResult> Upsert([FromBody] UpsertPersonRequest request,
CancellationToken cancellationToken)
{
var personToUpsert = new Person
{
Id = request.Id,
Name = request.Name
};

var result = await _peopleCosmosStore.UpsertAsync(personToUpsert, cancellationToken: cancellationToken);

var personId = result.Entity.Id;

if (personId == null)
{
return BadRequest();
}

return Ok(personId);
}
}
}
21 changes: 21 additions & 0 deletions samples/Cosmonaut.NetCore2WebApi/Cosmonaut.NetCore2WebApi.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Cosmonaut" Version="2.4.0"/>
<PackageReference Include="Cosmonaut.Extensions.Microsoft.DependencyInjection" Version="2.3.0"/>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8"/>
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4"/>
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions samples/Cosmonaut.NetCore2WebApi/CosmonautConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Cosmonaut.Extensions.Microsoft.DependencyInjection;
using Cosmonaut.NetCore2WebApi.Model;
using Microsoft.Azure.Documents.Client;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Cosmonaut.NetCore2WebApi
{
public class CosmonautConfiguration
{
public static void Configure(IServiceCollection services, IConfiguration configuration)
{
//Look at appsettings.Development.json | https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1
var cosmosConfig = new AppSettingsSection();
configuration.GetSection("CosmosDb").Bind(cosmosConfig);

var connectionPolicy = new ConnectionPolicy
{
ConnectionProtocol = Protocol.Tcp,
ConnectionMode = ConnectionMode.Direct
};

var cosmosSettings = new CosmosStoreSettings(cosmosConfig.CosmosDatabaseName,
cosmosConfig.CosmosDatabaseUrl,
cosmosConfig.CosmosAuthKey
, connectionPolicy
, defaultCollectionThroughput: cosmosConfig.DefaultConnectionThroughput);

AddCosmosStores(services, cosmosSettings);
}

private static void AddCosmosStores(IServiceCollection services, CosmosStoreSettings cosmosSettings)
{
services.AddCosmosStore<Person>(cosmosSettings);
}
}

public class AppSettingsSection
{
public AppSettingsSection()
{
DefaultConnectionThroughput = 5000;
}

public string CosmosDatabaseName { get; set; }
public string CosmosDatabaseUrl { get; set; }
public string CosmosAuthKey { get; set; }
public int DefaultConnectionThroughput { get; set; }
}
}
12 changes: 12 additions & 0 deletions samples/Cosmonaut.NetCore2WebApi/Model/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;

namespace Cosmonaut.NetCore2WebApi.Model
{
public class Person
{
[JsonProperty("id")]
public string Id { get; set; }

public string Name { get; set; }
}
}
25 changes: 25 additions & 0 deletions samples/Cosmonaut.NetCore2WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Cosmonaut.NetCore2WebApi
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
Loading