mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-08-14 20:40:18 +02:00
sync: remove item type specific model classes & simplify sync repository usage
This commit is contained in:
@@ -17,59 +17,76 @@ You should have received a copy of the Affero GNU General Public License
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using MongoDB.Driver;
|
||||||
using Notesnook.API.Interfaces;
|
using Notesnook.API.Interfaces;
|
||||||
using Notesnook.API.Models;
|
using Notesnook.API.Models;
|
||||||
using Notesnook.API.Repositories;
|
using Notesnook.API.Repositories;
|
||||||
|
using Streetwriters.Data.Interfaces;
|
||||||
using Streetwriters.Data.Repositories;
|
using Streetwriters.Data.Repositories;
|
||||||
|
|
||||||
namespace Notesnook.API.Accessors
|
namespace Notesnook.API.Accessors
|
||||||
{
|
{
|
||||||
public class SyncItemsRepositoryAccessor : ISyncItemsRepositoryAccessor
|
public class SyncItemsRepositoryAccessor : ISyncItemsRepositoryAccessor
|
||||||
{
|
{
|
||||||
public SyncItemsRepository<Note> Notes { get; }
|
public SyncItemsRepository Notes { get; }
|
||||||
public SyncItemsRepository<Notebook> Notebooks { get; }
|
public SyncItemsRepository Notebooks { get; }
|
||||||
public SyncItemsRepository<Shortcut> Shortcuts { get; }
|
public SyncItemsRepository Shortcuts { get; }
|
||||||
public SyncItemsRepository<Relation> Relations { get; }
|
public SyncItemsRepository Relations { get; }
|
||||||
public SyncItemsRepository<Reminder> Reminders { get; }
|
public SyncItemsRepository Reminders { get; }
|
||||||
public SyncItemsRepository<Content> Contents { get; }
|
public SyncItemsRepository Contents { get; }
|
||||||
public SyncItemsRepository<Setting> LegacySettings { get; }
|
public SyncItemsRepository LegacySettings { get; }
|
||||||
public SyncItemsRepository<SettingItem> Settings { get; }
|
public SyncItemsRepository Settings { get; }
|
||||||
public SyncItemsRepository<Attachment> Attachments { get; }
|
public SyncItemsRepository Attachments { get; }
|
||||||
public SyncItemsRepository<Color> Colors { get; }
|
public SyncItemsRepository Colors { get; }
|
||||||
public SyncItemsRepository<Vault> Vaults { get; }
|
public SyncItemsRepository Vaults { get; }
|
||||||
public SyncItemsRepository<Tag> Tags { get; }
|
public SyncItemsRepository Tags { get; }
|
||||||
public Repository<UserSettings> UsersSettings { get; }
|
public Repository<UserSettings> UsersSettings { get; }
|
||||||
public Repository<Monograph> Monographs { get; }
|
public Repository<Monograph> Monographs { get; }
|
||||||
|
|
||||||
public SyncItemsRepositoryAccessor(SyncItemsRepository<Note> _notes,
|
public SyncItemsRepositoryAccessor(IDbContext dbContext,
|
||||||
SyncItemsRepository<Notebook> _notebooks,
|
|
||||||
SyncItemsRepository<Content> _content,
|
[FromKeyedServices(Collections.NotebooksKey)]
|
||||||
SyncItemsRepository<Setting> _legacySettings,
|
IMongoCollection<SyncItem> notebooks,
|
||||||
SyncItemsRepository<SettingItem> _settings,
|
[FromKeyedServices(Collections.NotesKey)]
|
||||||
SyncItemsRepository<Attachment> _attachments,
|
IMongoCollection<SyncItem> notes,
|
||||||
SyncItemsRepository<Shortcut> _shortcuts,
|
[FromKeyedServices(Collections.ContentKey)]
|
||||||
SyncItemsRepository<Relation> _relations,
|
IMongoCollection<SyncItem> content,
|
||||||
SyncItemsRepository<Reminder> _reminders,
|
[FromKeyedServices(Collections.SettingsKey)]
|
||||||
SyncItemsRepository<Color> _colors,
|
IMongoCollection<SyncItem> settings,
|
||||||
SyncItemsRepository<Vault> _vaults,
|
[FromKeyedServices(Collections.LegacySettingsKey)]
|
||||||
SyncItemsRepository<Tag> _tags,
|
IMongoCollection<SyncItem> legacySettings,
|
||||||
Repository<UserSettings> _usersSettings,
|
[FromKeyedServices(Collections.AttachmentsKey)]
|
||||||
Repository<Monograph> _monographs)
|
IMongoCollection<SyncItem> attachments,
|
||||||
|
[FromKeyedServices(Collections.ShortcutsKey)]
|
||||||
|
IMongoCollection<SyncItem> shortcuts,
|
||||||
|
[FromKeyedServices(Collections.RemindersKey)]
|
||||||
|
IMongoCollection<SyncItem> reminders,
|
||||||
|
[FromKeyedServices(Collections.RelationsKey)]
|
||||||
|
IMongoCollection<SyncItem> relations,
|
||||||
|
[FromKeyedServices(Collections.ColorsKey)]
|
||||||
|
IMongoCollection<SyncItem> colors,
|
||||||
|
[FromKeyedServices(Collections.VaultsKey)]
|
||||||
|
IMongoCollection<SyncItem> vaults,
|
||||||
|
[FromKeyedServices(Collections.TagsKey)]
|
||||||
|
IMongoCollection<SyncItem> tags,
|
||||||
|
|
||||||
|
Repository<UserSettings> usersSettings, Repository<Monograph> monographs)
|
||||||
{
|
{
|
||||||
Notebooks = _notebooks;
|
UsersSettings = usersSettings;
|
||||||
Notes = _notes;
|
Monographs = monographs;
|
||||||
Contents = _content;
|
Notebooks = new SyncItemsRepository(dbContext, notebooks);
|
||||||
Settings = _settings;
|
Notes = new SyncItemsRepository(dbContext, notes);
|
||||||
LegacySettings = _legacySettings;
|
Contents = new SyncItemsRepository(dbContext, content);
|
||||||
Attachments = _attachments;
|
Settings = new SyncItemsRepository(dbContext, settings);
|
||||||
UsersSettings = _usersSettings;
|
LegacySettings = new SyncItemsRepository(dbContext, legacySettings);
|
||||||
Monographs = _monographs;
|
Attachments = new SyncItemsRepository(dbContext, attachments);
|
||||||
Shortcuts = _shortcuts;
|
Shortcuts = new SyncItemsRepository(dbContext, shortcuts);
|
||||||
Reminders = _reminders;
|
Reminders = new SyncItemsRepository(dbContext, reminders);
|
||||||
Relations = _relations;
|
Relations = new SyncItemsRepository(dbContext, relations);
|
||||||
Colors = _colors;
|
Colors = new SyncItemsRepository(dbContext, colors);
|
||||||
Vaults = _vaults;
|
Vaults = new SyncItemsRepository(dbContext, vaults);
|
||||||
Tags = _tags;
|
Tags = new SyncItemsRepository(dbContext, tags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
namespace Notesnook.API
|
||||||
|
{
|
||||||
|
public class Collections
|
||||||
|
{
|
||||||
|
public const string SettingsKey = "settingsv2";
|
||||||
|
public const string AttachmentsKey = "attachments";
|
||||||
|
public const string ContentKey = "content";
|
||||||
|
public const string NotesKey = "notes";
|
||||||
|
public const string NotebooksKey = "notebooks";
|
||||||
|
public const string RelationsKey = "relations";
|
||||||
|
public const string RemindersKey = "reminders";
|
||||||
|
public const string LegacySettingsKey = "settings";
|
||||||
|
public const string ShortcutsKey = "shortcuts";
|
||||||
|
public const string TagsKey = "tags";
|
||||||
|
public const string ColorsKey = "colors";
|
||||||
|
public const string VaultsKey = "vaults";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,18 +26,18 @@ namespace Notesnook.API.Interfaces
|
|||||||
{
|
{
|
||||||
public interface ISyncItemsRepositoryAccessor
|
public interface ISyncItemsRepositoryAccessor
|
||||||
{
|
{
|
||||||
SyncItemsRepository<Note> Notes { get; }
|
SyncItemsRepository Notes { get; }
|
||||||
SyncItemsRepository<Notebook> Notebooks { get; }
|
SyncItemsRepository Notebooks { get; }
|
||||||
SyncItemsRepository<Shortcut> Shortcuts { get; }
|
SyncItemsRepository Shortcuts { get; }
|
||||||
SyncItemsRepository<Reminder> Reminders { get; }
|
SyncItemsRepository Reminders { get; }
|
||||||
SyncItemsRepository<Relation> Relations { get; }
|
SyncItemsRepository Relations { get; }
|
||||||
SyncItemsRepository<Content> Contents { get; }
|
SyncItemsRepository Contents { get; }
|
||||||
SyncItemsRepository<Setting> LegacySettings { get; }
|
SyncItemsRepository LegacySettings { get; }
|
||||||
SyncItemsRepository<Attachment> Attachments { get; }
|
SyncItemsRepository Attachments { get; }
|
||||||
SyncItemsRepository<SettingItem> Settings { get; }
|
SyncItemsRepository Settings { get; }
|
||||||
SyncItemsRepository<Color> Colors { get; }
|
SyncItemsRepository Colors { get; }
|
||||||
SyncItemsRepository<Vault> Vaults { get; }
|
SyncItemsRepository Vaults { get; }
|
||||||
SyncItemsRepository<Tag> Tags { get; }
|
SyncItemsRepository Tags { get; }
|
||||||
Repository<UserSettings> UsersSettings { get; }
|
Repository<UserSettings> UsersSettings { get; }
|
||||||
Repository<Monograph> Monographs { get; }
|
Repository<Monograph> Monographs { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,40 +210,4 @@ namespace Notesnook.API.Models
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class SettingItem : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Attachment : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Content : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Note : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Notebook : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Relation : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Reminder : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Setting : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Shortcut : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Tag : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Color : SyncItem { }
|
|
||||||
|
|
||||||
[MessagePack.MessagePackObject]
|
|
||||||
public class Vault : SyncItem { }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,15 +37,19 @@ using Streetwriters.Data.Repositories;
|
|||||||
|
|
||||||
namespace Notesnook.API.Repositories
|
namespace Notesnook.API.Repositories
|
||||||
{
|
{
|
||||||
public class SyncItemsRepository<T> : Repository<SyncItem> where T : SyncItem
|
public class SyncItemsRepository : Repository<SyncItem>
|
||||||
{
|
{
|
||||||
private string collectionName;
|
private readonly string collectionName;
|
||||||
public SyncItemsRepository(IDbContext dbContext, string databaseName, string collectionName) : base(dbContext, databaseName, collectionName)
|
public SyncItemsRepository(IDbContext dbContext, IMongoCollection<SyncItem> collection) : base(dbContext, collection)
|
||||||
{
|
{
|
||||||
this.collectionName = collectionName;
|
this.collectionName = collection.CollectionNamespace.CollectionName;
|
||||||
Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId).Descending(i => i.DateSynced)));
|
#if DEBUG
|
||||||
Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId).Ascending((i) => i.ItemId)));
|
Collection.Indexes.CreateMany([
|
||||||
Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId)));
|
new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending("UserId").Descending("DateSynced")),
|
||||||
|
new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending("UserId").Ascending("ItemId")),
|
||||||
|
new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending("UserId"))
|
||||||
|
]);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly List<string> ALGORITHMS = new List<string> { Algorithms.Default };
|
private readonly List<string> ALGORITHMS = new List<string> { Algorithms.Default };
|
||||||
|
|||||||
+15
-57
@@ -169,42 +169,6 @@ namespace Notesnook.API
|
|||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(CallToAction)))
|
if (!BsonClassMap.IsClassMapRegistered(typeof(CallToAction)))
|
||||||
BsonClassMap.RegisterClassMap<CallToAction>();
|
BsonClassMap.RegisterClassMap<CallToAction>();
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Attachment)))
|
|
||||||
BsonClassMap.RegisterClassMap<Attachment>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Content)))
|
|
||||||
BsonClassMap.RegisterClassMap<Content>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Note)))
|
|
||||||
BsonClassMap.RegisterClassMap<Note>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Notebook)))
|
|
||||||
BsonClassMap.RegisterClassMap<Notebook>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Relation)))
|
|
||||||
BsonClassMap.RegisterClassMap<Relation>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Reminder)))
|
|
||||||
BsonClassMap.RegisterClassMap<Reminder>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Setting)))
|
|
||||||
BsonClassMap.RegisterClassMap<Setting>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(SettingItem)))
|
|
||||||
BsonClassMap.RegisterClassMap<SettingItem>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Shortcut)))
|
|
||||||
BsonClassMap.RegisterClassMap<Shortcut>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Tag)))
|
|
||||||
BsonClassMap.RegisterClassMap<Tag>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Color)))
|
|
||||||
BsonClassMap.RegisterClassMap<Color>();
|
|
||||||
|
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Vault)))
|
|
||||||
BsonClassMap.RegisterClassMap<Vault>();
|
|
||||||
|
|
||||||
services.AddScoped<IDbContext, MongoDbContext>();
|
services.AddScoped<IDbContext, MongoDbContext>();
|
||||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||||
|
|
||||||
@@ -212,18 +176,18 @@ namespace Notesnook.API
|
|||||||
.AddRepository<Monograph>("monographs")
|
.AddRepository<Monograph>("monographs")
|
||||||
.AddRepository<Announcement>("announcements");
|
.AddRepository<Announcement>("announcements");
|
||||||
|
|
||||||
services.AddSyncRepository<SettingItem>("settingsv2")
|
services.AddMongoCollection(Collections.SettingsKey)
|
||||||
.AddSyncRepository<Attachment>("attachments")
|
.AddMongoCollection(Collections.AttachmentsKey)
|
||||||
.AddSyncRepository<Content>("content")
|
.AddMongoCollection(Collections.ContentKey)
|
||||||
.AddSyncRepository<Note>("notes")
|
.AddMongoCollection(Collections.NotesKey)
|
||||||
.AddSyncRepository<Notebook>("notebooks")
|
.AddMongoCollection(Collections.NotebooksKey)
|
||||||
.AddSyncRepository<Relation>("relations")
|
.AddMongoCollection(Collections.RelationsKey)
|
||||||
.AddSyncRepository<Reminder>("reminders")
|
.AddMongoCollection(Collections.RemindersKey)
|
||||||
.AddSyncRepository<Setting>("settings")
|
.AddMongoCollection(Collections.LegacySettingsKey)
|
||||||
.AddSyncRepository<Shortcut>("shortcuts")
|
.AddMongoCollection(Collections.ShortcutsKey)
|
||||||
.AddSyncRepository<Tag>("tags")
|
.AddMongoCollection(Collections.TagsKey)
|
||||||
.AddSyncRepository<Color>("colors")
|
.AddMongoCollection(Collections.ColorsKey)
|
||||||
.AddSyncRepository<Vault>("vaults");
|
.AddMongoCollection(Collections.VaultsKey);
|
||||||
|
|
||||||
services.TryAddTransient<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>();
|
services.TryAddTransient<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>();
|
||||||
services.TryAddTransient<IUserService, UserService>();
|
services.TryAddTransient<IUserService, UserService>();
|
||||||
@@ -310,17 +274,11 @@ namespace Notesnook.API
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ServiceCollectionRepositoryExtensions
|
public static class ServiceCollectionMongoCollectionExtensions
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddRepository<T>(this IServiceCollection services, string collectionName, string database = "notesnook") where T : class
|
public static IServiceCollection AddMongoCollection(this IServiceCollection services, string collectionName, string database = "notesnook")
|
||||||
{
|
{
|
||||||
services.AddScoped((provider) => new Repository<T>(provider.GetRequiredService<IDbContext>(), database, collectionName));
|
services.AddKeyedSingleton(collectionName, (provider, key) => MongoDbContext.GetMongoCollection<SyncItem>(provider.GetService<MongoDB.Driver.IMongoClient>(), database, collectionName));
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection AddSyncRepository<T>(this IServiceCollection services, string collectionName, string database = "notesnook") where T : SyncItem
|
|
||||||
{
|
|
||||||
services.AddScoped((provider) => new SyncItemsRepository<T>(provider.GetRequiredService<IDbContext>(), database, collectionName));
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user