Init commit.

This commit is contained in:
emantzoros 2025-09-19 14:55:50 +03:00
parent 8d8484a648
commit c441f3c941
63 changed files with 1981 additions and 2 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
BlazorApp.Client/bin/
BlazorApp.Client/obj/
BlazorApp.Shared/obj/
BlazorApp/bin/
BlazorApp/obj/
Bullet6/bin/
Bullet6/obj/
BlazorApp.Shared/bin/

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazor.Bootstrap" Version="3.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.13" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BlazorApp.Shared\BlazorApp.Shared.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,19 @@
using BlazorApp.Client.Interfaces;
using BlazorApp.Client.Proxies;
namespace BlazorApp.Client
{
public static class Extensions
{
public static IServiceCollection AddClientServices(this IServiceCollection services)
{
services.AddScoped<ICustomerProxy, CustomerProxy>();
services.AddSingleton<WeatherForecastService>();
services.AddHttpClient("BlazorAppAPI", client =>
{
client.BaseAddress = new Uri("https://localhost:7018/");
});
return services;
}
}
}

View File

@ -0,0 +1,14 @@
using BlazorApp.Shared.Models;
using BlazorApp.Shared.Models.Pagination;
namespace BlazorApp.Client.Interfaces
{
public interface ICustomerProxy
{
Task<PaginatedResult<Customer>> Query(int pageIndex = 0, int resultsPerPage = 10);
Task<bool> Save(Customer model);
Task<bool> Update(Customer model);
Task<bool> Delete(string id);
Task<Customer> Get(string id);
}
}

View File

@ -0,0 +1,19 @@
@page "/counter"
@rendermode InteractiveAuto
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}

View File

@ -0,0 +1,94 @@
@page "/customers/edit/{id}"
@rendermode InteractiveAuto
@attribute [StreamRendering]
@using BlazorApp.Client.Interfaces
@using BlazorApp.Shared.Models
@using BlazorApp.Shared.Queries
@using BlazorApp.Client.Proxies
@using BlazorBootstrap
@inject ToastService ToastService
@inject ICustomerProxy CustomerProxy
@inject NavigationManager NavigationManager
<PageTitle>Επεξεργασία πελάτη</PageTitle>
<h1>Επεξεργασία πελάτη</h1>
<p>Στοιχεία πελάτη</p>
@if (model == null)
{
<p><em>Loading...</em></p>
}
else
{
<EditForm Model="@model" OnValidSubmit="HandleValidSubmit" FormName="EditCustomer">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label>CompanyName:</label>
<InputText @bind-Value="model.CompanyName" class="form-control" />
</div>
<div class="mb-3">
<label>ContactName:</label>
<InputText @bind-Value="model.ContactName" class="form-control" />
</div>
<div class="mb-3">
<label>Address:</label>
<InputText @bind-Value="model.Address" class="form-control" />
</div>
<div class="mb-3">
<label>City:</label>
<InputText @bind-Value="model.City" class="form-control" />
</div>
<div class="mb-3">
<label>Region:</label>
<InputText @bind-Value="model.Region" class="form-control" />
</div>
<div class="mb-3">
<label>PostalCode:</label>
<InputText @bind-Value="model.PostalCode" class="form-control" />
</div>
<div class="mb-3">
<label>Country:</label>
<InputText @bind-Value="model.Country" class="form-control" />
</div>
<div class="mb-3">
<label>Phone:</label>
<InputText @bind-Value="model.Phone" class="form-control" />
</div>
<button type="button" @onclick="Back" class="btn btn-light">Πίσω</button>
<button type="submit" class="btn btn-primary">Αποθήκευση</button>
</EditForm>
}
@code {
[Parameter]
public string id { get; set; }
[SupplyParameterFromForm]
private Customer? model { get; set; }
private void Back() => NavigationManager.NavigateTo("customers");
protected override async Task OnInitializedAsync()
{
model = await CustomerProxy.Get(id);
}
private async Task HandleValidSubmit()
{
var success = await CustomerProxy.Update(model);
if (success)
{
ToastService.Notify(new(ToastType.Success, $"Επιτυχής ενημέρωση."));
NavigationManager.NavigateTo("customers");
}
else
{
ToastService.Notify(new(ToastType.Danger, $"Αποτυχία ενημέρωσης."));
}
}
}

View File

