sync: remove item type specific model classes & simplify sync repository usage

This commit is contained in:
Abdullah Atta
2024-06-07 11:10:43 +05:00
parent 90b9012c32
commit ad4e43e879
6 changed files with 113 additions and 152 deletions

View File

@@ -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/>.
*/
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<Note> Notes { get; }
public SyncItemsRepository<Notebook> Notebooks { get; }
public SyncItemsRepository<Shortcut> Shortcuts { get; }
public SyncItemsRepository<Relation> Relations { get; }
public SyncItemsRepository<Reminder> Reminders { get; }
public SyncItemsRepository<Content> Contents { get; }
public SyncItemsRepository<Setting> LegacySettings { get; }
public SyncItemsRepository<SettingItem> Settings { get; }
public SyncItemsRepository<Attachment> Attachments { get; }
public SyncItemsRepository<Color> Colors { get; }
public SyncItemsRepository<Vault> Vaults { get; }
public SyncItemsRepository<Tag> 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<UserSettings> UsersSettings { get; }
public Repository<Monograph> Monographs { get; }
public SyncItemsRepositoryAccessor(SyncItemsRepository<Note> _notes,
SyncItemsRepository<Notebook> _notebooks,
SyncItemsRepository<Content> _content,
SyncItemsRepository<Setting> _legacySettings,
SyncItemsRepository<SettingItem> _settings,
SyncItemsRepository<Attachment> _attachments,
SyncItemsRepository<Shortcut> _shortcuts,
SyncItemsRepository<Relation> _relations,
SyncItemsRepository<Reminder> _reminders,
SyncItemsRepository<Color> _colors,
SyncItemsRepository<Vault> _vaults,
SyncItemsRepository<Tag> _tags,
Repository<UserSettings> _usersSettings,
Repository<Monograph> _monographs)
public SyncItemsRepositoryAccessor(IDbContext dbContext,
[FromKeyedServices(Collections.NotebooksKey)]
IMongoCollection<SyncItem> notebooks,
[FromKeyedServices(Collections.NotesKey)]
IMongoCollection<SyncItem> notes,
[FromKeyedServices(Collections.ContentKey)]
IMongoCollection<SyncItem> content,
[FromKeyedServices(Collections.SettingsKey)]
IMongoCollection<SyncItem> settings,
[FromKeyedServices(Collections.LegacySettingsKey)]
IMongoCollection<SyncItem> legacySettings,
[FromKeyedServices(Collections.AttachmentsKey)]
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;
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);
}
}
}

View File

@@ -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";
}
}

View File

@@ -26,18 +26,18 @@ namespace Notesnook.API.Interfaces
{
public interface ISyncItemsRepositoryAccessor
{
SyncItemsRepository<Note> Notes { get; }
SyncItemsRepository<Notebook> Notebooks { get; }
SyncItemsRepository<Shortcut> Shortcuts { get; }
SyncItemsRepository<Reminder> Reminders { get; }
SyncItemsRepository<Relation> Relations { get; }
SyncItemsRepository<Content> Contents { get; }
SyncItemsRepository<Setting> LegacySettings { get; }
SyncItemsRepository<Attachment> Attachments { get; }
SyncItemsRepository<SettingItem> Settings { get; }
SyncItemsRepository<Color> Colors { get; }
SyncItemsRepository<Vault> Vaults { get; }
SyncItemsRepository<Tag> 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<UserSettings> UsersSettings { get; }
Repository<Monograph> Monographs { get; }
}

View File

@@ -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 { }
}

View File

@@ -37,15 +37,19 @@ using Streetwriters.Data.Repositories;
namespace Notesnook.API.Repositories
{
public class SyncItemsRepository<T> : Repository<SyncItem> where T : SyncItem
public class SyncItemsRepository : Repository<SyncItem>
{
private string collectionName;
public SyncItemsRepository(IDbContext dbContext, string databaseName, string collectionName) : base(dbContext, databaseName, collectionName)
private readonly string collectionName;
public SyncItemsRepository(IDbContext dbContext, IMongoCollection<SyncItem> collection) : base(dbContext, collection)
{
this.collectionName = collectionName;
Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId).Descending(i => i.DateSynced)));
Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId).Ascending((i) => i.ItemId)));
Collection.Indexes.CreateOne(new CreateIndexModel<SyncItem>(Builders<SyncItem>.IndexKeys.Ascending(i => i.UserId)));
this.collectionName = collection.CollectionNamespace.CollectionName;
#if DEBUG
Collection.Indexes.CreateMany([
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 };

View File

@@ -169,42 +169,6 @@ namespace Notesnook.API
if (!BsonClassMap.IsClassMapRegistered(typeof(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<IUnitOfWork, UnitOfWork>();
@@ -212,18 +176,18 @@ namespace Notesnook.API
.AddRepository<Monograph>("monographs")
.AddRepository<Announcement>("announcements");
services.AddSyncRepository<SettingItem>("settingsv2")
.AddSyncRepository<Attachment>("attachments")
.AddSyncRepository<Content>("content")
.AddSyncRepository<Note>("notes")
.AddSyncRepository<Notebook>("notebooks")
.AddSyncRepository<Relation>("relations")
.AddSyncRepository<Reminder>("reminders")
.AddSyncRepository<Setting>("settings")
.AddSyncRepository<Shortcut>("shortcuts")
.AddSyncRepository<Tag>("tags")
.AddSyncRepository<Color>("colors")
.AddSyncRepository<Vault>("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<ISyncItemsRepositoryAccessor, SyncItemsRepositoryAccessor>();
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));
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));
services.AddKeyedSingleton(collectionName, (provider, key) => MongoDbContext.GetMongoCollection<SyncItem>(provider.GetService<MongoDB.Driver.IMongoClient>(), database, collectionName));
return services;
}
}