From 7ec6a3be3fdb745e0c1c08d0e611a9edae4b84df Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Mon, 30 Dec 2024 16:16:49 +0800 Subject: [PATCH] Add custom #Script method example --- MyApp/_pages/autoquery/crud.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/MyApp/_pages/autoquery/crud.md b/MyApp/_pages/autoquery/crud.md index a362e11323..3c5ea6bd0a 100644 --- a/MyApp/_pages/autoquery/crud.md +++ b/MyApp/_pages/autoquery/crud.md @@ -288,6 +288,8 @@ public abstract class AuditBase : IAudit } ``` +#### AutoPopulate Examples + We can then create a base Request DTO that all Audit Create Services will implement: ```csharp @@ -300,7 +302,36 @@ We can then create a base Request DTO that all Audit Create Services will implem [AutoPopulate(nameof(IAudit.ModifiedInfo), Eval = "`${userSession.DisplayName} (${userSession.City})`")] public abstract class CreateAuditBase : ICreateDb, IReturn {} ``` -These all call [#Script Methods](https://sharpscript.net/docs/methods) which you can [add/extend yourself](https://sharpscript.net/docs/script-pages#extend). +These all call [#Script Methods](https://sharpscript.net/docs/methods) which you can [add/extend yourself](https://sharpscript.net/docs/script-pages#extend), e.g: + +```csharp +public override void Configure() +{ + // Register custom script methods + ScriptContext.ScriptMethods.Add(new MyScripts()); +} + +// Custom #Script Methods, see: https://sharpscript.net/docs/methods +public class MyScripts : ScriptMethods +{ + public string tenantId(ScriptScopeContext scope) + { + var req = scope.GetRequest(); + var requestDto = req.Dto; + return requestDto is IHasTenantId hasTenantId + ? hasTenantId.TenantId // Explicitly set on Request DTO + : req.AbsoluteUri.RightPart("//").LeftPart('.'); //Fallback to use subdomain + } +} + +// Populate Post.TenantId property with `tenantId` #Script Method +[AutoPopulate(nameof(Post.TenantId), Eval = "tenantId")] +public class CreatePost : ICreatDb, IReturn +{ + public string Name { get; set; } + public string Content { get; set; } +} +``` The `*Info` examples is a superfluous example showing that you can basically evaluate any `#Script` expression. Typically you'd only save User Id or Username