@ -0,0 +1,112 @@
@page "/customers"
@rendermode InteractiveAuto
@using BlazorApp.Client.Interfaces
@using BlazorApp.Shared.Models
@using BlazorApp.Shared.Models.Pagination
@using BlazorApp.Shared.Queries
@using BlazorApp.Client.Proxies
@inject ToastService ToastService
@inject IHttpClientFactory ClientFactory
@attribute [StreamRendering]
@inject ICustomerProxy CustomerProxy
@inject NavigationManager NavigationManager
@using BlazorBootstrap
<PageTitle>Customers</PageTitle>
<h1>Customers</h1>
<p>This component demonstrates showing data.</p>
@if (model == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>CompanyName</th>
<th>ContactName</th>
<th>Address</th>
<th>City</th>
<th>Region</th>
<th>PostalCode</th>
<th>Country</th>
<th>Phone</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var result in model.Results)
{
<tr>
<td>@result.Id</td>
<td>@result.CompanyName</td>
<td>@result.ContactName</td>
<td>@result.Address</td>
<td>@result.City</td>
<td>@result.Region</td>
<td>@result.PostalCode</td>
<td>@result.Country</td>
<td>@result.Phone</td>
<td>
<button type="button" @onclick="() => EditCustomer(result.Id)" class="btn btn-secondary">Επεξεργασια</button>
<button type="button" @onclick="async () => await DeleteCustomer(result.Id)" class="btn btn-danger">
Διαγραφή
</button>
</td>
</tr>
}
</tbody>
</table>
<Pagination ActivePageNumber="activePageIndex + 1"
TotalPages="totalPages"
DisplayPages="5"
Alignment="Alignment.Center"
FirstLinkIcon="IconName.ChevronDoubleLeft"
PreviousLinkIcon="IconName.ChevronLeft"
NextLinkIcon="IconName.ChevronRight"
LastLinkIcon="IconName.ChevronDoubleRight"
PageChanged="OnPageChangedAsync" />
}
<button type="button" @onclick="NewCustomer" class="btn btn-primary">Προσθήκη πελάτη</button>
@code {
private PaginatedResult<Customer> model;
private int activePageIndex = 0;
private int resultsPerPage = 10;
private int totalPages { get => model.TotalCount % resultsPerPage != 0 ? (model.TotalCount / resultsPerPage) + 1 : (model.TotalCount / resultsPerPage); }
protected override async Task OnInitializedAsync()
{
model = await CustomerProxy.Query(activePageIndex, resultsPerPage);
}
private async Task OnPageChangedAsync(int newPageNumber)
{
activePageIndex = newPageNumber - 1;
model = await CustomerProxy.Query(activePageIndex, resultsPerPage);
}
private void NewCustomer() => NavigationManager.NavigateTo("customers/new");
private void EditCustomer(string id) => NavigationManager.NavigateTo($"customers/edit/{id}");
private async Task DeleteCustomer(string id)
{
var success = await CustomerProxy.Delete(id);
if (success)
{
ToastService.Notify(new(ToastType.Success, $"Επιτυχής διαγραφή."));
model = await CustomerProxy.Query(activePageIndex, resultsPerPage);
}
else
{
ToastService.Notify(new(ToastType.Danger, $"Αποτυχία διαγραφής."));
}
}
}

View File

@ -0,0 +1,80 @@
@page "/customers/new"
@rendermode InteractiveAuto
@attribute [StreamRendering]
@using BlazorApp.Client.Interfaces
@using BlazorApp.Shared.Models
@using BlazorApp.Shared.Queries
@using BlazorApp.Client.Proxies
@using BlazorBootstrap
@inject ToastService ToastService
@inject ICustomerProxy CustomerProxy
@inject NavigationManager NavigationManager
<PageTitle>Νέος πελάτης</PageTitle>
<h1>Νέος πελάτης</h1>
<p>Στοιχεία πελάτη</p>
<EditForm Model="@model" OnValidSubmit="HandleValidSubmit" FormName="NewCustomer">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label>CompanyName:</label>
<InputText @bind-Value="model.CompanyName" class="form-control" />
</div>
<div class="mb-3">
<label>ContactName:</label>
<InputText @bind-Value="model.ContactName" class="form-control" />
</div>
<div class="mb-3">
<label>Address:</label>
<InputText @bind-Value="model.Address" class="form-control" />
</div>
<div class="mb-3">
<label>City:</label>
<InputText @bind-Value="model.City" class="form-control" />
</div>
<div class="mb-3">
<label>Region:</label>
<InputText @bind-Value="model.Region" class="form-control" />
</div>
<div class="mb-3">
<label>PostalCode:</label>
<InputText @bind-Value="model.PostalCode" class="form-control" />
</div>
<div class="mb-3">
<label>Country:</label>
<InputText @bind-Value="model.Country" class="form-control" />
</div>
<div class="mb-3">
<label>Phone:</label>
<InputText @bind-Value="model.Phone" class="form-control" />
</div>
<button type="button" @onclick="Back" class="btn btn-light">Πίσω</button>
<button type="submit" class="btn btn-primary">Αποθήκευση</button>
</EditForm>
@code {
[SupplyParameterFromForm]
private Customer? model { get; set; }
protected override void OnInitialized() => model ??= new();
private void Back() => NavigationManager.NavigateTo("customers");
private async Task HandleValidSubmit()
{
var success = await CustomerProxy.Save(model);
if (success)
{
ToastService.Notify(new(ToastType.Success, $"Επιτυχής δημιουργία."));
NavigationManager.NavigateTo("customers");
}
else
{
ToastService.Notify(new(ToastType.Danger, $"Αποτυχία δημιουργίας."));
}
}
}

View File

@ -0,0 +1,9 @@
using BlazorApp.Client;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddBlazorBootstrap();
builder.Services.AddClientServices();
await builder.Build().RunAsync();

View File

@ -0,0 +1,73 @@
using BlazorApp.Client.Interfaces;
using BlazorApp.Shared.Models;
using BlazorApp.Shared.Models.Pagination;
using BlazorApp.Shared.Queries;
using System.Net.Http.Json;
namespace BlazorApp.Client.Proxies
{
public class CustomerProxy : ICustomerProxy
{
private readonly IHttpClientFactory _httpClientFactory;
public CustomerProxy(IHttpClientFactory httpClientFactory)
{
this._httpClientFactory = httpClientFactory;
}
public async Task<PaginatedResult<Customer>> Query(int pageIndex = 0, int resultsPerPage = 10)
{
PaginatedResult<Customer> models = new PaginatedResult<Customer>();
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
var query = new CustomerQuery
{
PageIndex = pageIndex,
ResultsPerPage = resultsPerPage
};
var response = await client.PostAsJsonAsync("customer/query", query);
if (response.IsSuccessStatusCode)
{
models = await response.Content.ReadFromJsonAsync<PaginatedResult<Customer>>();
}
return models;
}
public async Task<bool> Save(Customer model)
{
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
var response = await client.PostAsJsonAsync("customer/save", model);
return response.IsSuccessStatusCode;
}
public async Task<bool> Update(Customer model)
{
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
var response = await client.PatchAsJsonAsync("customer/update", model);
return response.IsSuccessStatusCode;
}
public async Task<bool> Delete(string id)
{
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
var response = await client.DeleteAsync($"customer/{id}");
return response.IsSuccessStatusCode;
}
public async Task<Customer> Get(string id)
{
var client = this._httpClientFactory.CreateClient("BlazorAppAPI");
var response = await client.GetAsync($"customer/{id}");
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadFromJsonAsync<Customer>();
}
else
{
throw new Exception($"Customer with id: {id} not found");
}
}
}
}

