fork from 1.3
This commit is contained in:
9
FreeSR.Database/Configuration/DatabaseConfiguration.cs
Normal file
9
FreeSR.Database/Configuration/DatabaseConfiguration.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace FreeSR.Database.Configuration
|
||||
{
|
||||
public class DatabaseConfiguration
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DatabaseEntry[] Entries { get; set; }
|
||||
}
|
||||
}
|
||||
8
FreeSR.Database/Configuration/DatabaseEntry.cs
Normal file
8
FreeSR.Database/Configuration/DatabaseEntry.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace FreeSR.Database.Configuration
|
||||
{
|
||||
public class DatabaseEntry
|
||||
{
|
||||
public string CollectionName { get; set; }
|
||||
public DatabaseType Type { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace FreeSR.Database.Configuration
|
||||
{
|
||||
using FreeSR.Shared.Exceptions;
|
||||
|
||||
internal class DatabaseMisconfiguredException : ServerInitializationException
|
||||
{
|
||||
public DatabaseMisconfiguredException(string message) : base(message)
|
||||
{
|
||||
// DatabaseMisconfiguredException.
|
||||
}
|
||||
}
|
||||
}
|
||||
7
FreeSR.Database/Configuration/DatabaseType.cs
Normal file
7
FreeSR.Database/Configuration/DatabaseType.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace FreeSR.Database.Configuration
|
||||
{
|
||||
public enum DatabaseType
|
||||
{
|
||||
Account
|
||||
}
|
||||
}
|
||||
55
FreeSR.Database/DatabaseManager.cs
Normal file
55
FreeSR.Database/DatabaseManager.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
namespace FreeSR.Database
|
||||
{
|
||||
using FreeSR.Database.Configuration;
|
||||
using FreeSR.Shared;
|
||||
using MongoDB.Driver;
|
||||
using NLog;
|
||||
|
||||
public sealed class DatabaseManager : Singleton<DatabaseManager>
|
||||
{
|
||||
private static readonly Logger s_log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private DatabaseConfiguration _configuration;
|
||||
private Dictionary<Type, object> _databases;
|
||||
|
||||
public IMongoDatabase MongoDatabase { get; private set; }
|
||||
|
||||
private DatabaseManager()
|
||||
{
|
||||
_databases = new Dictionary<Type, object>();
|
||||
}
|
||||
|
||||
public void Initialize(DatabaseConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
|
||||
var mongoClient = new MongoClient(configuration.ConnectionString);
|
||||
MongoDatabase = mongoClient.GetDatabase(configuration.Name);
|
||||
}
|
||||
|
||||
public string GetCollectionName(DatabaseType databaseType)
|
||||
{
|
||||
foreach (var entry in _configuration.Entries)
|
||||
{
|
||||
if (entry.Type == databaseType)
|
||||
return entry.CollectionName;
|
||||
}
|
||||
|
||||
throw new DatabaseMisconfiguredException($"Can not find database of type {databaseType} in provided configuration.");
|
||||
}
|
||||
|
||||
public DatabaseManager Add<T>(ISRDatabase<T> database) where T : class
|
||||
{
|
||||
_databases.Add(database.GetType(), database);
|
||||
return this;
|
||||
}
|
||||
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
if (_databases.TryGetValue(typeof(T), out var database))
|
||||
return database as T;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
FreeSR.Database/FreeSR.Database.csproj
Normal file
17
FreeSR.Database/FreeSR.Database.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FreeSR.Shared\FreeSR.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
18
FreeSR.Database/ISRDatabase.cs
Normal file
18
FreeSR.Database/ISRDatabase.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace FreeSR.Database
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
public interface ISRDatabase<T> where T : class
|
||||
{
|
||||
Task Insert(T document);
|
||||
Task InsertMany(IEnumerable<T> documents);
|
||||
Task<List<T>> Find(Expression<Func<T, bool>> filter);
|
||||
Task<T> FindOne(Expression<Func<T, bool>> filter);
|
||||
Task Update(Expression<Func<T, bool>> filter, T updatedDocument);
|
||||
Task Delete(Expression<Func<T, bool>> filter);
|
||||
Task<long> Count();
|
||||
Task<T> FindMax(Expression<Func<T, object>> fieldSelector);
|
||||
}
|
||||
}
|
||||
58
FreeSR.Database/Mongo/SRMongoDatabase.cs
Normal file
58
FreeSR.Database/Mongo/SRMongoDatabase.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
namespace FreeSR.Database.Mongo
|
||||
{
|
||||
using MongoDB.Driver;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
public class SRMongoDatabase<T> : ISRDatabase<T> where T : class
|
||||
{
|
||||
protected readonly IMongoCollection<T> _collection;
|
||||
|
||||
public SRMongoDatabase(IMongoDatabase database, string collectionName)
|
||||
{
|
||||
_collection = database.GetCollection<T>(collectionName);
|
||||
}
|
||||
|
||||
public async Task Insert(T document)
|
||||
{
|
||||
await _collection.InsertOneAsync(document);
|
||||
}
|
||||
|
||||
public async Task InsertMany(IEnumerable<T> documents)
|
||||
{
|
||||
await _collection.InsertManyAsync(documents);
|
||||
}
|
||||
|
||||
public async Task<List<T>> Find(Expression<Func<T, bool>> filter)
|
||||
{
|
||||
var result = await _collection.FindAsync(filter);
|
||||
return await result.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<T> FindOne(Expression<Func<T, bool>> filter)
|
||||
{
|
||||
var result = await _collection.FindAsync(filter);
|
||||
return await result.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task Update(Expression<Func<T, bool>> filter, T updatedDocument)
|
||||
{
|
||||
await _collection.ReplaceOneAsync(filter, updatedDocument);
|
||||
}
|
||||
|
||||
public async Task Delete(Expression<Func<T, bool>> filter)
|
||||
{
|
||||
await _collection.DeleteOneAsync(filter);
|
||||
}
|
||||
|
||||
public async Task<long> Count()
|
||||
{
|
||||
return await _collection.CountDocumentsAsync(Builders<T>.Filter.Empty);
|
||||
}
|
||||
|
||||
public async Task<T> FindMax(Expression<Func<T, object>> fieldSelector)
|
||||
{
|
||||
var sortDefinition = Builders<T>.Sort.Descending(fieldSelector);
|
||||
return await _collection.Find(Builders<T>.Filter.Empty).Sort(sortDefinition).FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user