Skip to content

Commit dd1113c

Browse files
committed
Stable, needs some cleaning
1 parent d9a1d97 commit dd1113c

File tree

5 files changed

+104
-35
lines changed

5 files changed

+104
-35
lines changed

app/BlazorLibrary/Models/Author.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BookManagementApp.Models
44
{
55
public class Author
66
{
7-
public int? Id { get; set; } // Make Id nullable
7+
public int? id { get; set; } // Make Id nullable
88

99
public string first_name { get; set; } = ""; // Initialize with empty string
1010

app/BlazorLibrary/Pages/AuthorDetails.razor

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
@page "/authordetails"
1+
@* @page "/authordetails" *@
22
@page "/authordetails/{id}"
33
@using BookManagementApp.Models
44
@using BookManagementApp.Services
55
@using System.Text.Json
66
@inject NavigationManager Navigation
77
@inject AuthorService authorService
88

9-
@if(string.IsNullOrEmpty(Id))
9+
@* @if(string.IsNullOrEmpty(Id))
1010
{
1111
<PageTitle>Add New Author</PageTitle>
1212
<h3>Author management</h3>
1313
<p>Add details for a new author</p>
1414
}
1515
else
16-
{
16+
{ *@
1717
<PageTitle>Edit Author</PageTitle>
1818
<h3>Author details</h3>
1919
<p>Edit details for a existing author</p>
20-
}
20+
@* } *@
2121

2222
<div class="row">
2323
<EditForm Model="@author" OnValidSubmit="@HandleValidRequest" OnInvalidSubmit="@HandleFailedRequest">
@@ -91,8 +91,8 @@ else
9191

9292
protected async override Task OnInitializedAsync()
9393
{
94-
if (!string.IsNullOrEmpty(Id))
95-
{
94+
@* if (!string.IsNullOrEmpty(Id))
95+
{ *@
9696
var authorId = Convert.ToInt32(Id);
9797
var apiAuthor = await authorService.GetAuthorByIdAsync(authorId);
9898
if (apiAuthor != null)
@@ -101,11 +101,11 @@ else
101101
StateHasChanged();
102102
Console.WriteLine($"Fetched author: {JsonSerializer.Serialize(author)}");
103103
}
104-
}
105-
else
106-
{
104+
@* } *@
105+
@* else
106+
{ *@
107107
// Code for adding a new author goes here
108-
if (string.IsNullOrEmpty(Id))
108+
@* if (string.IsNullOrEmpty(Id))
109109
{
110110
var result = await authorService.AddAuthorAsync2(author);
111111
@@ -116,20 +116,20 @@ else
116116
}
117117
else
118118
{
119-
author.Id = Convert.ToInt32(Id); // Add this line
119+
author.id = Convert.ToInt32(Id); // Add this line
120120
var result = await authorService.UpdateAuthorAsync(author);
121121
122122
if (result)
123123
Navigation.NavigateTo("/authors");
124124
else
125125
Message = "Failed to update author";
126126
}
127-
}
127+
} *@
128128
}
129129

130130
protected async void HandleValidRequest()
131131
{
132-
if (string.IsNullOrEmpty(Id))
132+
@* if (string.IsNullOrEmpty(Id))
133133
{
134134
var result = await authorService.AddAuthorAsync2(author);
135135
@@ -139,14 +139,14 @@ else
139139
Message = "Failed to add author";
140140
}
141141
else
142-
{
143-
author.Id = Convert.ToInt32(Id); // Add this line
142+
{ *@
143+
author.id = Convert.ToInt32(Id); // Add this line
144144
var result = await authorService.UpdateAuthorAsync(author);
145145

146146
if (result)
147147
Navigation.NavigateTo("/authors");
148148
else
149149
Message = "Failed to update author";
150-
}
150+
@* } *@
151151
}
152152
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
@page "/editauthor/{Id}"
2+
@using BookManagementApp.Models
3+
@using BookManagementApp.Services
4+
@using System.Text.Json
5+
@inject NavigationManager Navigation
6+
@inject AuthorService authorService
7+
8+
<PageTitle>Edit Author</PageTitle>
9+
<h3>Author management</h3>
10+
<p>Edit details of existing author</p>
11+
12+
<form>
13+
<div class="form-group">
14+
<label for="firstname">Id</label>
15+
<input type="text" id="id" @bind="@author.id" class="form-control" />
16+
</div>
17+
<div class="form-group">
18+
<label for="firstname">First name</label>
19+
<input type="text" id="firstname" @bind="@author.first_name" class="form-control" />
20+
</div>
21+
<div class="form-group">
22+
<label for="middlename">Middle name</label>
23+
<input type="text" id="middlename" @bind="@author.middle_name" class="form-control" />
24+
</div>
25+
<div class="form-group">
26+
<label for="lastname">Last name</label>
27+
<input type="text" id="lastname" @bind="@author.last_name" class="form-control" />
28+
</div>
29+
30+
<div class="row">
31+
<div class="col-md-4">
32+
<div class="form-group">
33+
<input type="button" class="btn btn-success" @onclick="@UpdateEmployee" value="Update author" />
34+
<input type="button" class="btn btn-danger" @onclick="@Cancel" value="Cancel" />
35+
</div>
36+
</div>
37+
</div>
38+
</form>
39+
40+
@code {
41+
@* protected Author author { get; set; } = new Author(); *@
42+
43+
[Parameter]
44+
public string Id { get; set; }
45+
46+
Author author = new Author();
47+
48+
protected override async Task OnInitializedAsync()
49+
{
50+
Console.WriteLine($"Id: {Id}");
51+
author = await authorService.GetAuthorByIdAsync(Convert.ToInt32(Id));
52+
}
53+
54+
private async Task UpdateEmployee()
55+
{
56+
await authorService.UpdateAuthorAsync(author);
57+
Navigation.NavigateTo("/authors");
58+
}
59+
60+
private void Cancel()
61+
{
62+
Navigation.NavigateTo("/authors");
63+
}
64+
}

app/BlazorLibrary/Services/AuthorService.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace BookManagementApp.Services
1313
{
1414

15-
public class ApiResponse
15+
public class AuthorResponse
1616
{
17-
public List<Author>? Value { get; set; }
17+
public List<Author> Value { get; set; }
1818
}
1919

2020
public class AuthorService
@@ -30,19 +30,24 @@ public AuthorService(HttpClient httpClient)
3030
{
3131
try
3232
{
33-
var response = await _httpClient.GetStreamAsync($"http://127.0.0.1:5001/api/Author/id/{id}");
34-
var author = await JsonSerializer.DeserializeAsync<Author>(response, new JsonSerializerOptions
33+
var responseStream = await _httpClient.GetStreamAsync($"http://127.0.0.1:5001/api/Author/id/{id}");
34+
var reader = new StreamReader(responseStream);
35+
var responseString = await reader.ReadToEndAsync();
36+
37+
var authorResponse = JsonSerializer.Deserialize<AuthorResponse>(responseString, new JsonSerializerOptions
3538
{
3639
PropertyNameCaseInsensitive = true
3740
});
41+
42+
var author = authorResponse.Value.FirstOrDefault();
3843
return author;
3944
}
4045
catch (Exception ex)
4146
{
4247
Console.WriteLine($"Error fetching author: {ex}");
4348
return null;
4449
}
45-
}
50+
}
4651

4752
public async Task AddAuthorAsync(Author author)
4853
{
@@ -57,18 +62,18 @@ public async Task AddAuthorAsync(Author author)
5762
response.EnsureSuccessStatusCode();
5863
}
5964

60-
public async Task<bool> AddAuthorAsync2(Author author)
61-
{
62-
var authorWithoutId2 = new
63-
{
64-
author.first_name,
65-
author.middle_name,
66-
author.last_name
67-
};
65+
// public async Task<bool> AddAuthorAsync2(Author author)
66+
// {
67+
// var authorWithoutId2 = new
68+
// {
69+
// author.first_name,
70+
// author.middle_name,
71+
// author.last_name
72+
// };
6873

69-
var response = await _httpClient.PostAsJsonAsync("http://127.0.0.1:5001/api/Author", authorWithoutId2);
70-
return response.IsSuccessStatusCode;
71-
}
74+
// var response = await _httpClient.PostAsJsonAsync("http://127.0.0.1:5001/api/Author", authorWithoutId2);
75+
// return response.IsSuccessStatusCode;
76+
// }
7277

7378
public async Task<bool> UpdateAuthorAsync(Author author)
7479
{
@@ -80,7 +85,7 @@ public async Task<bool> UpdateAuthorAsync(Author author)
8085
author.last_name
8186
};
8287

83-
var response = await _httpClient.PatchAsJsonAsync($"http://127.0.0.1:5001/api/Author/id/{author.Id}", authorWithoutId);
88+
var response = await _httpClient.PatchAsJsonAsync($"http://127.0.0.1:5001/api/Author/id/{author.id}", authorWithoutId);
8489
return response.IsSuccessStatusCode;
8590
}
8691

app/BlazorLibrary/Shared/MainLayout.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<main>
99
<div class="top-row px-4">
10-
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
10+
<a href="https://aka.ms/sqldev" target="_blank">Azure SQL - Developer resources</a>
1111
</div>
1212

1313
<article class="content px-4">

0 commit comments

Comments
 (0)