View File

@ -0,0 +1,22 @@
using BlazorApp.Shared.Models;
namespace BlazorApp.Client
{
public class WeatherForecastService
{
private static readonly string[] Summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];
public Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}
}

View File

@ -0,0 +1,9 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorApp.Client

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,15 @@
namespace BlazorApp.Shared.Models
{
public class Customer
{
public string? Id { get; set; }
public string? CompanyName { get; set; }
public string? ContactName { get; set; }
public string? Address { get; set; }
public string? City { get; set; }
public string? Region { get; set; }
public string? PostalCode { get; set; }
public string? Country { get; set; }
public string? Phone { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace BlazorApp.Shared.Models.Pagination
{
public class PaginatedResult<T>
{
public IEnumerable<T> Results { get; set; } = new List<T>();
public int TotalCount { get; set; } = 0;
}
}

View File

@ -0,0 +1,10 @@
namespace BlazorApp.Shared.Models
{
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}

View File

@ -0,0 +1,8 @@
namespace BlazorApp.Shared.Queries
{
public class BaseQuery
{
public int PageIndex { get; set; } = 0;
public int ResultsPerPage { get; set; } = 10;
}
}

View File

@ -0,0 +1,6 @@
namespace BlazorApp.Shared.Queries
{
public class CustomerQuery : BaseQuery
{
}
}

43
BlazorApp.sln Normal file
View File

@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34701.34
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp", "BlazorApp\BlazorApp.csproj", "{2DA700EB-3F13-4FD0-9217-FD2D41E34CA0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Client", "BlazorApp.Client\BlazorApp.Client.csproj", "{D05644D7-D16D-4110-95D0-CCEF18B7AA39}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp.Shared", "BlazorApp.Shared\BlazorApp.Shared.csproj", "{C6627F01-4882-40B1-890C-D70AA74B9BD1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bullet6", "Bullet6\Bullet6.csproj", "{8FA67E47-DDAB-4712-B628-F904086B1F77}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2DA700EB-3F13-4FD0-9217-FD2D41E34CA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DA700EB-3F13-4FD0-9217-FD2D41E34CA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DA700EB-3F13-4FD0-9217-FD2D41E34CA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DA700EB-3F13-4FD0-9217-FD2D41E34CA0}.Release|Any CPU.Build.0 = Release|Any CPU
{D05644D7-D16D-4110-95D0-CCEF18B7AA39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D05644D7-D16D-4110-95D0-CCEF18B7AA39}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D05644D7-D16D-4110-95D0-CCEF18B7AA39}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D05644D7-D16D-4110-95D0-CCEF18B7AA39}.Release|Any CPU.Build.0 = Release|Any CPU
{C6627F01-4882-40B1-890C-D70AA74B9BD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6627F01-4882-40B1-890C-D70AA74B9BD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6627F01-4882-40B1-890C-D70AA74B9BD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6627F01-4882-40B1-890C-D70AA74B9BD1}.Release|Any CPU.Build.0 = Release|Any CPU
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FA67E47-DDAB-4712-B628-F904086B1F77}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {249CF358-4880-4F79-BF13-5D4198AEB00E}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BlazorApp.Client\BlazorApp.Client.csproj" />
<ProjectReference Include="..\BlazorApp.Shared\BlazorApp.Shared.csproj" />
<PackageReference Include="Blazor.Bootstrap" Version="3.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Scrutor" Version="6.1.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
<link href="_content/Blazor.Bootstrap/blazor.bootstrap.css" rel="stylesheet" />
@* <link rel="stylesheet" href="bootstrap/bootstrap.min.css" /> *@
<link rel="stylesheet" href="app.css" />
<link rel="stylesheet" href="BlazorApp.styles.css" />
<link rel="icon" type="image/png" href="favicon.png" />
<HeadOutlet />
</head>
<body>
<Routes />
<script src="_framework/blazor.web.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
<!-- Add chart.js reference if chart components are used in your application. -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.0.1/chart.umd.js" integrity="sha512-gQhCDsnnnUfaRzD8k1L5llCCV6O9HN09zClIzzeJ8OJ9MpGmIlCxm+pdCkqTwqJ4JcjbojFr79rl2F1mzcoLMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- Add chartjs-plugin-datalabels.min.js reference if chart components with data label feature is used in your application. -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.2.0/chartjs-plugin-datalabels.min.js" integrity="sha512-JPcRR8yFa8mmCsfrw4TNte1ZvF1e3+1SdGMslZvmrzDYxS69J7J49vkFL8u6u8PlPJK+H3voElBtUCzaXj+6ig==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- Add sortable.js reference if SortableList component is used in your application. -->
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
<script src="_content/Blazor.Bootstrap/blazor.bootstrap.js"></script>
</body>
</html>

View File

@ -0,0 +1,26 @@
@inherits LayoutComponentBase
@using BlazorBootstrap
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
</div>
<article class="content px-4">
@Body
</article>
</main>
</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<Toasts @rendermode="InteractiveAuto" class="p-3" AutoHide="true" Delay="5000" Placement="ToastsPlacement.TopRight" />

View File

@ -0,0 +1,96 @@
.page {
position: relative;
display: flex;
flex-direction: column;
}
main {
flex: 1;
}
.sidebar {
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
}
.top-row {
background-color: #f7f7f7;
border-bottom: 1px solid #d6d5d5;
justify-content: flex-end;
height: 3.5rem;
display: flex;
align-items: center;
}
.top-row ::deep a, .top-row ::deep .btn-link {
white-space: nowrap;
margin-left: 1.5rem;
text-decoration: none;
}
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
text-decoration: underline;
}
.top-row ::deep a:first-child {
overflow: hidden;
text-overflow: ellipsis;
}
@media (max-width: 640.98px) {
.top-row {
justify-content: space-between;
}
.top-row ::deep a, .top-row ::deep .btn-link {
margin-left: 0;
}
}
@media (min-width: 641px) {
.page {
flex-direction: row;
}
.sidebar {
width: 250px;
height: 100vh;
position: sticky;
top: 0;
}
.top-row {
position: sticky;
top: 0;
z-index: 1;
}
.top-row.auth ::deep a:first-child {
flex: 1;
text-align: right;
width: 0;
}
.top-row, article {
padding-left: 2rem !important;
padding-right: 1.5rem !important;
}
}
#blazor-error-ui {
background: lightyellow;
bottom: 0;
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
display: none;
left: 0;
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
position: fixed;
width: 100%;
z-index: 1000;
}
#blazor-error-ui .dismiss {
cursor: pointer;
position: absolute;
right: 0.75rem;
top: 0.5rem;
}

