Skip to content

Commit f06ba67

Browse files
authored
Merge pull request #83 from neozhu/editpagetemplate
.create.razor.txt .edit.razor.txt
2 parents ecb91a6 + 41d3bde commit f06ba67

File tree

4 files changed

+169
-2
lines changed

4 files changed

+169
-2
lines changed

src/CleanArchitectureCodeGenerator.csproj

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@
111111
<DependentUpon>source.extension.vsixmanifest</DependentUpon>
112112
</Compile>
113113
<Compile Include="TemplateMap.cs" />
114+
<Content Include="Templates\Pages\.create.razor.txt">
115+
<IncludeInVSIX>true</IncludeInVSIX>
116+
</Content>
117+
<Content Include="Templates\Pages\.edit.razor.txt">
118+
<IncludeInVSIX>true</IncludeInVSIX>
119+
</Content>
114120
<Content Include="Templates\PermissionSet\.cs.txt">
115121
<IncludeInVSIX>true</IncludeInVSIX>
116122
</Content>
@@ -282,21 +288,27 @@
282288
</BootstrapperPackage>
283289
</ItemGroup>
284290
<ItemGroup>
291+
<PackageReference Include="Microsoft.IO.Redist">
292+
<Version>6.0.1</Version>
293+
</PackageReference>
285294
<PackageReference Include="Microsoft.VisualStudio.SDK" ExcludeAssets="runtime">
286-
<Version>17.8.37222</Version>
295+
<Version>17.11.40262</Version>
287296
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
288297
</PackageReference>
289298
<PackageReference Include="Microsoft.VisualStudio.Validation">
290299
<Version>17.8.8</Version>
291300
</PackageReference>
292301
<PackageReference Include="Microsoft.VSSDK.BuildTools">
293-
<Version>17.9.3168</Version>
302+
<Version>17.11.435</Version>
294303
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
295304
<PrivateAssets>all</PrivateAssets>
296305
</PackageReference>
297306
<PackageReference Include="System.ComponentModel.Composition">
298307
<Version>8.0.0</Version>
299308
</PackageReference>
309+
<PackageReference Include="System.Text.Json">
310+
<Version>8.0.4</Version>
311+
</PackageReference>
300312
</ItemGroup>
301313
<ItemGroup />
302314
<PropertyGroup>

src/CodeGeneratorPackage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ private void ExecuteAsync(object sender, EventArgs e)
142142

