From b6ba37d2ca044bb6e252fa76d2bc3b7e320c23f9 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Mon, 4 Oct 2021 15:53:50 +0200 Subject: [PATCH] interceptor updates --- zero.Core/Collections/CollectionBase.cs | 44 +++++++------------ .../CollectionInterceptorParameters.cs | 10 +++++ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/zero.Core/Collections/CollectionBase.cs b/zero.Core/Collections/CollectionBase.cs index b167ee84..7414acdd 100644 --- a/zero.Core/Collections/CollectionBase.cs +++ b/zero.Core/Collections/CollectionBase.cs @@ -235,7 +235,7 @@ namespace zero.Core.Collections PreSave?.Invoke(model); - bool isCreate = false; + bool isUpdate = model.Id.IsNullOrEmpty() ? false : await Session.Advanced.ExistsAsync(model.Id); // set IDs model.AutoSetIds(); @@ -244,10 +244,8 @@ namespace zero.Core.Collections string userId = Context.BackofficeUser.FindFirstValue(Constants.Auth.Claims.UserId); // set default properties - if (model.Id.IsNullOrEmpty()) + if (!isUpdate) { - isCreate = true; - model.CreatedDate = DateTimeOffset.Now; model.CreatedById = userId; model.LanguageId ??= "languages.1-A"; // TODO correct language id @@ -260,19 +258,6 @@ namespace zero.Core.Collections model.CreatedById ??= userId; model.Hash ??= IdGenerator.Classic(); - // run interceptors - if (isCreate) - { - return await Create(model); - } - - return await Update(model); - } - - - /// - async Task> Create(T model) - { // run validator if (Validator != null) { @@ -284,6 +269,18 @@ namespace zero.Core.Collections } } + if (!isUpdate) + { + return await Create(model); + } + + return await Update(model); + } + + + /// + async Task> Create(T model) + { // run interceptor var instruction = CreateInstruction.CreateParameters>("create", args => args.Model = model); await instruction.HandleBefore(x => x.Creating(instruction.Parameters)); @@ -298,6 +295,7 @@ namespace zero.Core.Collections { args.Model = model; args.Id = model.Id; + args.IsUpdate = false; }); await instruction2.HandleBefore(x => x.Saving(instruction2.Parameters)); @@ -320,17 +318,6 @@ namespace zero.Core.Collections /// async Task> Update(T model) { - // run validator - if (Validator != null) - { - ValidationResult validation = await Validator.ValidateAsync(model); - - if (!validation.IsValid) - { - return EntityResult.Fail(validation); - } - } - // run interceptor var instruction = CreateInstruction.UpdateParameters>("update", args => { @@ -349,6 +336,7 @@ namespace zero.Core.Collections { args.Model = model; args.Id = model.Id; + args.IsUpdate = true; }); await instruction2.HandleBefore(x => x.Saving(instruction2.Parameters)); diff --git a/zero.Core/Collections/Interceptors/CollectionInterceptorParameters.cs b/zero.Core/Collections/Interceptors/CollectionInterceptorParameters.cs index 37b7c40b..986dc03b 100644 --- a/zero.Core/Collections/Interceptors/CollectionInterceptorParameters.cs +++ b/zero.Core/Collections/Interceptors/CollectionInterceptorParameters.cs @@ -77,6 +77,16 @@ namespace zero.Core.Collections /// The Id of the model which is saving (empty when creating) /// public string Id { get; set; } + + /// + /// Whether this instruction is an update + /// + public bool IsUpdate { get; set; } + + /// + /// Whether this instruction is create + /// + public bool IsCreate => !IsUpdate; }