Skip to content

Commit 23b509a

Browse files
authored
Merge pull request #292 from abpframework/blazor-examples
Update Blazor bookstore examples to handle errors
2 parents 7a4508c + e2b7efe commit 23b509a

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

BookStore-Blazor-EfCore/src/Acme.BookStore.Blazor/Pages/Authors.razor.cs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,22 @@ private void OpenEditAuthorModal(AuthorDto author)
112112

113113
private async Task DeleteAuthorAsync(AuthorDto author)
114114
{
115-
var confirmMessage = L["AuthorDeletionConfirmationMessage", author.Name];
116-
if (!await Message.Confirm(confirmMessage))
115+
try
117116
{
118-
return;
119-
}
117+
var confirmMessage = L["AuthorDeletionConfirmationMessage", author.Name];
118+
if (!await Message.Confirm(confirmMessage))
119+
{
120+
return;
121+
}
120122

121-
await AuthorAppService.DeleteAsync(author.Id);
122-
await GetAuthorsAsync();
123+
await AuthorAppService.DeleteAsync(author.Id);
124+
await GetAuthorsAsync();
125+
}
126+
catch (Exception ex)
127+
{
128+
await HandleErrorAsync(ex);
129+
}
130+
123131
}
124132

125133
private void CloseEditAuthorModal()
@@ -129,21 +137,35 @@ private void CloseEditAuthorModal()
129137

130138
private async Task CreateAuthorAsync()
131139
{
132-
if (await CreateValidationsRef.ValidateAll())
140+
try
133141
{
134-
await AuthorAppService.CreateAsync(NewAuthor);
135-
await GetAuthorsAsync();
136-
CreateAuthorModal.Hide();
142+
if (await CreateValidationsRef.ValidateAll())
143+
{
144+
await AuthorAppService.CreateAsync(NewAuthor);
145+
await GetAuthorsAsync();
146+
CreateAuthorModal.Hide();
147+
}
148+
}
149+
catch (Exception ex)
150+
{
151+
await HandleErrorAsync(ex);
137152
}
138153
}
139154

140155
private async Task UpdateAuthorAsync()
141156
{
142-
if (await EditValidationsRef.ValidateAll())
157+
try
143158
{
144-
await AuthorAppService.UpdateAsync(EditingAuthorId, EditingAuthor);
145-
await GetAuthorsAsync();
146-
EditAuthorModal.Hide();
159+
if (await EditValidationsRef.ValidateAll())
160+
{
161+
await AuthorAppService.UpdateAsync(EditingAuthorId, EditingAuthor);
162+
await GetAuthorsAsync();
163+
EditAuthorModal.Hide();
164+
}
165+
}
166+
catch (Exception ex)
167+
{
168+
await HandleErrorAsync(ex);
147169
}
148170
}
149171
}

MudBlazorSample/src/Acme.BookStore.Blazor/Pages/Authors.razor.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,21 @@ private void OpenEditAuthorModal(AuthorDto author)
8585

8686
private async Task DeleteAuthorAsync(AuthorDto author)
8787
{
88-
var confirmMessage = L["AuthorDeletionConfirmationMessage", author.Name];
89-
if (!await Message.Confirm(confirmMessage))
88+
try
9089
{
91-
return;
92-
}
90+
var confirmMessage = L["AuthorDeletionConfirmationMessage", author.Name];
91+
if (!await Message.Confirm(confirmMessage))
92+
{
93+
return;
94+
}
9395

94-
await AuthorAppService.DeleteAsync(author.Id);
95-
await _authorList.ReloadServerData();
96+
await AuthorAppService.DeleteAsync(author.Id);
97+
await _authorList.ReloadServerData();
98+
}
99+
catch (Exception ex)
100+
{
101+
await HandleErrorAsync(ex);
102+
}
96103
}
97104

98105
private void CloseEditAuthorModal()
@@ -102,21 +109,35 @@ private void CloseEditAuthorModal()
102109

103110
private async Task CreateAuthorAsync()
104111
{
105-
if (_createForm.IsValid)
112+
try
106113
{
107-
await AuthorAppService.CreateAsync(NewAuthor);
108-
_createAuthorDialogVisible = false;
109-
await _authorList.ReloadServerData();
114+
if (_createForm.IsValid)
115+
{
116+
await AuthorAppService.CreateAsync(NewAuthor);
117+
_createAuthorDialogVisible = false;
118+
await _authorList.ReloadServerData();
119+
}
110120
}
121+
catch (Exception ex)
122+
{
123+
await HandleErrorAsync(ex);
124+
}
111125
}
112126

113127
private async Task UpdateAuthorAsync()
114128
{
115-
if (_editForm.IsValid)
129+
try
116130
{
117-
await AuthorAppService.UpdateAsync(EditingAuthorId, EditingAuthor);
118-
_editAuthorDialogVisible = false;
119-
await _authorList.ReloadServerData();
131+
if (_editForm.IsValid)
132+
{
133+
await AuthorAppService.UpdateAsync(EditingAuthorId, EditingAuthor);
134+
_editAuthorDialogVisible = false;
135+
await _authorList.ReloadServerData();
136+
}
137+
}
138+
catch (Exception ex)
139+
{
140+
await HandleErrorAsync(ex);
120141
}
121142
}
122143
}

0 commit comments

Comments
 (0)