mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 19:22:45 +00:00
open source Notesnook API
This commit is contained in:
17
Streetwriters.Data/Attributes/BsonCollection.cs
Normal file
17
Streetwriters.Data/Attributes/BsonCollection.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Streetwriters.Data.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class BsonCollectionAttribute : Attribute
|
||||
{
|
||||
public string CollectionName { get; }
|
||||
public string DatabaseName { get; }
|
||||
|
||||
public BsonCollectionAttribute(string databaseName, string collectionName)
|
||||
{
|
||||
CollectionName = collectionName;
|
||||
DatabaseName = databaseName;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
Streetwriters.Data/DbContexts/MongoDbContext.cs
Normal file
90
Streetwriters.Data/DbContexts/MongoDbContext.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Driver;
|
||||
using Streetwriters.Data.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Streetwriters.Data.DbContexts
|
||||
{
|
||||
public class MongoDbContext : IDbContext
|
||||
{
|
||||
private IMongoDatabase Database { get; set; }
|
||||
private MongoClient MongoClient { get; set; }
|
||||
private readonly List<Func<IClientSessionHandle, CancellationToken, Task>> _commands;
|
||||
private IDbSettings DbSettings { get; set; }
|
||||
public MongoDbContext(IDbSettings dbSettings)
|
||||
{
|
||||
DbSettings = dbSettings;
|
||||
Configure();
|
||||
// Every command will be stored and it'll be processed at SaveChanges
|
||||
_commands = new List<Func<IClientSessionHandle, CancellationToken, Task>>();
|
||||
}
|
||||
|
||||
public async Task<int> SaveChanges()
|
||||
{
|
||||
try
|
||||
{
|
||||
var count = _commands.Count;
|
||||
|
||||
using (IClientSessionHandle session = await MongoClient.StartSessionAsync())
|
||||
{
|
||||
#if DEBUG
|
||||
await Task.WhenAll(_commands.Select(c => c(session, default(CancellationToken))));
|
||||
#else
|
||||
await session.WithTransactionAsync(async (handle, token) =>
|
||||
{
|
||||
await Task.WhenAll(_commands.Select(c => c(handle, token)));
|
||||
return true;
|
||||
});
|
||||
#endif
|
||||
|
||||
}
|
||||
return count;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO use Slogger here.
|
||||
await Console.Error.WriteLineAsync(ex.ToString());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void Configure()
|
||||
{
|
||||
if (MongoClient != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var settings = MongoClientSettings.FromConnectionString(DbSettings.ConnectionString);
|
||||
settings.MaxConnectionPoolSize = 5000;
|
||||
settings.MinConnectionPoolSize = 300;
|
||||
MongoClient = new MongoClient(settings);
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>(string databaseName, string collectionName)
|
||||
{
|
||||
return MongoClient.GetDatabase(databaseName).GetCollection<T>(collectionName, new MongoCollectionSettings()
|
||||
{
|
||||
AssignIdOnInsert = true,
|
||||
});
|
||||
}
|
||||
|
||||
public void AddCommand(Func<IClientSessionHandle, CancellationToken, Task> func)
|
||||
{
|
||||
_commands.Add(func);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public Task DropDatabaseAsync()
|
||||
{
|
||||
return MongoClient.DropDatabaseAsync(DbSettings.DatabaseName);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Streetwriters.Data/DbSettings.cs
Normal file
10
Streetwriters.Data/DbSettings.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Streetwriters.Data.Interfaces;
|
||||
|
||||
namespace Streetwriters.Data
|
||||
{
|
||||
public class DbSettings : IDbSettings
|
||||
{
|
||||
public string DatabaseName { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
||||
15
Streetwriters.Data/Interfaces/IDbContext.cs
Normal file
15
Streetwriters.Data/Interfaces/IDbContext.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Streetwriters.Data.Interfaces
|
||||
{
|
||||
public interface IDbContext : IDisposable
|
||||
{
|
||||
void AddCommand(Func<IClientSessionHandle, CancellationToken, Task> func);
|
||||
Task<int> SaveChanges();
|
||||
IMongoCollection<T> GetCollection<T>(string databaseName, string collectionName);
|
||||
}
|
||||
}
|
||||
8
Streetwriters.Data/Interfaces/IDbSettings.cs
Normal file
8
Streetwriters.Data/Interfaces/IDbSettings.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Streetwriters.Data.Interfaces
|
||||
{
|
||||
public interface IDbSettings
|
||||
{
|
||||
string DatabaseName { get; set; }
|
||||
string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
||||
10
Streetwriters.Data/Interfaces/IUnitOfWork.cs
Normal file
10
Streetwriters.Data/Interfaces/IUnitOfWork.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Streetwriters.Data.Interfaces
|
||||
{
|
||||
public interface IUnitOfWork : IDisposable
|
||||
{
|
||||
Task<bool> Commit();
|
||||
}
|
||||
}
|
||||
119
Streetwriters.Data/Repositories/Repository.cs
Normal file
119
Streetwriters.Data/Repositories/Repository.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Streetwriters.Data.Attributes;
|
||||
using Streetwriters.Data.Interfaces;
|
||||
|
||||
namespace Streetwriters.Data.Repositories
|
||||
{
|
||||
public class Repository<TEntity> where TEntity : class
|
||||
{
|
||||
protected readonly IDbContext dbContext;
|
||||
protected IMongoCollection<TEntity> Collection { get; set; }
|
||||
|
||||
public Repository(IDbContext _dbContext)
|
||||
{
|
||||
dbContext = _dbContext;
|
||||
Collection = GetCollection();
|
||||
}
|
||||
|
||||
private protected IMongoCollection<TEntity> GetCollection()
|
||||
{
|
||||
var attribute = (BsonCollectionAttribute)typeof(TEntity).GetCustomAttributes(
|
||||
typeof(BsonCollectionAttribute),
|
||||
true).FirstOrDefault();
|
||||
if (string.IsNullOrEmpty(attribute.CollectionName) || string.IsNullOrEmpty(attribute.DatabaseName)) throw new Exception("Could not get a valid collection or database name.");
|
||||
return dbContext.GetCollection<TEntity>(attribute.DatabaseName, attribute.CollectionName);
|
||||
}
|
||||
|
||||
|
||||
public virtual void Insert(TEntity obj)
|
||||
{
|
||||
dbContext.AddCommand((handle, ct) => Collection.InsertOneAsync(handle, obj, null, ct));
|
||||
}
|
||||
|
||||
|
||||
public virtual Task InsertAsync(TEntity obj)
|
||||
{
|
||||
return Collection.InsertOneAsync(obj);
|
||||
}
|
||||
|
||||
public virtual void Upsert(TEntity obj, Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, filterExpression, obj, new ReplaceOptions { IsUpsert = true }, ct));
|
||||
}
|
||||
|
||||
public virtual Task UpsertAsync(TEntity obj, Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
return Collection.ReplaceOneAsync(filterExpression, obj, new ReplaceOptions { IsUpsert = true });
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> FindOneAsync(Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
var data = await Collection.FindAsync(filterExpression);
|
||||
return data.FirstOrDefault();
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> GetAsync(string id)
|
||||
{
|
||||
var data = await Collection.FindAsync(Builders<TEntity>.Filter.Eq("_id", ObjectId.Parse(id)));
|
||||
return data.FirstOrDefault();
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
var data = await Collection.FindAsync(filterExpression);
|
||||
return data.ToList();
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntity>> GetAllAsync()
|
||||
{
|
||||
var all = await Collection.FindAsync(Builders<TEntity>.Filter.Empty);
|
||||
return all.ToList();
|
||||
}
|
||||
|
||||
public virtual void Update(string id, TEntity obj)
|
||||
{
|
||||
dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, Builders<TEntity>.Filter.Eq("_id", ObjectId.Parse(id)), obj, cancellationToken: ct));
|
||||
}
|
||||
|
||||
public virtual Task UpdateAsync(string id, TEntity obj)
|
||||
{
|
||||
return Collection.ReplaceOneAsync(Builders<TEntity>.Filter.Eq("_id", ObjectId.Parse(id)), obj);
|
||||
}
|
||||
|
||||
public virtual void DeleteById(string id)
|
||||
{
|
||||
dbContext.AddCommand((handle, ct) => Collection.DeleteOneAsync(handle, Builders<TEntity>.Filter.Eq("_id", ObjectId.Parse(id)), cancellationToken: ct));
|
||||
}
|
||||
|
||||
public virtual Task DeleteByIdAsync(string id)
|
||||
{
|
||||
return Collection.DeleteOneAsync(Builders<TEntity>.Filter.Eq("_id", ObjectId.Parse(id)));
|
||||
}
|
||||
|
||||
public virtual void Delete(Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
dbContext.AddCommand((handle, ct) => Collection.DeleteOneAsync(handle, filterExpression, cancellationToken: ct));
|
||||
}
|
||||
|
||||
public virtual void DeleteMany(Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
dbContext.AddCommand((handle, ct) => Collection.DeleteManyAsync(handle, filterExpression, cancellationToken: ct));
|
||||
}
|
||||
|
||||
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
return Collection.DeleteOneAsync(filterExpression);
|
||||
}
|
||||
|
||||
public virtual Task DeleteManyAsync(Expression<Func<TEntity, bool>> filterExpression)
|
||||
{
|
||||
return Collection.DeleteManyAsync(filterExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Streetwriters.Data/Streetwriters.Data.csproj
Normal file
15
Streetwriters.Data/Streetwriters.Data.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.13.2" />
|
||||
<PackageReference Include="MongoDB.Driver.Core" Version="2.13.2" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="2.13.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
27
Streetwriters.Data/UnitOfWork.cs
Normal file
27
Streetwriters.Data/UnitOfWork.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Streetwriters.Data.Interfaces;
|
||||
|
||||
namespace Streetwriters.Data
|
||||
{
|
||||
public class UnitOfWork : IUnitOfWork
|
||||
{
|
||||
private readonly IDbContext dbContext;
|
||||
|
||||
public UnitOfWork(IDbContext _dbContext)
|
||||
{
|
||||
dbContext = _dbContext;
|
||||
}
|
||||
|
||||
public async Task<bool> Commit()
|
||||
{
|
||||
var changeAmount = await dbContext.SaveChanges();
|
||||
return changeAmount > 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.dbContext.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user