143143
var pages = new List<string>()
144144
{
145+
$"Pages/{nameofPlural}/Create{name}.razor",
146+
$"Pages/{nameofPlural}/Edit{name}.razor",
145147
$"Pages/{nameofPlural}/{nameofPlural}.razor",
146148
$"Pages/{nameofPlural}/Components/{name}FormDialog.razor",
147149
$"Pages/{nameofPlural}/Components/{nameofPlural}AdvancedSearchComponent.razor"
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
@page "/pages/{nameofPlural}/create"
2+
@using CleanArchitecture.Blazor.Application.Features.{nameofPlural}.Commands.Create
3+
4+
@inherits MudComponentBase
5+
@inject IValidationService Validator
6+
@inject IStringLocalizer<{nameofPlural}> L
7+
8+
<PageTitle>@Title</PageTitle>
9+
<Breadcrumbs OnSaveButtonClick="Submit" Saving="_saving" BreadcrumbItems="_breadcrumbItems"></Breadcrumbs>
10+
<MudContainer Class="p-4 mt-3" MaxWidth="MaxWidth.Small">
11+
<MudCard>
12+
<MudCardHeader>
13+
<CardHeaderContent>
14+
<MudText Typo="Typo.h6">@Title</MudText>
15+
</CardHeaderContent>
16+
</MudCardHeader>
17+
<MudCardContent>
18+
<MudForm Model="@model" @ref="@_form" Validation="@(Validator.ValidateValue(model))">
19+
<MudGrid>
20+
{mudFormFieldDefinition}
21+
</MudGrid>
22+
</MudForm>
23+
<MudCardActions Class="d-flex justify-end gap-2">
24+
<MudLoadingButton Color="Color.Primary" DropShadow="false" Loading="@_saving" Variant="Variant.Outlined" OnClick="Submit">@ConstantString.Save</MudLoadingButton>
25+
</MudCardActions>
26+
</MudCardContent>
27+
</MudCard>
28+
</MudContainer>
29+
30+
31+
@code {
32+
public string? Title { get; private set; }
33+
MudForm? _form;
34+
private bool _saving = false;
35+
private bool _savingnew = false;
36+
private List<BreadcrumbItem> _breadcrumbItems = new List<BreadcrumbItem>
37+
{
38+
new BreadcrumbItem("Home", href: "/"),
39+
new BreadcrumbItem("{nameofPlural}", href: "/pages/{nameofPlural}"),
40+
new BreadcrumbItem("Create {itemname}", href:null, disabled:true)
41+
};
42+
private Create{itemname}Command model = new();
43+
protected override Task OnInitializedAsync()
44+
{
45+
Title = L["Create {itemname}"];
46+
return Task.CompletedTask;
47+
}
48+
async Task Submit()
49+
{
50+
try
51+
{
52+
_saving = true;
53+
await _form!.Validate().ConfigureAwait(false);
54+
if (!_form!.IsValid)
55+
return;
56+
var result = await Mediator.Send(model);
57+
if (result.Succeeded)
58+
{
59+
Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info);
60+
Navigation.NavigateTo($"/pages/{nameofPlural}");
61+
}
62+
else
63+
{
64+
Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error);
65+
}
66+
}
67+
finally
68+
{
69+
_saving = false;
70+
}
71+
}
72+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
@page "/pages/{nameofPlural}/edit/{id:int}"
2+
@using CleanArchitecture.Blazor.Application.Features.{nameofPlural}.Commands.Update
3+
@using CleanArchitecture.Blazor.Application.Features.{nameofPlural}.Queries.GetById
4+
@inherits MudComponentBase
5+
@inject IValidationService Validator
6+
@inject IStringLocalizer<{nameofPlural}> L
7+
8+
<PageTitle>@Title</PageTitle>
9+
<Breadcrumbs OnSaveButtonClick="Submit" Saving="_saving" BreadcrumbItems="_breadcrumbItems"></Breadcrumbs>
10+
<MudContainer Class="p-4 mt-3" MaxWidth="MaxWidth.Small">
11+
@if (model != null)
12+
{
13+
<MudCard>
14+
<MudCardHeader>
15+
<CardHeaderContent>
16+
<MudText Typo="Typo.h6">@Title</MudText>
17+
</CardHeaderContent>
18+
</MudCardHeader>
19+
<MudCardContent>
20+
<MudForm Model="@model" @ref="@_form" Validation="@(Validator.ValidateValue(model))">
21+
<MudGrid>
22+
{mudFormFieldDefinition}
23+
</MudGrid>
24+
</MudForm>
25+
<MudCardActions Class="d-flex justify-end gap-2">
26+
<MudLoadingButton Color="Color.Primary" DropShadow="false" Loading="@_saving" Variant="Variant.Outlined" OnClick="Submit">@ConstantString.Save</MudLoadingButton>
27+
</MudCardActions>
28+
</MudCardContent>
29+
</MudCard>
30+
}
31+
</MudContainer>
32+
33+
34+
@code {
35+
public string? Title { get; private set; }
36+
[Parameter]
37+
public int Id { get; set; }
38+
MudForm? _form;
39+
private bool _saving = false;
40+
private bool _savingnew = false;
41+
private List<BreadcrumbItem> _breadcrumbItems = new List<BreadcrumbItem>
42+
{
43+
new BreadcrumbItem("Home", href: "/"),
44+
new BreadcrumbItem("{nameofPlural}", href: "/pages/{nameofPlural}")
45+
};
46+
private Update{itemname}Command? model;
47+
protected override async Task OnInitializedAsync()
48+
{
49+
Title = L["Edit {itemname}"];
50+
var itemDto = await Mediator.Send(new Get{itemname}ByIdQuery() { Id = Id });
51+
if (itemDto is not null)
52+
{
53+
model = Mapper.Map<Update{itemname}Command>(itemDto);
54+
_breadcrumbItems.Add(new BreadcrumbItem(itemDto.Name, href: null, disabled: true));
55+
}
56+
57+
}
58+
async Task Submit()
59+
{
60+
try
61+
{
62+
_saving = true;
63+
await _form!.Validate().ConfigureAwait(false);
64+
if (!_form!.IsValid)
65+
return;
66+
var result = await Mediator.Send(model);
67+
if (result.Succeeded)
68+
{
69+
Snackbar.Add(ConstantString.SaveSuccess, MudBlazor.Severity.Info);
70+
}
71+
else
72+
{
73+
Snackbar.Add(result.ErrorMessage, MudBlazor.Severity.Error);
74+
}
75+
}
76+
finally
77+
{
78+
_saving = false;
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)