master
Jade VAN BRABANDT 1 year ago
parent 39ec338631
commit eb06a358c4

Binary file not shown.

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TpBlazorr2", "TpBlazorr2\TpBlazorr2.csproj", "{C4F1AB88-1FA8-42A6-919A-8581EBFC98D2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TpBlazorr2", "TpBlazorr2\TpBlazorr2.csproj", "{C4F1AB88-1FA8-42A6-919A-8581EBFC98D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

@ -0,0 +1,37 @@
using System.ComponentModel.DataAnnotations;
namespace TpBlazorr2.Models
{
public class ItemModel
{
public int Id { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The display name must not exceed 50 characters.")]
public string DisplayName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The name must not exceed 50 characters.")]
[RegularExpression(@"^[a-z''-'\s]{1,50}$", ErrorMessage = "Only lowercase characters are accepted.")]
public string Name { get; set; }
[Required]
[Range(1, 64)]
public int StackSize { get; set; }
[Required]
[Range(1, 125)]
public int MaxDurability { get; set; }
public List<string> EnchantCategories { get; set; }
public List<string> RepairWith { get; set; }
[Required]
[Range(typeof(bool), "true", "true", ErrorMessage = "You must agree to the terms.")]
public bool AcceptCondition { get; set; }
[Required(ErrorMessage = "The image of the item is mandatory!")]
public byte[] ImageContent { get; set; }
}
}

@ -0,0 +1,70 @@
@page "/add"
@using TpBlazorr2.Models;
<h3>Add</h3>
<EditForm Model="@itemModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label for="display-name">
Display name:
<InputText id="display-name" @bind-Value="itemModel.DisplayName" />
</label>
</p>
<p>
<label for="name">
Name:
<InputText id="name" @bind-Value="itemModel.Name" />
</label>
</p>
<p>
<label for="stack-size">
Stack size:
<InputNumber id="stack-size" @bind-Value="itemModel.StackSize" />
</label>
</p>
<p>
<label for="max-durability">
Max durability:
<InputNumber id="max-durability" @bind-Value="itemModel.MaxDurability" />
</label>
</p>
<p>
Enchant categories:
<div>
@foreach (var item in enchantCategories)
{
<label>
<input type="checkbox" @onchange="@(e => OnEnchantCategoriesChange(item, e.Value))" />@item
</label>
}
</div>
</p>
<p>
Repair with:
<div>
@foreach (var item in repairWith)
{
<label>
<input type="checkbox" @onchange="@(e => OnRepairWithChange(item, e.Value))" />@item
</label>
}
</div>
</p>
<p>
<label>
Item image:
<InputFile OnChange="@LoadImage" accept=".png" />
</label>
</p>
<p>
<label>
Accept Condition:
<InputCheckbox @bind-Value="itemModel.AcceptCondition" />
</label>
</p>
<button type="submit">Submit</button>
</EditForm>

@ -0,0 +1,127 @@
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using TpBlazorr2.Models;
namespace TpBlazorr2.Pages
{
public partial class Add
{
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public ILocalStorageService LocalStorage { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
/// <summary>
/// The default enchant categories.
/// </summary>
private List<string> enchantCategories = new List<string>() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" };
/// <summary>
/// The default repair with.
/// </summary>
private List<string> repairWith = new List<string>() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" };
/// <summary>
/// The current item model
/// </summary>
private ItemModel itemModel = new()
{
EnchantCategories = new List<string>(),
RepairWith = new List<string>()
};
private async void HandleValidSubmit()
{
// Get the current data
var currentData = await LocalStorage.GetItemAsync<List<Item>>("data");
// Simulate the Id
itemModel.Id = currentData.Max(s => s.Id) + 1;
// Add the item to the current data
currentData.Add(new Item
{
Id = itemModel.Id,
DisplayName = itemModel.DisplayName,
Name = itemModel.Name,
RepairWith = itemModel.RepairWith,
EnchantCategories = itemModel.EnchantCategories,
MaxDurability = itemModel.MaxDurability,
StackSize = itemModel.StackSize,
CreatedDate = DateTime.Now
});
// Save the image
var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images");
// Check if the folder "images" exist
if (!imagePathInfo.Exists)
{
imagePathInfo.Create();
}
// Determine the image name
var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
// Write the file content
await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
// Save the data
await LocalStorage.SetItemAsync("data", currentData);
NavigationManager.NavigateTo("list");
}
private async Task LoadImage(InputFileChangeEventArgs e)
{
// Set the content of the image to the model
using (var memoryStream = new MemoryStream())
{
await e.File.OpenReadStream().CopyToAsync(memoryStream);
itemModel.ImageContent = memoryStream.ToArray();
}
}
private void OnEnchantCategoriesChange(string item, object checkedValue)
{
if ((bool)checkedValue)
{
if (!itemModel.EnchantCategories.Contains(item))
{
itemModel.EnchantCategories.Add(item);
}
return;
}
if (itemModel.EnchantCategories.Contains(item))
{
itemModel.EnchantCategories.Remove(item);
}
}
private void OnRepairWithChange(string item, object checkedValue)
{
if ((bool)checkedValue)
{
if (!itemModel.RepairWith.Contains(item))
{
itemModel.RepairWith.Add(item);
}
return;
}
if (itemModel.RepairWith.Contains(item))
{
itemModel.RepairWith.Remove(item);
}
}
}
}

@ -1,7 +1,11 @@
@page "/liste"
@using TpBlazorr2.Models;
<h3>Liste</h3>
<div>
<NavLink class="btn btn-primary" href="Add" Match="NavLinkMatch.All">
<i class="fa fa-plus"></i> Ajouter
</NavLink>
</div>
<DataGrid TItem="Item"
Data="@items"
ReadData="@OnReadData"
@ -10,6 +14,18 @@
ShowPager
Responsive>
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" />
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image">
<DisplayTemplate>
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png"))
{
<img src="images/@(context.Name).png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" />
}
else
{
<img src="images/default.png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" />
}
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name" />
<DataGridColumn TItem="Item" Field="@nameof(Item.StackSize)" Caption="Stack size" />
<DataGridColumn TItem="Item" Field="@nameof(Item.MaxDurability)" Caption="Maximum durability" />

@ -1,6 +1,8 @@
using Blazored.LocalStorage;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
using TpBlazorr2.Models;
namespace TpBlazorr2.Pages;
public partial class Liste
{
@ -8,12 +10,37 @@ public partial class Liste
private int totalItem;
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public HttpClient Http { get; set; }
[Inject]
public ILocalStorageService LocalStorage { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Do not treat this action if is not the first render
if (!firstRender)
{
return;
}
var currentData = await LocalStorage.GetItemAsync<Item[]>("data");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
var originalData = Http.GetFromJsonAsync<Item[]>($"{NavigationManager.BaseUri}fake-data.json").Result;
await LocalStorage.SetItemAsync("data", originalData);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
{
if (e.CancellationToken.IsCancellationRequested)
@ -22,12 +49,12 @@ public partial class Liste
}
// When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Item[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
var response = (await Http.GetFromJsonAsync<Item[]>($"{NavigationManager.BaseUri}fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
//var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
var response = (await LocalStorage.GetItemAsync<Item[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested)
{
totalItem = (await Http.GetFromJsonAsync<List<Item>>($"{NavigationManager.BaseUri}fake-data.json")).Count;
totalItem = (await LocalStorage.GetItemAsync<List<Item>>("data")).Count;
items = new List<Item>(response); // an actual data for the current page
}
}

@ -1,3 +1,4 @@
using Blazored.LocalStorage;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
@ -16,7 +17,7 @@ builder.Services
.AddBlazorise()
.AddBootstrapProviders()
.AddFontAwesomeIcons();
builder.Services.AddBlazoredLocalStorage();
var app = builder.Build();
// Configure the HTTP request pipeline.

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="Blazorise.Bootstrap" Version="1.3.3" />
<PackageReference Include="Blazorise.DataGrid" Version="1.3.3" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.3.3" />

@ -2,5 +2,7 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>https</ActiveDebugProfile>
<RazorPage_SelectedScaffolderID>RazorPageScaffolder</RazorPage_SelectedScaffolderID>
<RazorPage_SelectedScaffolderCategoryPath>root/Common/RazorPage</RazorPage_SelectedScaffolderCategoryPath>
</PropertyGroup>
</Project>

@ -9,3 +9,4 @@
@using TpBlazorr2
@using TpBlazorr2.Shared
@using Blazorise.DataGrid
@using Blazored.LocalStorage;

@ -8,6 +8,7 @@
".NETCoreApp,Version=v7.0": {
"TpBlazorr2/1.0.0": {
"dependencies": {
"Blazored.LocalStorage": "4.4.0",
"Blazorise.Bootstrap": "1.3.3",
"Blazorise.DataGrid": "1.3.3",
"Blazorise.Icons.FontAwesome": "1.3.3"
@ -16,6 +17,17 @@
"TpBlazorr2.dll": {}
}
},
"Blazored.LocalStorage/4.4.0": {
"dependencies": {
"Microsoft.AspNetCore.Components.Web": "7.0.14"
},
"runtime": {
"lib/net7.0/Blazored.LocalStorage.dll": {
"assemblyVersion": "1.0.0.0",
"fileVersion": "1.0.0.0"
}
}
},
"Blazorise/1.3.3": {
"dependencies": {
"Blazorise.Licensing": "1.1.0",
@ -862,6 +874,13 @@
"serviceable": false,
"sha512": ""
},
"Blazored.LocalStorage/4.4.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-zuXZB4/WW3Pr1761peemffdkjt09lbOP1wAkSDTKl7BTbA9V5e8LxS6MNfmyHW+BJzXrNDq90E2Y+AfIfnIbWQ==",
"path": "blazored.localstorage/4.4.0",
"hashPath": "blazored.localstorage.4.4.0.nupkg.sha512"
},
"Blazorise/1.3.3": {
"type": "package",
"serviceable": true,

File diff suppressed because one or more lines are too long

@ -20,6 +20,10 @@ build_property._RazorSourceGeneratorDebug =
build_metadata.AdditionalFiles.TargetPath = QXBwLnJhem9y
build_metadata.AdditionalFiles.CssScope =
[C:/Users/axvanbraba/source/repos/TpBlazorr2/TpBlazorr2/Pages/Add.razor]
build_metadata.AdditionalFiles.TargetPath = UGFnZXNcQWRkLnJhem9y
build_metadata.AdditionalFiles.CssScope =
[C:/Users/axvanbraba/source/repos/TpBlazorr2/TpBlazorr2/Pages/Counter.razor]
build_metadata.AdditionalFiles.TargetPath = UGFnZXNcQ291bnRlci5yYXpvcg==
build_metadata.AdditionalFiles.CssScope =

@ -1 +1 @@
b14e5be04649095aa4ad28b0ad6475ac235be70e
85e5a5de048c6759c59ece14ed12455dbccae3d1

@ -45,3 +45,4 @@ C:\Users\axvanbraba\source\repos\TpBlazorr2\TpBlazorr2\bin\Debug\net7.0\Microsof
C:\Users\axvanbraba\source\repos\TpBlazorr2\TpBlazorr2\bin\Debug\net7.0\Microsoft.Extensions.Options.dll
C:\Users\axvanbraba\source\repos\TpBlazorr2\TpBlazorr2\bin\Debug\net7.0\Microsoft.JSInterop.dll
C:\Users\axvanbraba\source\repos\TpBlazorr2\TpBlazorr2\obj\Debug\net7.0\TpBlazorr2.csproj.CopyComplete
C:\Users\axvanbraba\source\repos\TpBlazorr2\TpBlazorr2\bin\Debug\net7.0\Blazored.LocalStorage.dll

File diff suppressed because one or more lines are too long

@ -1,6 +1,6 @@
{
"Version": 1,
"Hash": "r2QvDSyZpwKRL9LxSvYtOXn+9VwiXuiSd2pHMHETL4U=",
"Hash": "j40ss+Pli6JAfP7/2HeBKuQgGCtao/cJdkbu6NfihNc=",
"Source": "TpBlazorr2",
"BasePath": "_content/TpBlazorr2",
"Mode": "Default",
@ -984,6 +984,23 @@
"CopyToOutputDirectory": "Never",
"CopyToPublishDirectory": "PreserveNewest",
"OriginalItemSpec": "wwwroot\\favicon.png"
},
{
"Identity": "C:\\Users\\axvanbraba\\source\\repos\\TpBlazorr2\\TpBlazorr2\\wwwroot\\images\\default.png",
"SourceId": "TpBlazorr2",
"SourceType": "Discovered",
"ContentRoot": "C:\\Users\\axvanbraba\\source\\repos\\TpBlazorr2\\TpBlazorr2\\wwwroot\\",
"BasePath": "_content/TpBlazorr2",
"RelativePath": "images/default.png",
"AssetKind": "All",
"AssetMode": "All",
"AssetRole": "Primary",
"RelatedAsset": "",
"AssetTraitName": "",
"AssetTraitValue": "",
"CopyToOutputDirectory": "Never",
"CopyToPublishDirectory": "PreserveNewest",
"OriginalItemSpec": "wwwroot\\images\\default.png"
}
]
}

File diff suppressed because one or more lines are too long

@ -60,6 +60,10 @@
"Id": "C:\\Users\\axvanbraba\\source\\repos\\TpBlazorr2\\TpBlazorr2\\wwwroot\\favicon.png",
"PackagePath": "staticwebassets\\favicon.png"
},
{
"Id": "C:\\Users\\axvanbraba\\source\\repos\\TpBlazorr2\\TpBlazorr2\\wwwroot\\images\\default.png",
"PackagePath": "staticwebassets\\images\\default.png"
},
{
"Id": "obj\\Debug\\net7.0\\staticwebassets\\msbuild.TpBlazorr2.Microsoft.AspNetCore.StaticWebAssets.props",
"PackagePath": "build\\Microsoft.AspNetCore.StaticWebAssets.props"

@ -224,6 +224,22 @@
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<OriginalItemSpec>$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\staticwebassets\favicon.png))</OriginalItemSpec>
</StaticWebAsset>
<StaticWebAsset Include="$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\staticwebassets\images\default.png))">
<SourceType>Package</SourceType>
<SourceId>TpBlazorr2</SourceId>
<ContentRoot>$(MSBuildThisFileDirectory)..\staticwebassets\</ContentRoot>
<BasePath>_content/TpBlazorr2</BasePath>
<RelativePath>images/default.png</RelativePath>
<AssetKind>All</AssetKind>
<AssetMode>All</AssetMode>
<AssetRole>Primary</AssetRole>
<RelatedAsset></RelatedAsset>
<AssetTraitName></AssetTraitName>
<AssetTraitValue></AssetTraitValue>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<OriginalItemSpec>$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\staticwebassets\images\default.png))</OriginalItemSpec>
</StaticWebAsset>
<StaticWebAsset Include="$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\staticwebassets\TpBlazorr2.bundle.scp.css))">
<SourceType>Package</SourceType>
<SourceId>TpBlazorr2</SourceId>

@ -45,6 +45,10 @@
"net7.0": {
"targetAlias": "net7.0",
"dependencies": {
"Blazored.LocalStorage": {
"target": "Package",
"version": "[4.4.0, )"
},
"Blazorise.Bootstrap": {
"target": "Package",
"version": "[1.3.3, )"

@ -2,6 +2,18 @@
"version": 3,
"targets": {
"net7.0": {
"Blazored.LocalStorage/4.4.0": {
"type": "package",
"dependencies": {
"Microsoft.AspNetCore.Components.Web": "7.0.0"
},
"compile": {
"lib/net7.0/Blazored.LocalStorage.dll": {}
},
"runtime": {
"lib/net7.0/Blazored.LocalStorage.dll": {}
}
},
"Blazorise/1.3.3": {
"type": "package",
"dependencies": {
@ -1605,6 +1617,20 @@
}
},
"libraries": {
"Blazored.LocalStorage/4.4.0": {
"sha512": "zuXZB4/WW3Pr1761peemffdkjt09lbOP1wAkSDTKl7BTbA9V5e8LxS6MNfmyHW+BJzXrNDq90E2Y+AfIfnIbWQ==",
"type": "package",
"path": "blazored.localstorage/4.4.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"blazored.localstorage.4.4.0.nupkg.sha512",
"blazored.localstorage.nuspec",
"icon.png",
"lib/net6.0/Blazored.LocalStorage.dll",
"lib/net7.0/Blazored.LocalStorage.dll"
]
},
"Blazorise/1.3.3": {
"sha512": "fthikFkWQ65VkKzY7dt6FCFZFVbSmCVmYtOItfJrA9ZScICeU94uVivOv7MaWQboMP7ku/Ap7NKSgICrz4i1Pw==",
"type": "package",
@ -5356,6 +5382,7 @@
},
"projectFileDependencyGroups": {
"net7.0": [
"Blazored.LocalStorage >= 4.4.0",
"Blazorise.Bootstrap >= 1.3.3",
"Blazorise.DataGrid >= 1.3.3",
"Blazorise.Icons.FontAwesome >= 1.3.3"
@ -5406,6 +5433,10 @@
"net7.0": {
"targetAlias": "net7.0",
"dependencies": {
"Blazored.LocalStorage": {
"target": "Package",
"version": "[4.4.0, )"
},
"Blazorise.Bootstrap": {
"target": "Package",
"version": "[1.3.3, )"

@ -1,9 +1,10 @@
{
"version": 2,
"dgSpecHash": "g/3AMq7thBiOlU4ZsxsQGXgyziqhv6eY7rDbakr3PLKh3L6Gc1MQA7sH+yRnHWCBCnT/eubG7+thFWqQZtj/IQ==",
"dgSpecHash": "NRDx7QClM2rDU1qHR7CCVgjEL5l0OL4AgyT9p/5pGYc6VV5y/wIqXoCVUay/es6w5zJ+b9ERhO8C0PGG5fH2wQ==",
"success": true,
"projectFilePath": "C:\\Users\\axvanbraba\\source\\repos\\TpBlazorr2\\TpBlazorr2\\TpBlazorr2.csproj",
"expectedPackageFiles": [
"C:\\Users\\axvanbraba\\.nuget\\packages\\blazored.localstorage\\4.4.0\\blazored.localstorage.4.4.0.nupkg.sha512",
"C:\\Users\\axvanbraba\\.nuget\\packages\\blazorise\\1.3.3\\blazorise.1.3.3.nupkg.sha512",
"C:\\Users\\axvanbraba\\.nuget\\packages\\blazorise.bootstrap\\1.3.3\\blazorise.bootstrap.1.3.3.nupkg.sha512",
"C:\\Users\\axvanbraba\\.nuget\\packages\\blazorise.datagrid\\1.3.3\\blazorise.datagrid.1.3.3.nupkg.sha512",

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Loading…
Cancel
Save