View File

@ -0,0 +1,36 @@
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">BlazorApp</a>
</div>
</div>
<input type="checkbox" title="Navigation menu" class="navbar-toggler" />
<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()">
<nav class="flex-column">
<div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="weather">
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Weather
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="customers">
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Customers
</NavLink>
</div>
</nav>
</div>

View File

@ -0,0 +1,105 @@
.navbar-toggler {
appearance: none;
cursor: pointer;
width: 3.5rem;
height: 2.5rem;
color: white;
position: absolute;
top: 0.5rem;
right: 1rem;
border: 1px solid rgba(255, 255, 255, 0.1);
background: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%28255, 255, 255, 0.55%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e") no-repeat center/1.75rem rgba(255, 255, 255, 0.1);
}
.navbar-toggler:checked {
background-color: rgba(255, 255, 255, 0.5);
}
.top-row {
height: 3.5rem;
background-color: rgba(0,0,0,0.4);
}
.navbar-brand {
font-size: 1.1rem;
}
.bi {
display: inline-block;
position: relative;
width: 1.25rem;
height: 1.25rem;
margin-right: 0.75rem;
top: -1px;
background-size: cover;
}
.bi-house-door-fill-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-house-door-fill' viewBox='0 0 16 16'%3E%3Cpath d='M6.5 14.5v-3.505c0-.245.25-.495.5-.495h2c.25 0 .5.25.5.5v3.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5v-7a.5.5 0 0 0-.146-.354L13 5.793V2.5a.5.5 0 0 0-.5-.5h-1a.5.5 0 0 0-.5.5v1.293L8.354 1.146a.5.5 0 0 0-.708 0l-6 6A.5.5 0 0 0 1.5 7.5v7a.5.5 0 0 0 .5.5h4a.5.5 0 0 0 .5-.5Z'/%3E%3C/svg%3E");
}
.bi-plus-square-fill-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-plus-square-fill' viewBox='0 0 16 16'%3E%3Cpath d='M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2zm6.5 4.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3a.5.5 0 0 1 1 0z'/%3E%3C/svg%3E");
}
.bi-list-nested-nav-menu {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='white' class='bi bi-list-nested' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M4.5 11.5A.5.5 0 0 1 5 11h10a.5.5 0 0 1 0 1H5a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 3 7h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm-2-4A.5.5 0 0 1 1 3h10a.5.5 0 0 1 0 1H1a.5.5 0 0 1-.5-.5z'/%3E%3C/svg%3E");
}
.nav-item {
font-size: 0.9rem;
padding-bottom: 0.5rem;
}
.nav-item:first-of-type {
padding-top: 1rem;
}
.nav-item:last-of-type {
padding-bottom: 1rem;
}
.nav-item ::deep .nav-link {
color: #d7d7d7;
background: none;
border: none;
border-radius: 4px;
height: 3rem;
display: flex;
align-items: center;
line-height: 3rem;
width: 100%;
}
.nav-item ::deep a.active {
background-color: rgba(255,255,255,0.37);
color: white;
}
.nav-item ::deep .nav-link:hover {
background-color: rgba(255,255,255,0.1);
color: white;
}
.nav-scrollable {
display: none;
}
.navbar-toggler:checked ~ .nav-scrollable {
display: block;
}
@media (min-width: 641px) {
.navbar-toggler {
display: none;
}
.nav-scrollable {
/* Never collapse the sidebar for wide screens */
display: block;
/* Allow sidebar to scroll for tall menus */
height: calc(100vh - 3.5rem);
overflow-y: auto;
}
}

View File

@ -0,0 +1,36 @@
@page "/Error"
@using System.Diagnostics
<PageTitle>Error</PageTitle>
<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
@if (ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@RequestId</code>
</p>
}
<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
and restarting the app.
</p>
@code{
[CascadingParameter]
private HttpContext? HttpContext { get; set; }
private string? RequestId { get; set; }
private bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
protected override void OnInitialized() =>
RequestId = Activity.Current?.Id ?? HttpContext?.TraceIdentifier;
}

View File

@ -0,0 +1,7 @@
@page "/"
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.

View File

@ -0,0 +1,53 @@
@page "/weather"
@using BlazorApp.Data
@using BlazorApp.Shared.Models
@attribute [StreamRendering]
@inject WeatherForecastService ForecastService
<PageTitle>Weather</PageTitle>
<h1>Weather</h1>
<p>This component demonstrates showing data.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500);
forecasts = await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
}
}

