Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring result #93

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/Templates/Pages/.create.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
37 changes: 24 additions & 13 deletions src/Templates/Pages/.edit.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
result.Map(data =>
{
model = Mapper.Map<Update{itemname}Command>(itemDto);
_breadcrumbItems.Add(new BreadcrumbItem(itemDto.Name, href: $"/pages/{nameofPlural}/edit/{Id}"));
}
model = Mapper.Map<Update{itemname}Command>(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()
Expand All @@ -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
{
Expand Down
42 changes: 22 additions & 20 deletions src/Templates/Pages/.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte[]>(
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;
},
errors =>
{
Snackbar.Add($"{errors}", MudBlazor.Severity.Error);
return Task.FromResult(Array.Empty<byte>());
});
_exporting = false;
}
private async Task OnImportData(IBrowserFile file)
Expand All @@ -369,18 +372,17 @@
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;
}

Expand Down
18 changes: 14 additions & 4 deletions src/Templates/Pages/.view.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 12 additions & 10 deletions src/Templates/Pages/Components/.formdialog.razor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
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
return data;
}, errors =>
{
Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error);
}
Snackbar.Add(errors, MudBlazor.Severity.Error);
return -1;
});
}
finally
{
Expand All @@ -61,16 +62,17 @@
if (!_form!.IsValid)
return;
var result = await Mediator.Send(model);
if (result.Succeeded)
await result.MatchAsync(async data =>
{
Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info);
await Task.Delay(300);
model = new AddEdit{itemname}Command() { };
}
else
return data;
}, errors =>
{
Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error);
}
Snackbar.Add(errors, MudBlazor.Severity.Error);
return Task.FromResult(-1);
});
}
finally
{
Expand Down
8 changes: 4 additions & 4 deletions src/Templates/Queries/GetById/.cs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ using {selectns}.{nameofPlural}.Specifications;

namespace {namespace};

public class Get{itemname}ByIdQuery : ICacheableRequest<{itemname}Dto?>
public class Get{itemname}ByIdQuery : ICacheableRequest<Result<{itemname}Dto>>
{
public required int Id { get; set; }
public string CacheKey => {itemname}CacheKey.GetByIdCacheKey($"{Id}");
public MemoryCacheEntryOptions? Options => {itemname}CacheKey.MemoryCacheEntryOptions;
}

public class Get{itemname}ByIdQueryHandler :
IRequestHandler<Get{itemname}ByIdQuery, {itemname}Dto?>
IRequestHandler<Get{itemname}ByIdQuery, Result<{itemname}Dto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;
Expand All @@ -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<Result<{itemname}Dto>> 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<ContactDto>.SuccessAsync(data);
}
}
Loading