From 4e7f72bf6b8f1172bd67dfb3ab815c96e8a1dd09 Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 4 Oct 2024 18:07:38 +0800 Subject: [PATCH 1/4] refactoring result --- src/Templates/Pages/.create.razor.txt | 21 ++++++++------ src/Templates/Pages/.edit.razor.txt | 37 +++++++++++++++--------- src/Templates/Pages/.razor.txt | 41 ++++++++++++++------------- src/Templates/Pages/.view.razor.txt | 18 +++++++++--- src/Templates/Queries/GetById/.cs.txt | 8 +++--- 5 files changed, 76 insertions(+), 49 deletions(-) diff --git a/src/Templates/Pages/.create.razor.txt b/src/Templates/Pages/.create.razor.txt index 0065046..e3d6b24 100644 --- a/src/Templates/Pages/.create.razor.txt +++ b/src/Templates/Pages/.create.razor.txt @@ -54,15 +54,18 @@ if (!_form!.IsValid) return; var result = await Mediator.Send(model); - if (result.Succeeded) - { - Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); - Navigation.NavigateTo($"/pages/{nameofPlural}"); - } - else - { - Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error); - } + result.Match( + data=> + { + Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); + Navigation.NavigateTo($"/pages/{nameofPlural}"); + return data; + } + errors=> + { + Snackbar.Add(errors, MudBlazor.Severity.Error); + return -1; + }); } finally { diff --git a/src/Templates/Pages/.edit.razor.txt b/src/Templates/Pages/.edit.razor.txt index 90a0204..127084c 100644 --- a/src/Templates/Pages/.edit.razor.txt +++ b/src/Templates/Pages/.edit.razor.txt @@ -47,12 +47,20 @@ protected override async Task OnInitializedAsync() { Title = L["Edit {itemname}"]; - var itemDto = await Mediator.Send(new Get{itemname}ByIdQuery() { Id = Id }); - if (itemDto is not null) + var result = await Mediator.Send(new Get{itemname}ByIdQuery() { Id = Id }); + iresult.Map(data => { - model = Mapper.Map(itemDto); - _breadcrumbItems.Add(new BreadcrumbItem(itemDto.Name, href: $"/pages/{nameofPlural}/edit/{Id}")); - } + model = Mapper.Map(data); + return data; + }).Match(data => + { + _breadcrumbItems.Add(new BreadcrumbItem(data.Name, href: $"/pages/{nameofPlural}/edit/{Id}")); + return data; + }, errors => + { + Snackbar.Add($"{errors}", Severity.Error); + return null; + }); } async Task Submit() @@ -64,14 +72,17 @@ if (!_form!.IsValid) return; var result = await Mediator.Send(model); - if (result.Succeeded) - { - Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); - } - else - { - Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error); - } + result.Match( + data=> + { + Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); + return data; + }, + errors=> + { + Snackbar.Add(errors, MudBlazor.Severity.Error); + return 0; + }); } finally { diff --git a/src/Templates/Pages/.razor.txt b/src/Templates/Pages/.razor.txt index 2d9fd70..aad6d2b 100644 --- a/src/Templates/Pages/.razor.txt +++ b/src/Templates/Pages/.razor.txt @@ -351,15 +351,18 @@ SortDirection = (_table.SortDefinitions.Values.FirstOrDefault()?.Descending ?? true) ? SortDirection.Descending.ToString() : SortDirection.Ascending.ToString() }; var result = await Mediator.Send(request); - if (result.Succeeded) - { - var downloadresult = await BlazorDownloadFileService.DownloadFile($"{L["{nameofPlural}"]}.xlsx", result.Data, contentType:"application/octet-stream"); - Snackbar.Add($"{ConstantString.ExportSuccess}", MudBlazor.Severity.Info); - } - else - { - Snackbar.Add($"{result.ErrorMessage}", MudBlazor.Severity.Error); - } + await result.MatchAsync( + async data => + { + var downloadresult = await BlazorDownloadFileService.DownloadFile($"{L["{nameofPlural}"]}.xlsx", result.Data, contentType:"application/octet-stream"); + Snackbar.Add($"{ConstantString.ExportSuccess}", MudBlazor.Severity.Info); + return data; + } + else + { + Snackbar.Add($"{errors}", MudBlazor.Severity.Error); + return Task.FromResult(Array.Empty()); + }); _exporting = false; } private async Task OnImportData(IBrowserFile file) @@ -369,18 +372,18 @@ await file.OpenReadStream().CopyToAsync(stream); var command = new Import{nameofPlural}Command(file.Name, stream.ToArray()); var result = await Mediator.Send(command); - if (result.Succeeded) - { - await _table.ReloadServerData(); - Snackbar.Add($"{ConstantString.ImportSuccess}", MudBlazor.Severity.Info); - } - else - { - foreach (var msg in result.Errors) + await result.MatchAsync( + async data => { - Snackbar.Add($"{msg}", MudBlazor.Severity.Error); + await _table.ReloadServerData(); + Snackbar.Add($"{ConstantString.ImportSuccess}", MudBlazor.Severity.Info); + return data; } - } + errors => + { + Snackbar.Add($"{errors}", MudBlazor.Severity.Error); + return Task.FromResult(0); + }); _uploading = false; } diff --git a/src/Templates/Pages/.view.razor.txt b/src/Templates/Pages/.view.razor.txt index efca3c7..453123b 100644 --- a/src/Templates/Pages/.view.razor.txt +++ b/src/Templates/Pages/.view.razor.txt @@ -40,11 +40,21 @@ protected override async Task OnInitializedAsync() { Title = L["{itemname}"]; - model = await Mediator.Send(new Get{itemname}ByIdQuery() { Id = Id }); - if (model is not null) + var result = await Mediator.Send(new Get{itemname}ByIdQuery() { Id = Id }); + result.Map(data => { - _breadcrumbItems.Add(new BreadcrumbItem(model.Name, href:null, disabled: true)); - } + model = data; + return data; + }).Match(data => + { + _breadcrumbItems.Add(new BreadcrumbItem(data.Name, null, disabled: true)); + return data; + + }, errors => + { + Snackbar.Add(errors, MudBlazor.Severity.Error); + return null; + }); } void GoEdit() diff --git a/src/Templates/Queries/GetById/.cs.txt b/src/Templates/Queries/GetById/.cs.txt index 7f006a2..8f98e66 100644 --- a/src/Templates/Queries/GetById/.cs.txt +++ b/src/Templates/Queries/GetById/.cs.txt @@ -7,7 +7,7 @@ using {selectns}.{nameofPlural}.Specifications; namespace {namespace}; -public class Get{itemname}ByIdQuery : ICacheableRequest<{itemname}Dto?> +public class Get{itemname}ByIdQuery : ICacheableRequest> { public required int Id { get; set; } public string CacheKey => {itemname}CacheKey.GetByIdCacheKey($"{Id}"); @@ -15,7 +15,7 @@ public class Get{itemname}ByIdQuery : ICacheableRequest<{itemname}Dto?> } public class Get{itemname}ByIdQueryHandler : - IRequestHandler + IRequestHandler> { private readonly IApplicationDbContext _context; private readonly IMapper _mapper; @@ -32,11 +32,11 @@ public class Get{itemname}ByIdQueryHandler : _localizer = localizer; } - public async Task<{itemname}Dto?> Handle(Get{itemname}ByIdQuery request, CancellationToken cancellationToken) + public async Task> Handle(Get{itemname}ByIdQuery request, CancellationToken cancellationToken) { var data = await _context.{nameofPlural}.ApplySpecification(new {itemname}ByIdSpecification(request.Id)) .ProjectTo<{itemname}Dto>(_mapper.ConfigurationProvider) .FirstAsync(cancellationToken); - return data; + return await Result.SuccessAsync(data); } } From bff387e2429c42d014187c3d9475e18c12cb270e Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 4 Oct 2024 18:19:17 +0800 Subject: [PATCH 2/4] fixed --- src/Templates/Pages/.create.razor.txt | 2 +- src/Templates/Pages/.razor.txt | 6 +++--- .../Pages/Components/.formdialog.razor.txt | 18 ++++++++---------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Templates/Pages/.create.razor.txt b/src/Templates/Pages/.create.razor.txt index e3d6b24..126be4a 100644 --- a/src/Templates/Pages/.create.razor.txt +++ b/src/Templates/Pages/.create.razor.txt @@ -60,7 +60,7 @@ Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); Navigation.NavigateTo($"/pages/{nameofPlural}"); return data; - } + }, errors=> { Snackbar.Add(errors, MudBlazor.Severity.Error); diff --git a/src/Templates/Pages/.razor.txt b/src/Templates/Pages/.razor.txt index aad6d2b..e1eae02 100644 --- a/src/Templates/Pages/.razor.txt +++ b/src/Templates/Pages/.razor.txt @@ -357,8 +357,8 @@ var downloadresult = await BlazorDownloadFileService.DownloadFile($"{L["{nameofPlural}"]}.xlsx", result.Data, contentType:"application/octet-stream"); Snackbar.Add($"{ConstantString.ExportSuccess}", MudBlazor.Severity.Info); return data; - } - else + }, + errors => { Snackbar.Add($"{errors}", MudBlazor.Severity.Error); return Task.FromResult(Array.Empty()); @@ -378,7 +378,7 @@ await _table.ReloadServerData(); Snackbar.Add($"{ConstantString.ImportSuccess}", MudBlazor.Severity.Info); return data; - } + }, errors => { Snackbar.Add($"{errors}", MudBlazor.Severity.Error); diff --git a/src/Templates/Pages/Components/.formdialog.razor.txt b/src/Templates/Pages/Components/.formdialog.razor.txt index 03d5c44..43e56cb 100644 --- a/src/Templates/Pages/Components/.formdialog.razor.txt +++ b/src/Templates/Pages/Components/.formdialog.razor.txt @@ -37,15 +37,14 @@ if (!_form!.IsValid) return; var result = await Mediator.Send(model); - if (result.Succeeded) + result.Match(data => { MudDialog.Close(DialogResult.Ok(true)); Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); - } - else + }, errors => { - Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error); - } + Snackbar.Add(errors, MudBlazor.Severity.Error); + }); } finally { @@ -61,16 +60,15 @@ if (!_form!.IsValid) return; var result = await Mediator.Send(model); - if (result.Succeeded) + result.Match(data => { Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); await Task.Delay(300); model = new AddEdit{itemname}Command() { }; - } - else + }, errors => { - Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error); - } + Snackbar.Add(errors, MudBlazor.Severity.Error); + }); } finally { From fa119fa67e050938e868da8ff2ed848ecb4627f1 Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 4 Oct 2024 18:26:41 +0800 Subject: [PATCH 3/4] commit --- src/Templates/Pages/.edit.razor.txt | 2 +- src/Templates/Pages/Components/.formdialog.razor.txt | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Templates/Pages/.edit.razor.txt b/src/Templates/Pages/.edit.razor.txt index 127084c..2c22cbd 100644 --- a/src/Templates/Pages/.edit.razor.txt +++ b/src/Templates/Pages/.edit.razor.txt @@ -48,7 +48,7 @@ { Title = L["Edit {itemname}"]; var result = await Mediator.Send(new Get{itemname}ByIdQuery() { Id = Id }); - iresult.Map(data => + result.Map(data => { model = Mapper.Map(data); return data; diff --git a/src/Templates/Pages/Components/.formdialog.razor.txt b/src/Templates/Pages/Components/.formdialog.razor.txt index 43e56cb..22e55d2 100644 --- a/src/Templates/Pages/Components/.formdialog.razor.txt +++ b/src/Templates/Pages/Components/.formdialog.razor.txt @@ -41,9 +41,11 @@ { MudDialog.Close(DialogResult.Ok(true)); Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); + return data; }, errors => { Snackbar.Add(errors, MudBlazor.Severity.Error); + return -1; }); } finally @@ -60,14 +62,16 @@ if (!_form!.IsValid) return; var result = await Mediator.Send(model); - result.Match(data => + result.MatchAsync(data => { Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); await Task.Delay(300); model = new AddEdit{itemname}Command() { }; + return data; }, errors => { Snackbar.Add(errors, MudBlazor.Severity.Error); + return Task.FromResult(-1); }); } finally From 3291920a4f400a5ba1045b8e002467279c1ffdad Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 4 Oct 2024 21:14:21 +0800 Subject: [PATCH 4/4] commit --- src/Templates/Pages/.razor.txt | 3 +-- src/Templates/Pages/Components/.formdialog.razor.txt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Templates/Pages/.razor.txt b/src/Templates/Pages/.razor.txt index e1eae02..40f69ab 100644 --- a/src/Templates/Pages/.razor.txt +++ b/src/Templates/Pages/.razor.txt @@ -378,8 +378,7 @@ await _table.ReloadServerData(); Snackbar.Add($"{ConstantString.ImportSuccess}", MudBlazor.Severity.Info); return data; - }, - errors => + }, errors => { Snackbar.Add($"{errors}", MudBlazor.Severity.Error); return Task.FromResult(0); diff --git a/src/Templates/Pages/Components/.formdialog.razor.txt b/src/Templates/Pages/Components/.formdialog.razor.txt index 22e55d2..70ddcc4 100644 --- a/src/Templates/Pages/Components/.formdialog.razor.txt +++ b/src/Templates/Pages/Components/.formdialog.razor.txt @@ -62,7 +62,7 @@ if (!_form!.IsValid) return; var result = await Mediator.Send(model); - result.MatchAsync(data => + await result.MatchAsync(async data => { Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info); await Task.Delay(300);