Add MongoDB

This commit is contained in:
Robert Marshall 2020-06-22 19:42:42 +01:00
parent 1ce9373949
commit 0d0c077d84
9 changed files with 96 additions and 6 deletions

View file

@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Data", "Robware.Dat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "..\build\_build.csproj", "{19A36DA9-BFBF-4988-B7C7-4808D6B57246}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robware.Data.MongoDB", "Robware.Data.MongoDB\Robware.Data.MongoDB.csproj", "{574A8F26-06E4-4225-AE82-5824A721DD10}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -43,6 +45,14 @@ Global
{69989FA2-BEE8-491D-97B9-856D4916D154}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69989FA2-BEE8-491D-97B9-856D4916D154}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69989FA2-BEE8-491D-97B9-856D4916D154}.Release|Any CPU.Build.0 = Release|Any CPU
{574A8F26-06E4-4225-AE82-5824A721DD10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{574A8F26-06E4-4225-AE82-5824A721DD10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{574A8F26-06E4-4225-AE82-5824A721DD10}.Release|Any CPU.ActiveCfg = Release|Any CPU
{574A8F26-06E4-4225-AE82-5824A721DD10}.Release|Any CPU.Build.0 = Release|Any CPU
{C1EF16D4-1D62-4396-B456-F4C3E203C4CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1EF16D4-1D62-4396-B456-F4C3E203C4CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1EF16D4-1D62-4396-B456-F4C3E203C4CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1EF16D4-1D62-4396-B456-F4C3E203C4CD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

@ -6,7 +6,11 @@
<ItemGroup>
<ProjectReference Include="..\Robware.Auth\Robware.Auth.csproj" />
<ProjectReference Include="..\Robware.Data\Robware.Data.csproj" />
<ProjectReference Include="..\Robware.Data.MongoDB\Robware.Data.MongoDB.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
</ItemGroup>

View file

@ -3,10 +3,10 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MongoDB.Driver;
using Robware.Auth.API;
using Robware.Auth.Users;
using Robware.Data.API;
using Robware.Data.Users;
using Robware.Data.MongoDB;
namespace Robware.Api.Auth {
public class Startup {
@ -22,12 +22,17 @@ namespace Robware.Api.Auth {
services.AddSingleton<ICryptographyProvider, CryptographyProvider>()
.AddSingleton<IAuthenticator, Authenticator>()
.AddSingleton<IDatabaseProvider>(new MySQLDatabaseProvider(Configuration.GetConnectionString("database")))
.AddSingleton<IMongoDatabase>(SetupMongoDatabase())
.AddSingleton<IUsers, UserRepository>()
.AddSingleton<IApiKeyValidator, ApiKeyValidator>()
.AddSingleton<IApiKeys, ApiKeyRepository>();
}
private IMongoDatabase SetupMongoDatabase() {
var client = new MongoClient(Configuration.GetConnectionString("database"));
return client.GetDatabase("robware");
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if (env.IsDevelopment()) {

View file

@ -9,11 +9,11 @@
"Kestrel": {
"EndPoints": {
"Http": {
"Url": "http://0.0.0.0:5002"
"Url": "http://0.0.0.0:5003"
}
}
},
"ConnectionStrings": {
"database": "Server=localhost;User ID=user;Password=pass;Database=db"
"database": "mongodb://localhost"
}
}

View file

@ -0,0 +1,10 @@
using Robware.Auth.API;
using System.Threading.Tasks;
namespace Robware.Data.MongoDB {
public class ApiKeyRepository : IApiKeys {
public Task<ApiKey> Get(string key) {
throw new System.NotImplementedException();
}
}
}

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.10.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Robware.Auth\Robware.Auth.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,23 @@
using MongoDB.Driver;
using Robware.Auth.Users;
using Robware.Data.MongoDB.Users;
using Robware.Data.MongoDB.Users.States;
using System.Threading.Tasks;
namespace Robware.Data.MongoDB {
public class UserRepository : IUsers {
IMongoCollection<UserState> _collection;
public UserRepository(IMongoDatabase database) {
_collection = database.GetCollection<UserState>("users");
}
public async Task<User> GetByEmail(string email) {
var result = (await _collection.FindAsync(user => user.Email == email)).FirstOrDefault();
if (result == null)
throw new UserNotFoundException(email);
return new DatabaseUser(result);
}
}
}

View file

@ -0,0 +1,11 @@
using Robware.Auth.Users;
using Robware.Data.MongoDB.Users.States;
namespace Robware.Data.MongoDB.Users {
public class DatabaseUser : User {
public DatabaseUser(UserState state) {
Username = state.Email;
Password = state.Password;
}
}
}

View file

@ -0,0 +1,12 @@
using MongoDB.Bson.Serialization.Attributes;
using System;
namespace Robware.Data.MongoDB.Users.States {
public class UserState {
[BsonId]
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime Created{ get; set; }
}
}