View File

@ -0,0 +1,6 @@
<Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(Client._Imports).Assembly }">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
</Router>

View File

@ -0,0 +1,11 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using BlazorApp
@using BlazorApp.Client
@using BlazorApp.Components

View File

@ -0,0 +1,54 @@
using BlazorApp.Interfaces.Services;
using BlazorApp.Shared.Models;
using BlazorApp.Shared.Models.Pagination;
using BlazorApp.Shared.Queries;
using Microsoft.AspNetCore.Mvc;
namespace BlazorApp.Controllers
{
[Route("customer")]
public class CustomerController : ControllerBase
{
private readonly ICustomerService _customerService;
public CustomerController(ICustomerService customerService)
{
this._customerService = customerService;
}
[HttpPost("query")]
public async Task<ActionResult<PaginatedResult<Customer>>> Query([FromBody] CustomerQuery query)
{
PaginatedResult<Customer> result = await this._customerService.Query(query);
return Ok(result);
}
[HttpGet("{id}")]
public async Task<ActionResult<Customer>> Get([FromRoute] string id)
{
Customer result = await this._customerService.Get(id);
return Ok(result);
}
[HttpPost("save")]
public async Task<ActionResult> Save([FromBody] Customer customer)
{
await this._customerService.Save(customer);
return NoContent();
}
[HttpPatch("update")]
public async Task<ActionResult> Update([FromBody] Customer customer)
{
await this._customerService.Update(customer);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<ActionResult> Delete([FromRoute] string id)
{
await this._customerService.Delete(id);
return NoContent();
}
}
}

View File

@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;
namespace BlazorApp.Data.Context
{
public class ApplicationDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}
}
}

View File

@ -0,0 +1,47 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BlazorApp.Data
{
[Table("Customer")]
public class Customer
{
[Key]
[Required]
[MaxLength(50)]
[Column("Id")]
public string? Id { get; set; }
[MaxLength(250)]
[Column("CompanyName")]
public string? CompanyName { get; set; }
[MaxLength(250)]
[Column("ContactName")]
public string? ContactName { get; set; }
[MaxLength(250)]
[Column("Address")]
public string? Address { get; set; }
[MaxLength(250)]
[Column("City")]
public string? City { get; set; }
[MaxLength(250)]
[Column("Region")]
public string? Region { get; set; }
[MaxLength(250)]
[Column("PostalCode")]
public string? PostalCode { get; set; }
[MaxLength(250)]
[Column("Country")]
public string? Country { get; set; }
[MaxLength(250)]
[Column("Phone")]
public string? Phone { get; set; }
}
}

68
BlazorApp/Extensions.cs Normal file
View File

@ -0,0 +1,68 @@
using BlazorApp.Data.Context;
using BlazorApp.Shared.Models;
using Microsoft.EntityFrameworkCore;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace BlazorApp
{
public static class Extensions
{
public static WebApplication ExecuteDbMigration(this WebApplication app)
{
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}
return app;
}
public static async void BootstrapMockCustomers(this WebApplication app)
{
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
bool.TryParse(app.Configuration.GetSection("MockCustomers").Value, out bool shouldMockCustomers);
if (shouldMockCustomers && await db.Customers.CountAsync() == 0)
{
int.TryParse(app.Configuration.GetSection("MockCustomersCount").Value, out int mockCustomersCount);
var indexes = Enumerable.Range(0, mockCustomersCount).ToList();
Guid[] orderedIds = indexes.Select(i => Guid.NewGuid()).OrderBy(x => x).ToArray();
foreach (var i in indexes)
{
Data.Customer customer = new Data.Customer();
customer.Id = orderedIds[i].ToString();
customer.CompanyName = $"{nameof(customer.CompanyName)}{i + 1}";
customer.ContactName = $"{nameof(customer.ContactName)}{i + 1}";
customer.Address = $"{nameof(customer.Address)}{i + 1}";
customer.City = $"{nameof(customer.City)}{i + 1}";
customer.Region = $"{nameof(customer.Region)}{i + 1}";
customer.PostalCode = $"{nameof(customer.PostalCode)}{i + 1}";
customer.Country = $"{nameof(customer.Country)}{i + 1}";
customer.Phone = $"{nameof(customer.Phone)}{i + 1}";
await db.AddAsync(customer);
}
await db.SaveChangesAsync();
}
}
}
public static Customer Convert(this Data.Customer data)
{
Customer model = new Customer();
model.Id = data.Id;
model.CompanyName = data.CompanyName;
model.ContactName = data.ContactName;
model.Address = data.Address;
model.City = data.City;
model.Region = data.Region;
model.PostalCode = data.PostalCode;
model.Country = data.Country;
model.Phone = data.Phone;
return model;
}
}
}

View File

@ -0,0 +1,15 @@
using BlazorApp.Data;
using BlazorApp.Shared.Queries;
namespace BlazorApp.Interfaces.Repositories
{
public interface ICustomerRepository
{
Task<IEnumerable<Customer>> Query(CustomerQuery query);
Task<Customer> Get(string id);
Task Save(Shared.Models.Customer customer);
Task Update(Shared.Models.Customer customer);
Task Delete(string id);
Task<int> Count();
}
}

View File

@ -0,0 +1,16 @@
using BlazorApp.Shared.Models;
using BlazorApp.Shared.Models.Pagination;
using BlazorApp.Shared.Queries;
namespace BlazorApp.Interfaces.Services
{
public interface ICustomerService
{
Task<PaginatedResult<Customer>> Query(CustomerQuery query);
Task<Customer> Get(string id);
Task Save(Customer customer);
Task Update(Customer customer);
Task Delete(string id);
Task<int> Count();
}
}

