diff --git a/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs b/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs index c686636..3d2d1c1 100644 --- a/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs +++ b/Notesnook.API/Accessors/SyncItemsRepositoryAccessor.cs @@ -17,59 +17,76 @@ You should have received a copy of the Affero GNU General Public License along with this program. If not, see . */ +using Microsoft.Extensions.DependencyInjection; +using MongoDB.Driver; using Notesnook.API.Interfaces; using Notesnook.API.Models; using Notesnook.API.Repositories; +using Streetwriters.Data.Interfaces; using Streetwriters.Data.Repositories; namespace Notesnook.API.Accessors { public class SyncItemsRepositoryAccessor : ISyncItemsRepositoryAccessor { - public SyncItemsRepository Notes { get; } - public SyncItemsRepository Notebooks { get; } - public SyncItemsRepository Shortcuts { get; } - public SyncItemsRepository Relations { get; } - public SyncItemsRepository Reminders { get; } - public SyncItemsRepository Contents { get; } - public SyncItemsRepository LegacySettings { get; } - public SyncItemsRepository Settings { get; } - public SyncItemsRepository Attachments { get; } - public SyncItemsRepository Colors { get; } - public SyncItemsRepository Vaults { get; } - public SyncItemsRepository Tags { get; } + public SyncItemsRepository Notes { get; } + public SyncItemsRepository Notebooks { get; } + public SyncItemsRepository Shortcuts { get; } + public SyncItemsRepository Relations { get; } + public SyncItemsRepository Reminders { get; } + public SyncItemsRepository Contents { get; } + public SyncItemsRepository LegacySettings { get; } + public SyncItemsRepository Settings { get; } + public SyncItemsRepository Attachments { get; } + public SyncItemsRepository Colors { get; } + public SyncItemsRepository Vaults { get; } + public SyncItemsRepository Tags { get; } public Repository UsersSettings { get; } public Repository Monographs { get; } - public SyncItemsRepositoryAccessor(SyncItemsRepository _notes, - SyncItemsRepository _notebooks, - SyncItemsRepository _content, - SyncItemsRepository _legacySettings, - SyncItemsRepository _settings, - SyncItemsRepository _attachments, - SyncItemsRepository _shortcuts, - SyncItemsRepository _relations, - SyncItemsRepository _reminders, - SyncItemsRepository _colors, - SyncItemsRepository _vaults, - SyncItemsRepository _tags, - Repository _usersSettings, - Repository _monographs) + public SyncItemsRepositoryAccessor(IDbContext dbContext, + + [FromKeyedServices(Collections.NotebooksKey)] + IMongoCollection notebooks, + [FromKeyedServices(Collections.NotesKey)] + IMongoCollection notes, + [FromKeyedServices(Collections.ContentKey)] + IMongoCollection content, + [FromKeyedServices(Collections.SettingsKey)] + IMongoCollection settings, + [FromKeyedServices(Collections.LegacySettingsKey)] + IMongoCollection legacySettings, + [FromKeyedServices(Collections.AttachmentsKey)] + IMongoCollection attachments, + [FromKeyedServices(Collections.ShortcutsKey)] + IMongoCollection shortcuts, + [FromKeyedServices(Collections.RemindersKey)] + IMongoCollection reminders, + [FromKeyedServices(Collections.RelationsKey)] + IMongoCollection relations, + [FromKeyedServices(Collections.ColorsKey)] + IMongoCollection colors, + [FromKeyedServices(Collections.VaultsKey)] + IMongoCollection vaults, + [FromKeyedServices(Collections.TagsKey)] + IMongoCollection tags, + + Repository usersSettings, Repository monographs) { - Notebooks = _notebooks; - Notes = _notes; - Contents = _content; - Settings = _settings; - LegacySettings = _legacySettings; - Attachments = _attachments; - UsersSettings = _usersSettings; - Monographs = _monographs; - Shortcuts = _shortcuts; - Reminders = _reminders; - Relations = _relations; - Colors = _colors; - Vaults = _vaults; - Tags = _tags; + UsersSettings = usersSettings; + Monographs = monographs; + Notebooks = new SyncItemsRepository(dbContext, notebooks); + Notes = new SyncItemsRepository(dbContext, notes); + Contents = new SyncItemsRepository(dbContext, content); + Settings = new SyncItemsRepository(dbContext, settings); + LegacySettings = new SyncItemsRepository(dbContext, legacySettings); + Attachments = new SyncItemsRepository(dbContext, attachments); + Shortcuts = new SyncItemsRepository(dbContext, shortcuts); + Reminders = new SyncItemsRepository(dbContext, reminders); + Relations = new SyncItemsRepository(dbContext, relations); + Colors = new SyncItemsRepository(dbContext, colors); + Vaults = new SyncItemsRepository(dbContext, vaults); + Tags = new SyncItemsRepository(dbContext, tags); } } } \ No newline at end of file diff --git a/Notesnook.API/Constants.cs b/Notesnook.API/Constants.cs new file mode 100644 index 0000000..47909e1 --- /dev/null +++ b/Notesnook.API/Constants.cs @@ -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"; + } +} \ No newline at end of file diff --git a/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs b/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs index fbbcb44..f8baab0 100644 --- a/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs +++ b/Notesnook.API/Interfaces/ISyncItemsRepositoryAccessor.cs @@ -26,18 +26,18 @@ namespace Notesnook.API.Interfaces { public interface ISyncItemsRepositoryAccessor { - SyncItemsRepository Notes { get; } - SyncItemsRepository Notebooks { get; } - SyncItemsRepository Shortcuts { get; } - SyncItemsRepository Reminders { get; } - SyncItemsRepository Relations { get; } - SyncItemsRepository Contents { get; } - SyncItemsRepository LegacySettings { get; } - SyncItemsRepository Attachments { get; } - SyncItemsRepository Settings { get; } - SyncItemsRepository Colors { get; } - SyncItemsRepository Vaults { get; } - SyncItemsRepository Tags { get; } + SyncItemsRepository Notes { get; } + SyncItemsRepository Notebooks { get; } + SyncItemsRepository Shortcuts { get; } + SyncItemsRepository Reminders { get; } + SyncItemsRepository Relations { get; } + SyncItemsRepository Contents { get; } + SyncItemsRepository LegacySettings { get; } + SyncItemsRepository Attachments { get; } + SyncItemsRepository Settings { get; } + SyncItemsRepository Colors { get; } + SyncItemsRepository Vaults { get; } + SyncItemsRepository Tags { get; } Repository UsersSettings { get; } Repository Monographs { get; } } diff --git a/Notesnook.API/Models/SyncItem.cs b/Notesnook.API/Models/SyncItem.cs index fac0d1e..899e61c 100644 --- a/Notesnook.API/Models/SyncItem.cs +++ b/Notesnook.API/Models/SyncItem.cs @@ -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 { } } diff --git a/Notesnook.API/Repositories/SyncItemsRepository.cs b/Notesnook.API/Repositories/SyncItemsRepository.cs index a44f719..509d0c5 100644 --- a/Notesnook.API/Repositories/SyncItemsRepository.cs +++ b/Notesnook.API/Repositories/SyncItemsRepository.cs @@ -37,15 +37,19 @@ using Streetwriters.Data.Repositories; namespace Notesnook.API.Repositories { - public class SyncItemsRepository : Repository where T : SyncItem + public class SyncItemsRepository : Repository { - private string collectionName; - public SyncItemsRepository(IDbContext dbContext, string databaseName, string collectionName) : base(dbContext, databaseName, collectionName) + private readonly string collectionName; + public SyncItemsRepository(IDbContext dbContext, IMongoCollection collection) : base(dbContext, collection) { - this.collectionName = collectionName; - Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(i => i.UserId).Descending(i => i.DateSynced))); - Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(i => i.UserId).Ascending((i) => i.ItemId))); - Collection.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(i => i.UserId))); + this.collectionName = collection.CollectionNamespace.CollectionName; +#if DEBUG + Collection.Indexes.CreateMany([ + new CreateIndexModel(Builders.IndexKeys.Ascending("UserId").Descending("DateSynced")), + new CreateIndexModel(Builders.IndexKeys.Ascending("UserId").Ascending("ItemId")), + new CreateIndexModel(Builders.IndexKeys.Ascending("UserId")) + ]); +#endif } private readonly List ALGORITHMS = new List { Algorithms.Default }; diff --git a/Notesnook.API/Startup.cs b/Notesnook.API/Startup.cs index 8932f4b..9ef61e6 100644 --- a/Notesnook.API/Startup.cs +++ b/Notesnook.API/Startup.cs @@ -169,42 +169,6 @@ namespace Notesnook.API if (!BsonClassMap.IsClassMapRegistered(typeof(CallToAction))) BsonClassMap.RegisterClassMap(); - if (!BsonClassMap.IsClassMapRegistered(typeof(Attachment))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Content))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Note))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Notebook))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Relation))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Reminder))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Setting))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(SettingItem))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Shortcut))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Tag))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Color))) - BsonClassMap.RegisterClassMap(); - - if (!BsonClassMap.IsClassMapRegistered(typeof(Vault))) - BsonClassMap.RegisterClassMap(); - services.AddScoped(); services.AddScoped(); @@ -212,18 +176,18 @@ namespace Notesnook.API .AddRepository("monographs") .AddRepository("announcements"); - services.AddSyncRepository("settingsv2") - .AddSyncRepository("attachments") - .AddSyncRepository("content") - .AddSyncRepository("notes") - .AddSyncRepository("notebooks") - .AddSyncRepository("relations") - .AddSyncRepository("reminders") - .AddSyncRepository("settings") - .AddSyncRepository("shortcuts") - .AddSyncRepository("tags") - .AddSyncRepository("colors") - .AddSyncRepository("vaults"); + services.AddMongoCollection(Collections.SettingsKey) + .AddMongoCollection(Collections.AttachmentsKey) + .AddMongoCollection(Collections.ContentKey) + .AddMongoCollection(Collections.NotesKey) + .AddMongoCollection(Collections.NotebooksKey) + .AddMongoCollection(Collections.RelationsKey) + .AddMongoCollection(Collections.RemindersKey) + .AddMongoCollection(Collections.LegacySettingsKey) + .AddMongoCollection(Collections.ShortcutsKey) + .AddMongoCollection(Collections.TagsKey) + .AddMongoCollection(Collections.ColorsKey) + .AddMongoCollection(Collections.VaultsKey); services.TryAddTransient(); services.TryAddTransient(); @@ -310,17 +274,11 @@ namespace Notesnook.API } } - public static class ServiceCollectionRepositoryExtensions + public static class ServiceCollectionMongoCollectionExtensions { - public static IServiceCollection AddRepository(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(provider.GetRequiredService(), database, collectionName)); - return services; - } - - public static IServiceCollection AddSyncRepository(this IServiceCollection services, string collectionName, string database = "notesnook") where T : SyncItem - { - services.AddScoped((provider) => new SyncItemsRepository(provider.GetRequiredService(), database, collectionName)); + services.AddKeyedSingleton(collectionName, (provider, key) => MongoDbContext.GetMongoCollection(provider.GetService(), database, collectionName)); return services; } }