diff --git a/zero.Sqlite/Operations/DbOperations.Delete.cs b/zero.Sqlite/Operations/DbOperations.Delete.cs index 3f443709..1bec7304 100644 --- a/zero.Sqlite/Operations/DbOperations.Delete.cs +++ b/zero.Sqlite/Operations/DbOperations.Delete.cs @@ -34,6 +34,7 @@ public partial class DbOperations : IDbOperations } Logger.LogInformation("{id} ({type}) successfully deleted", typeof(T), model.Id); + await EntityModifiedHandler.Deleted(model); return Result.Success(); } diff --git a/zero.Sqlite/Operations/DbOperations.Write.cs b/zero.Sqlite/Operations/DbOperations.Write.cs index bf1190a6..337eaa96 100644 --- a/zero.Sqlite/Operations/DbOperations.Write.cs +++ b/zero.Sqlite/Operations/DbOperations.Write.cs @@ -93,6 +93,8 @@ public partial class DbOperations : IDbOperations Logger.LogInformation(action + " {id}", model.Id); } + await EntityModifiedHandler.Saved(model, update); + return Result.Success(model); } @@ -114,5 +116,9 @@ public partial class DbOperations : IDbOperations } } await Db.UpdateAllAsync(items); + foreach (T item in items) + { + await EntityModifiedHandler.Updated(item); + } } } \ No newline at end of file diff --git a/zero.Sqlite/Operations/DbOperations.cs b/zero.Sqlite/Operations/DbOperations.cs index c1426869..5d99fdf8 100644 --- a/zero.Sqlite/Operations/DbOperations.cs +++ b/zero.Sqlite/Operations/DbOperations.cs @@ -8,6 +8,7 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using ServiceStack.OrmLite; +using zero.Communication; using zero.Context; using zero.Models; using zero.Utils; @@ -27,14 +28,17 @@ public partial class DbOperations : IDbOperations protected ILogger Logger { get; private set; } + protected IEntityModifiedHandler EntityModifiedHandler { get; private set; } + - public DbOperations(StoreContext context, IDbConnection db, ILogger logger) + public DbOperations(StoreContext context, IDbConnection db, ILogger logger, IHandlerHolder handler) { Context = context.Context; Services = context.Services; Flavors = context.Options.For(); Db = db; Logger = logger; + EntityModifiedHandler = handler.Get(); } diff --git a/zero.Sqlite/Operations/IEntityModifiedHandler.cs b/zero.Sqlite/Operations/IEntityModifiedHandler.cs new file mode 100644 index 00000000..bbfb1d28 --- /dev/null +++ b/zero.Sqlite/Operations/IEntityModifiedHandler.cs @@ -0,0 +1,21 @@ +using System.Threading.Tasks; +using zero.Communication; +using zero.Models; + +namespace zero.Sqlite; + +public interface IEntityModifiedHandler : IHandler +{ + Task Saved(T model, bool update) where T : ZeroIdEntity, new() => update ? Updated(model) : Created(model); + Task Created(T model) where T : ZeroIdEntity, new(); + Task Updated(T model) where T : ZeroIdEntity, new(); + Task Deleted(T model) where T : ZeroIdEntity, new(); +} + + +public class EmptyEntityModifiedHandler : IEntityModifiedHandler +{ + public Task Created(T model) where T : ZeroIdEntity, new() => Task.CompletedTask; + public Task Updated(T model) where T : ZeroIdEntity, new() => Task.CompletedTask; + public Task Deleted(T model) where T : ZeroIdEntity, new() => Task.CompletedTask; +} \ No newline at end of file diff --git a/zero.Sqlite/ZeroSqliteModule.cs b/zero.Sqlite/ZeroSqliteModule.cs index ca22f561..be9939b1 100644 --- a/zero.Sqlite/ZeroSqliteModule.cs +++ b/zero.Sqlite/ZeroSqliteModule.cs @@ -27,6 +27,7 @@ internal class ZeroSqliteModule : ZeroModule services.AddScoped(CreateDbConnection); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddOptions(); services.AddOptions().Bind(configuration.GetSection("Zero:Sqlite")); services.ConfigureOptions();