View File

@ -0,0 +1,81 @@
// <auto-generated />
using BlazorApp.Data.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BlazorApp.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250919054731_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.13")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BlazorApp.Data.Customer", b =>
{
b.Property<string>("Id")
.HasMaxLength(50)
.HasColumnType("nvarchar(50)")
.HasColumnName("Id");
b.Property<string>("Address")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Address");
b.Property<string>("City")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("City");
b.Property<string>("CompanyName")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("CompanyName");
b.Property<string>("ContactName")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("ContactName");
b.Property<string>("Country")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Country");
b.Property<string>("Phone")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Phone");
b.Property<string>("PostalCode")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("PostalCode");
b.Property<string>("Region")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Region");
b.HasKey("Id");
b.ToTable("Customer");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BlazorApp.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Customer",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
CompanyName = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
ContactName = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
Address = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
City = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
Region = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
PostalCode = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
Country = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
Phone = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Customer", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Customer");
}
}
}

View File

@ -0,0 +1,78 @@
// <auto-generated />
using BlazorApp.Data.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BlazorApp.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.13")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BlazorApp.Data.Customer", b =>
{
b.Property<string>("Id")
.HasMaxLength(50)
.HasColumnType("nvarchar(50)")
.HasColumnName("Id");
b.Property<string>("Address")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Address");
b.Property<string>("City")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("City");
b.Property<string>("CompanyName")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("CompanyName");
b.Property<string>("ContactName")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("ContactName");
b.Property<string>("Country")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Country");
b.Property<string>("Phone")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Phone");
b.Property<string>("PostalCode")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("PostalCode");
b.Property<string>("Region")
.HasMaxLength(250)
.HasColumnType("nvarchar(250)")
.HasColumnName("Region");
b.HasKey("Id");
b.ToTable("Customer");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,9 @@
namespace BlazorApp.Models.Config
{
public class InMemoryCacheConfiguration
{
public bool Enabled { get; set; }
public string CustomerCacheKey { get; set; }
public int CustomerCacheSeconds { get; set; }
}
}

70
BlazorApp/Program.cs Normal file
View File

@ -0,0 +1,70 @@
using BlazorApp;
using BlazorApp.Client;
using BlazorApp.Components;
using BlazorApp.Data.Context;
using BlazorApp.Interfaces.Repositories;
using BlazorApp.Interfaces.Services;
using BlazorApp.Models.Config;
using BlazorApp.Repositories.Database;
using BlazorApp.Repositories.InMemory;
using BlazorApp.Services;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddBlazorBootstrap();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("ApplicationDB")));
builder.Services.AddControllers();
builder.Services.AddMemoryCache();
InMemoryCacheConfiguration inMemoryCacheConfiguration = new InMemoryCacheConfiguration();
builder.Configuration.GetSection(nameof(InMemoryCacheConfiguration)).Bind(inMemoryCacheConfiguration);
builder.Services.AddSingleton(inMemoryCacheConfiguration);
builder.Services.AddScoped<ICustomerService, CustomerService>();
builder.Services.AddScoped<ICustomerRepository, CustomerDatabaseRepository>();
if (inMemoryCacheConfiguration.Enabled)
{
builder.Services.Decorate<ICustomerRepository, CustomerInMemoryRepository>();
}
builder.Services.AddClientServices();
var app = builder.Build();
app.ExecuteDbMigration();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.MapControllers();
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(BlazorApp.Client._Imports).Assembly);
app.BootstrapMockCustomers();
app.Run();

View File

@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63615",
"sslPort": 44345
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5217",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7018;http://localhost:5217",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,83 @@
using BlazorApp.Data;
using BlazorApp.Data.Context;
using BlazorApp.Interfaces.Repositories;
using BlazorApp.Shared.Queries;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace BlazorApp.Repositories.Database
{
public class CustomerDatabaseRepository : ICustomerRepository
{
private readonly ApplicationDbContext _dbContext;
public CustomerDatabaseRepository(ApplicationDbContext dbContext)
{
this._dbContext = dbContext;
}
public async Task Delete(string id)
{
Customer data = await this.Get(id);
this._dbContext.Customers.Remove(data);
await this._dbContext.SaveChangesAsync();
}
public async Task<Customer> Get(string id)
{
Customer data = await this._dbContext.Customers.FindAsync(id);
if (data is null)
{
throw new KeyNotFoundException($"Customer with id: {id} not found");
}
return data;
}
public async Task<IEnumerable<Customer>> Query(CustomerQuery query)
{
return await this._dbContext.Customers
.OrderBy(x => x.Id)
.Skip(query.PageIndex * query.ResultsPerPage)
.Take(query.ResultsPerPage)
.ToListAsync();
}
public async Task Save(Shared.Models.Customer model)
{
if (!string.IsNullOrWhiteSpace(model.Id))
{
throw new ValidationException($"Customer has already id: {model.Id}.");
}
Customer data = new Customer()
{
Id = Guid.NewGuid().ToString()
};
data.CompanyName = model.CompanyName;
data.ContactName = model.ContactName;
data.Address = model.Address;
data.City = model.City;
data.Region = model.Region;
data.PostalCode = model.PostalCode;
data.Country = model.Country;
data.Phone = model.Phone;
await this._dbContext.AddAsync(data);
await this._dbContext.SaveChangesAsync();
}
public async Task Update(Shared.Models.Customer model)
{
Customer data = await this.Get(model.Id);
data.CompanyName = model.CompanyName;
data.ContactName = model.ContactName;
data.Address = model.Address;
data.City = model.City;
data.Region = model.Region;
data.PostalCode = model.PostalCode;
data.Country = model.Country;
data.Phone = model.Phone;
this._dbContext.Update(data);
await this._dbContext.SaveChangesAsync();
}
public async Task<int> Count() => await this._dbContext.Customers.CountAsync();
}
}

