Skip to content

Commit 79cce57

Browse files
authored
Feat razor changes (#1)
* Changes for Razor WebAssembly Frontend * Add more resources to sql server container for windows * Changes to add books and book authors * Remove testsql file and add line for postCreateCommand.sh
1 parent a2e2201 commit 79cce57

File tree

17 files changed

+461
-72
lines changed

17 files changed

+461
-72
lines changed

.devcontainer/devcontainer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
5959
"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {
6060
"version": "latest"
6161
}
62+
},
63+
"portsAttributes": {
64+
"5000:5000": {
65+
"label": "DAB-HTTP-5000"
66+
},
67+
"5001:5001": {
68+
"label": "DAB-HTTPS-5001"
69+
}
6270
}
6371
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
6472
// "remoteUser": "root"

.devcontainer/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
deploy:
3535
resources:
3636
limits:
37-
cpus: '1'
38-
memory: 1024M
37+
cpus: '2'
38+
memory: 2048M
3939
# Add "forwardPorts": ["1433"] to **devcontainer.json** to forward MSSQL locally.
4040
# (Adding the "ports" property to this file will not forward from a Codespace.)

app/BlazorLibrary/.graphqlrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"strawberryShake": {
66
"name": "LibraryClient",
77
"namespace": "BookLibrary",
8-
"url": "http://127.0.0.1:5001/graphql",
8+
"url": "http://127.0.0.1:5000/graphql",
99
"records": {
1010
"inputs": false,
1111
"entities": false

app/BlazorLibrary/Models/Book.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class Book
77
public string Title { get; set; } = ""; // Initialize with empty string
88
public int? Year { get; set; } // Year of publication (nullable)
99
public int? Pages { get; set; } // Number of pages (nullable)
10+
1011
// Add other properties as needed
1112
}
1213
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Book.cs
2+
namespace BookManagementApp.Models
3+
{
4+
public class BookAuthorIdModel
5+
{
6+
public int author_id { get; set; }
7+
public int book_id { get; set; }
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Book.cs
2+
namespace BookManagementApp.Models
3+
{
4+
public class BookModel
5+
{
6+
public int? Id { get; set; } // Make Id nullable
7+
public string Title { get; set; } = ""; // Initialize with empty string
8+
public string Year { get; set; } // Year of publication (nullable)
9+
public string Pages { get; set; } // Number of pages (nullable)
10+
// Add other properties as needed
11+
}
12+
}

app/BlazorLibrary/Pages/AddAuthor.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<div>
2727
<br>
2828
<button @onclick="AddNewAuthor" class="btn btn-success">Save</button>
29+
<a @onclick="GoToAuthors" class="btn btn-primary">Back to authors</a>
2930
</div>
3031

3132
@code {
@@ -35,7 +36,13 @@
3536
private async Task AddNewAuthor()
3637
{
3738
await authorService.AddAuthorAsync(author);
38-
// Optionally, you can navigate to a different page after adding the book
39+
40+
// Optionally, you can navigate to a different page after adding the Author
41+
Navigation.NavigateTo("/authors");
42+
}
43+
44+
protected void GoToAuthors()
45+
{
3946
Navigation.NavigateTo("/authors");
4047
}
4148
}

app/BlazorLibrary/Pages/AddBook.razor

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
@using BookManagementApp.Services
44
@inject NavigationManager Navigation
55
@inject BookService bookService
6+
@inject AuthorService authorService
67

78
<h3>Book management</h3>
89
<p>Add details for a new book</p>
@@ -20,20 +21,97 @@
2021
<label for="pages">Pages</label>
2122
<input type="number" id="pages" @bind="@book.Pages" placeholder="Number of pages" class="form-control" />
2223
</div>
24+
<div class="form-group">
25+
<label for="pages">Author</label>
26+
</div>
27+
<div class="input-group">
28+
<select id="author" @bind="@AuthorId" class="form-control">
29+
@foreach (var item in authors)
30+
{
31+
<option value="@item.id">@item.first_name @item.last_name</option>
32+
}
33+
</select>
34+
<button @onclick="AddAuthor" type="button" class="btn btn-success">Add Author</button>
35+
</div>
36+
<br>
37+
<legend>Authors</legend>
38+
39+
<div class="form-group">
40+
<table class="table table-striped">
41+
<thead>
42+
<tr>
43+
<th>Author</th>
44+
<th>Action</th>
45+
</tr>
46+
</thead>
47+
<tbody>
48+
@foreach (var item in lstAuthors)
49+
{
50+
<tr>
51+
<td>@item.first_name @item.last_name</td>
52+
<td>
53+
<button @onclick="() => DeleteAuthor(item.id)" type="button" class="btn btn-danger">Delete Author</button>
54+
</td>
55+
</tr>
56+
}
57+
</tbody>
58+
</table>
59+
60+
</div>
2361
</form>
2462

2563
<div>
2664
<br>
2765
<button @onclick="AddNewBook" class="btn btn-success">Save</button>
66+
<a @onclick="GoToBooks" class="btn btn-primary">Back to books</a>
2867
</div>
2968

3069
@code {
3170
private Book book = new Book();
71+
private string? AuthorId = "";
72+
73+
private List<Author> authors = new List<Author>();
74+
75+
private List<Author> lstAuthors = new List<Author>();
76+
77+
protected async override Task OnInitializedAsync()
78+
{
79+
authors = await authorService.GetAuthors();
80+
81+
if (authors != null)
82+
AuthorId = "" + authors.FirstOrDefault().id;
83+
}
3284

3385
private async Task AddNewBook()
3486
{
35-
await bookService.AddBookAsync(book);
87+
var newBook = await bookService.AddBookAsync(book);
88+
89+
foreach(var itm in lstAuthors){
90+
await bookService.AddAuthorBookAsync(newBook.Value.FirstOrDefault().Id, itm.id);
91+
}
92+
3693
// Optionally, you can navigate to a different page after adding the book
3794
Navigation.NavigateTo("/books");
3895
}
96+
97+
private async Task AddAuthor()
98+
{
99+
var authId = int.Parse("" + AuthorId);
100+
var itm = authors.Where(x => x.id == authId).FirstOrDefault();
101+
102+
if (!lstAuthors.Contains(itm)){
103+
lstAuthors.Add(itm);
104+
}
105+
}
106+
107+
private void DeleteAuthor(int? id)
108+
{
109+
var itm = authors.Where(x => x.id == id).FirstOrDefault();
110+
lstAuthors.Remove(itm);
111+
}
112+
113+
protected void GoToBooks()
114+
{
115+
Navigation.NavigateTo("/books");
116+
}
39117
}

app/BlazorLibrary/Pages/AuthorDetails.razor

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@* @page "/authordetails" *@
21
@page "/authordetails/{id}"
32
@using BookManagementApp.Models
43
@using BookManagementApp.Services
@@ -42,11 +41,6 @@
4241

4342
[Parameter]
4443
public string? Id { get; set; }
45-
private async Task ManageAuthor()
46-
{
47-
await authorService.AddAuthorAsync(author);
48-
Navigation.NavigateTo("/books");
49-
}
5044

5145
protected void HandleFailedRequest()
5246
{
@@ -78,20 +72,7 @@
7872

7973
protected async override Task OnInitializedAsync()
8074
{
81-
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-
}
94-
75+
author = await authorService.GetAuthorByIdAsync(int.Parse(Id));
9576
}
9677

9778
protected async void HandleValidRequest()

0 commit comments

Comments
 (0)