66@inject NavigationManager Navigation
77@inject AuthorService authorService
88
9- @* @if(string.IsNullOrEmpty(Id))
10- {
11- <PageTitle>Add New Author</PageTitle>
12- <h3>Author management</h3>
13- <p>Add details for a new author</p>
14- }
15- else
16- { *@
17- <PageTitle >Edit Author</PageTitle >
18- <h3 >Author details</h3 >
19- <p >Edit details for a existing author</p >
20- @* } *@
9+ <PageTitle >Edit Author</PageTitle >
10+ <h3 >Author details</h3 >
11+ <p >Edit details for a existing author</p >
2112
2213<div class =" row" >
2314 <EditForm Model =" @author" OnValidSubmit =" @HandleValidRequest" OnInvalidSubmit =" @HandleFailedRequest" >
2415 <div class =" form-group" >
2516 <label class =" col-md-8" >First name</label >
26- <InputText class =" col-md-8 form-control" @bind-Value =" @author.first_name" />
17+ <InputText class =" col-md-8 form-control" @bind-Value =" @author.first_name" />
2718 </div >
2819 <div class =" form-group" >
2920 <label class =" col-md-8" >Middle name</label >
30- <InputText class =" col-md-8 form-control" @bind-Value =" @author.middle_name" />
21+ <InputText class =" col-md-8 form-control" @bind-Value =" @author.middle_name" />
3122 </div >
3223 <div class =" form-group" >
3324 <label class =" col-md-8" >Last name</label >
34- <InputText class =" col-md-8 form-control" @bind-Value =" @author.last_name" />
25+ <InputText class =" col-md-8 form-control" @bind-Value =" @author.last_name" />
3526 </div >
36- <br />
27+ <br />
3728 <div class =" form-group" >
3829 <button type =" submit" class =" btn btn-success" >Save author</button >
3930 <a @onclick =" GoToAuthors" class =" btn btn-primary" >Back to authors</a >
4031 <a @onclick =" @DeleteAuthor" class =" btn btn-danger" >Delete author</a >
41- @* @if (!string.IsNullOrEmpty(Id))
42- {
43- <a @onclick="@DeleteAuthor" class="btn btn-danger">Delete author</a>
44- } *@
4532 </div >
46- <br />
33+ <br />
4734 <p style =" color : red " >@Message </p >
4835 </EditForm >
4936</div >
5037
5138@code {
5239
5340 protected string Message = string .Empty ;
54- protected Author author { get ; set ;} = new Author ();
41+ protected Author author { get ; set ; } = new Author ();
5542
5643 [Parameter ]
5744 public string ? Id { get ; set ; }
9178
9279 protected async override Task OnInitializedAsync ()
9380 {
94- @* if (!string.IsNullOrEmpty(Id))
95- { *@
96- var authorId = Convert .ToInt32 (Id );
97- var apiAuthor = await authorService .GetAuthorByIdAsync (authorId );
98- if (apiAuthor != null )
99- {
100- author = apiAuthor ;
101- StateHasChanged ();
102- Console .WriteLine ($" Fetched author: {JsonSerializer .Serialize (author )}" );
103- }
104- @* } *@
105- @* else
106- { *@
107- // Code for adding a new author goes here
108- @* if (string.IsNullOrEmpty(Id))
109- {
110- var result = await authorService.AddAuthorAsync2(author);
11181
112- if (result)
113- Navigation.NavigateTo("/authors");
114- else
115- Message = "Failed to add author";
116- }
117- else
118- {
119- author.id = Convert.ToInt32(Id); // Add this line
120- var result = await authorService.UpdateAuthorAsync(author);
82+ var authorId = Convert .ToInt32 (Id );
83+ var apiAuthor = await authorService .GetAuthorByIdAsync (authorId );
84+ if (apiAuthor != null )
85+ {
86+ author = apiAuthor ;
87+ StateHasChanged ();
88+ Console .WriteLine ($" Fetched author: {JsonSerializer .Serialize (author )}" );
89+ }
90+ else
91+ {
92+ Message = " Author not found" ;
93+ }
12194
122- if (result)
123- Navigation.NavigateTo("/authors");
124- else
125- Message = "Failed to update author";
126- }
127- } *@
12895 }
12996
13097 protected async void HandleValidRequest ()
13198 {
132- @* if (string.IsNullOrEmpty(Id))
133- {
134- var result = await authorService.AddAuthorAsync2(author);
99+ author .id = Convert .ToInt32 (Id ); // Set the id of the author
100+ var result = await authorService .UpdateAuthorAsync (author );
135101
136- if (result)
137- Navigation.NavigateTo("/authors");
138- else
139- Message = "Failed to add author";
140- }
102+ if (result )
103+ Navigation .NavigateTo (" /authors" );
141104 else
142- { *@
143- author .id = Convert .ToInt32 (Id ); // Add this line
144- var result = await authorService .UpdateAuthorAsync (author );
145-
146- if (result )
147- Navigation .NavigateTo (" /authors" );
148- else
149- Message = " Failed to update author" ;
150- @* } *@
105+ Message = " Failed to update author" ;
151106 }
152107}
0 commit comments