Skip to content

Commit

Permalink
Lekce 09 - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Hijtec committed Nov 20, 2024
1 parent 2b3e125 commit 9b42124
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 44 deletions.
7 changes: 7 additions & 0 deletions ToDoList/src/ToDoList.Frontend/Clients/IToDoItemsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ToDoList.Frontend.Clients;
using ToDoList.Frontend.Views;

public interface IToDoItemsClient
{
public List<ToDoItemView> ReadItems();
}
29 changes: 29 additions & 0 deletions ToDoList/src/ToDoList.Frontend/Clients/ToDoItemsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace ToDoList.Frontend.Clients;

using ToDoList.Domain.DTOs;
using ToDoList.Frontend.Views;

public class ToDoItemsClient : IToDoItemsClient
{
private readonly HttpClient httpClient;

Check warning on line 8 in ToDoList/src/ToDoList.Frontend/Clients/ToDoItemsClient.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ToDoItemsClient.httpClient' is never assigned to, and will always have its default value null
public ToDoItemsClient(HttpClient httpClient)

Check warning on line 9 in ToDoList/src/ToDoList.Frontend/Clients/ToDoItemsClient.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'httpClient' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
httpClient = httpClient;

Check warning on line 11 in ToDoList/src/ToDoList.Frontend/Clients/ToDoItemsClient.cs

View workflow job for this annotation

GitHub Actions / build

Assignment made to same variable; did you mean to assign something else?
}

public List<ToDoItemView> ReadItems()
{
var toDoItemsView = new List<ToDoItemView>();
var response = httpClient.GetFromJsonAsync<List<ToDoItemGetResponseDto>>("api/ToDoItems");

toDoItemsView = response.Result.Select(dto => new ToDoItemView

Check warning on line 19 in ToDoList/src/ToDoList.Frontend/Clients/ToDoItemsClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'source' in 'IEnumerable<ToDoItemView> Enumerable.Select<ToDoItemGetResponseDto, ToDoItemView>(IEnumerable<ToDoItemGetResponseDto> source, Func<ToDoItemGetResponseDto, ToDoItemView> selector)'.
{
ToDoItemId = dto.Id,
Name = dto.Name,
Description = dto.Description,
IsCompleted = dto.IsCompleted
}).ToList();

return toDoItemsView;
}
}
32 changes: 20 additions & 12 deletions ToDoList/src/ToDoList.Frontend/Components/Dashboard.razor
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
@rendermode InteractiveServer
@using ToDoList.Frontend.Views
@using ToDoList.Frontend.Clients
@inject IToDoItemsClient ToDoItemsClient
@rendermode InteractiveServer

<h1>Dashboard</h1>

<table>
<tr>
<td><button @onclick="OrderById">Seradit Podle Id</button></td>
<td><button @onclick="OrderByName">Seradit Podle Jmena</button></td>
<td></td>
<td></td>
</tr>

@foreach (var toDoItem in toDoItems)
{
<tr>
<td>@toDoItem.ToDoItemId</td>
<td>@toDoItem.Name</td>
<td>@toDoItem.IsCompleted</td>
<td>@toDoItem.Description</td>
<td>@toDoItem.IsCompleted</td>
</tr>
}
</table>

@code
{
private List<ToDoItemView> toDoItems =
[
new ToDoItemView() {ToDoItemId = 1, Name = "Udelat ukol na Czechitas", IsCompleted = false},
new ToDoItemView() {ToDoItemId = 2, Name = "Udelat nepovinny ukol na Czechitas", IsCompleted = false}
];
private List<ToDoItemView> toDoItems = ToDoItemsClient.ReadItems();

Check failure on line 29 in ToDoList/src/ToDoList.Frontend/Components/Dashboard.razor

View workflow job for this annotation

GitHub Actions / build

A field initializer cannot reference the non-static field, method, or property 'Dashboard.ToDoItemsClient'

Check failure on line 29 in ToDoList/src/ToDoList.Frontend/Components/Dashboard.razor

View workflow job for this annotation

GitHub Actions / build

A field initializer cannot reference the non-static field, method, or property 'Dashboard.ToDoItemsClient'

public class ToDoItemView
public void OrderByName()
{
toDoItems = toDoItems.OrderBy(item => item.Name).ToList();
}
public void OrderById()
{
public int ToDoItemId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool IsCompleted { get; set; }
toDoItems = toDoItems.OrderBy(item => item.ToDoItemId).ToList();
}
}
2 changes: 1 addition & 1 deletion ToDoList/src/ToDoList.Frontend/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PageTitle>Home</PageTitle>

<Dashboard/>
<Dashboard />

<h1>@ahojSvete</h1>

Expand Down
4 changes: 4 additions & 0 deletions ToDoList/src/ToDoList.Frontend/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using ToDoList.Frontend.Clients;
using ToDoList.Frontend.Components;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -6,6 +7,9 @@
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("http://localhost:5000") });
builder.Services.AddScoped<IToDoItemsClient, ToDoItemsClient>();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
62 changes: 31 additions & 31 deletions ToDoList/src/ToDoList.Frontend/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64752",
"sslPort": 44304
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64752",
"sslPort": 44304
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"applicationUrl": "https://localhost:7001;http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5174",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7006;http://localhost:5174",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
4 changes: 4 additions & 0 deletions ToDoList/src/ToDoList.Frontend/ToDoList.Frontend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ToDoList.Domain\ToDoList.Domain.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions ToDoList/src/ToDoList.Frontend/Views/ToDoItemView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ToDoList.Frontend.Views;

public class ToDoItemView
{
public int ToDoItemId { get; set; }
public string Name { get; set; }

Check warning on line 6 in ToDoList/src/ToDoList.Frontend/Views/ToDoItemView.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string Description { get; set; }

Check warning on line 7 in ToDoList/src/ToDoList.Frontend/Views/ToDoItemView.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Description' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public bool IsCompleted { get; set; }
}

0 comments on commit 9b42124

Please sign in to comment.