View File

@ -0,0 +1,28 @@
using Microsoft.Extensions.Caching.Memory;
namespace BlazorApp.Repositories.InMemory
{
public class BaseInMemoryRepository
{
protected readonly IMemoryCache _cache;
protected BaseInMemoryRepository(IMemoryCache cache)
{
this._cache = cache;
}
protected T GetFromCache<T>(string cacheKey)
{
return _cache.TryGetValue(cacheKey, out T entity) ? entity : default;
}
protected void SetToCache<T>(string cacheKey, T entity, TimeSpan duration)
{
_cache.Set(cacheKey, entity, duration);
}
protected void InvalidateCache(string cacheKey)
{
this._cache.Remove(cacheKey);
}
}
}

View File

@ -0,0 +1,61 @@
using BlazorApp.Data;
using BlazorApp.Interfaces.Repositories;
using BlazorApp.Models.Config;
using BlazorApp.Shared.Queries;
using Microsoft.Extensions.Caching.Memory;
namespace BlazorApp.Repositories.InMemory
{
public class CustomerInMemoryRepository : BaseInMemoryRepository, ICustomerRepository
{
private readonly ICustomerRepository _inner;
private readonly InMemoryCacheConfiguration _inMemoryCacheConfig;
public CustomerInMemoryRepository(ICustomerRepository inner, IMemoryCache cache, InMemoryCacheConfiguration inMemoryCacheConfig) : base(cache)
{
this._inner = inner;
this._inMemoryCacheConfig = inMemoryCacheConfig;
}
public async Task Delete(string id)
{
await _inner.Delete(id);
this.InvalidateCache($"{this._inMemoryCacheConfig.CustomerCacheKey}_{id}");
}
public async Task<Customer> Get(string id)
{
string cacheKey = $"{this._inMemoryCacheConfig.CustomerCacheKey}_{id}";
Customer data = this.GetFromCache<Customer>(cacheKey);
if (data is null)
{
data = await _inner.Get(id);
if (data is not null)
{
_cache.Set(cacheKey, data, TimeSpan.FromSeconds(this._inMemoryCacheConfig.CustomerCacheSeconds));
}
}
return data;
}
public async Task<IEnumerable<Customer>> Query(CustomerQuery query)
{
return await this._inner.Query(query);
}
public async Task Save(Shared.Models.Customer model)
{
await this._inner.Save(model);
}
public async Task Update(Shared.Models.Customer model)
{
await this._inner.Update(model);
this.InvalidateCache($"{this._inMemoryCacheConfig.CustomerCacheKey}_{model.Id}");
}
public async Task<int> Count() => await this._inner.Count();
}
}

View File

@ -0,0 +1,50 @@
using BlazorApp.Interfaces.Repositories;
using BlazorApp.Interfaces.Services;
using BlazorApp.Shared.Models;
using BlazorApp.Shared.Models.Pagination;
using BlazorApp.Shared.Queries;
namespace BlazorApp.Services
{
public class CustomerService : ICustomerService
{
private readonly ICustomerRepository _customerRepository;
public CustomerService(ICustomerRepository customerRepository)
{
this._customerRepository = customerRepository;
}
public async Task Delete(string id)
{
await this._customerRepository.Delete(id);
}
public async Task<Customer> Get(string id)
{
Data.Customer data = await this._customerRepository.Get(id);
return data.Convert();
}
public async Task<PaginatedResult<Customer>> Query(CustomerQuery query)
{
IEnumerable<Data.Customer> datas = await this._customerRepository.Query(query);
return new PaginatedResult<Customer>()
{
Results = datas.Select(x => x.Convert()),
TotalCount = await this.Count()
};
}
public async Task Save(Customer customer)
{
await this._customerRepository.Save(customer);
}
public async Task Update(Customer customer)
{
await this._customerRepository.Update(customer);
}
public async Task<int> Count() => await this._customerRepository.Count();
}
}

View File

@ -0,0 +1,19 @@
{
"APIBaseAddress": "https://localhost:7018/",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"ApplicationDB": "Server=localhost;Database=BlazorApp;Trusted_Connection=True;TrustServerCertificate=True;"
},
"InMemoryCacheConfiguration": {
"Enabled": true,
"CustomerCacheKey": "customer",
"CustomerCacheSeconds": 5
},
"MockCustomers": true,
"MockCustomersCount": 238
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

51
BlazorApp/wwwroot/app.css Normal file
View File

@ -0,0 +1,51 @@
html, body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
a, .btn-link {
color: #006bb7;
}
.btn-primary {
color: #fff;
background-color: #1b6ec2;
border-color: #1861ac;
}
.btn:focus, .btn:active:focus, .btn-link.nav-link:focus, .form-control:focus, .form-check-input:focus {
box-shadow: 0 0 0 0.1rem white, 0 0 0 0.25rem #258cfb;
}
.content {
padding-top: 1.1rem;
}
h1:focus {
outline: none;
}
.valid.modified:not([type=checkbox]) {
outline: 1px solid #26b050;
}
.invalid {
outline: 1px solid #e50000;
}
.validation-message {
color: #e50000;
}
.blazor-error-boundary {
background: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTYiIGhlaWdodD0iNDkiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIG92ZXJmbG93PSJoaWRkZW4iPjxkZWZzPjxjbGlwUGF0aCBpZD0iY2xpcDAiPjxyZWN0IHg9IjIzNSIgeT0iNTEiIHdpZHRoPSI1NiIgaGVpZ2h0PSI0OSIvPjwvY2xpcFBhdGg+PC9kZWZzPjxnIGNsaXAtcGF0aD0idXJsKCNjbGlwMCkiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0yMzUgLTUxKSI+PHBhdGggZD0iTTI2My41MDYgNTFDMjY0LjcxNyA1MSAyNjUuODEzIDUxLjQ4MzcgMjY2LjYwNiA1Mi4yNjU4TDI2Ny4wNTIgNTIuNzk4NyAyNjcuNTM5IDUzLjYyODMgMjkwLjE4NSA5Mi4xODMxIDI5MC41NDUgOTIuNzk1IDI5MC42NTYgOTIuOTk2QzI5MC44NzcgOTMuNTEzIDI5MSA5NC4wODE1IDI5MSA5NC42NzgyIDI5MSA5Ny4wNjUxIDI4OS4wMzggOTkgMjg2LjYxNyA5OUwyNDAuMzgzIDk5QzIzNy45NjMgOTkgMjM2IDk3LjA2NTEgMjM2IDk0LjY3ODIgMjM2IDk0LjM3OTkgMjM2LjAzMSA5NC4wODg2IDIzNi4wODkgOTMuODA3MkwyMzYuMzM4IDkzLjAxNjIgMjM2Ljg1OCA5Mi4xMzE0IDI1OS40NzMgNTMuNjI5NCAyNTkuOTYxIDUyLjc5ODUgMjYwLjQwNyA1Mi4yNjU4QzI2MS4yIDUxLjQ4MzcgMjYyLjI5NiA1MSAyNjMuNTA2IDUxWk0yNjMuNTg2IDY2LjAxODNDMjYwLjczNyA2Ni4wMTgzIDI1OS4zMTMgNjcuMTI0NSAyNTkuMzEzIDY5LjMzNyAyNTkuMzEzIDY5LjYxMDIgMjU5LjMzMiA2OS44NjA4IDI1OS4zNzEgNzAuMDg4N0wyNjEuNzk1IDg0LjAxNjEgMjY1LjM4IDg0LjAxNjEgMjY3LjgyMSA2OS43NDc1QzI2Ny44NiA2OS43MzA5IDI2Ny44NzkgNjkuNTg3NyAyNjcuODc5IDY5LjMxNzkgMjY3Ljg3OSA2Ny4xMTgyIDI2Ni40NDggNjYuMDE4MyAyNjMuNTg2IDY2LjAxODNaTTI2My41NzYgODYuMDU0N0MyNjEuMDQ5IDg2LjA1NDcgMjU5Ljc4NiA4Ny4zMDA1IDI1OS43ODYgODkuNzkyMSAyNTkuNzg2IDkyLjI4MzcgMjYxLjA0OSA5My41Mjk1IDI2My41NzYgOTMuNTI5NSAyNjYuMTE2IDkzLjUyOTUgMjY3LjM4NyA5Mi4yODM3IDI2Ny4zODcgODkuNzkyMSAyNjcuMzg3IDg3LjMwMDUgMjY2LjExNiA4Ni4wNTQ3IDI2My41NzYgODYuMDU0N1oiIGZpbGw9IiNGRkU1MDAiIGZpbGwtcnVsZT0iZXZlbm9kZCIvPjwvZz48L3N2Zz4=) no-repeat 1rem/1.8rem, #b32121;
padding: 1rem 1rem 1rem 3.7rem;
color: white;
}
.blazor-error-boundary::after {
content: "An error has occurred."
}
.darker-border-checkbox.form-check-input {
border-color: #929292;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

10
Bullet6/Bullet6.csproj Normal file
View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,9 @@
using Bullet6.Interfaces;
namespace Bullet6.Handlers
{
public static class EntityHandler
{
public static void PrintName(IEntity entity) => Console.WriteLine(entity.Name);
}
}

View File

@ -0,0 +1,7 @@
namespace Bullet6.Interfaces
{
public interface IEntity
{
string Name { get; set; }
}
}

View File

@ -0,0 +1,9 @@
using Bullet6.Interfaces;
namespace Bullet6.Models
{
public class Employee : IEntity
{
public string Name { get; set; }
}
}

View File

@ -0,0 +1,9 @@
using Bullet6.Interfaces;
namespace Bullet6.Models
{
public class Manager : IEntity
{
public string Name { get; set; }
}
}

15
Bullet6/Program.cs Normal file
View File

@ -0,0 +1,15 @@
using Bullet6.Handlers;
using Bullet6.Interfaces;
using Bullet6.Models;
namespace Bullet6
{
public class Program
{
static void Main(string[] args)
{
IEntity[] entites = [new Employee() { Name = "Ilias" }, new Manager() { Name = "Maria" }];
foreach (var entity in entites) EntityHandler.PrintName(entity);
}
}
}

View File

@ -1,3 +1,42 @@
# BlazorApp # Description
Epsilon BlazorApp You are given a solution that contains a Blazor Web App with a Customer model class.
You should fork this project and provide a github link for your solution.
You have to develop
Required:
- A grid with all customers with server side paging
- CRUD Operations on “Customer” model with new, edit and delete functionalities
- Expose all CRUD Operations as an API
- Configure application to use Sql Server
- Manage migrations
- Below are the two classes Employee and Manager. Your task is to create a method in a new class that takes either Manager or an Employee as a parameter and prints its name.
```
public class Employee
{
public string Name { get; set; }
}
public class Manager
{
public string Name { get; set; }
}
```
Extra (nice to have)
- Add authentication with the provided demo of Duende IdentityServer https://demo.duendesoftware.com/
- Protect your API with authentication with the provided demo of Duende IdentityServer done in the previous step
- Unit & Integration Tests
## Requirements
- C#
- .NET 8+
- Blazor Wasm
Optional
- Blazor UI framework

6
renovate.json Normal file
View File

@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
}