diff --git a/.idea/.idea.TP Blazor/.idea/indexLayout.xml b/.idea/.idea.TP Blazor/.idea/indexLayout.xml
new file mode 100644
index 0000000..7b08163
--- /dev/null
+++ b/.idea/.idea.TP Blazor/.idea/indexLayout.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/.idea.TP Blazor/.idea/vcs.xml b/.idea/.idea.TP Blazor/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/.idea.TP Blazor/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.vscode/tasks.json b/.vscode/tasks.json
new file mode 100644
index 0000000..522c4a2
--- /dev/null
+++ b/.vscode/tasks.json
@@ -0,0 +1,41 @@
+{
+ "version": "2.0.0",
+ "tasks": [
+ {
+ "label": "build",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "build",
+ "${workspaceFolder}/TP Blazor/TP Blazor.csproj",
+ "/property:GenerateFullPaths=true",
+ "/consoleloggerparameters:NoSummary"
+ ],
+ "problemMatcher": "$msCompile"
+ },
+ {
+ "label": "publish",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "publish",
+ "${workspaceFolder}/TP Blazor/TP Blazor.csproj",
+ "/property:GenerateFullPaths=true",
+ "/consoleloggerparameters:NoSummary"
+ ],
+ "problemMatcher": "$msCompile"
+ },
+ {
+ "label": "watch",
+ "command": "dotnet",
+ "type": "process",
+ "args": [
+ "watch",
+ "run",
+ "--project",
+ "${workspaceFolder}/TP Blazor/TP Blazor.csproj"
+ ],
+ "problemMatcher": "$msCompile"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/TP Blazor.sln b/TP Blazor.sln
index f2dd4d1..0d42231 100644
--- a/TP Blazor.sln
+++ b/TP Blazor.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TP Blazor", "TP Blazor\TP Blazor.csproj", "{DC7DF5A3-75B4-4044-B267-88F4E7705712}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TP Blazor", "TP Blazor\TP Blazor.csproj", "{DC7DF5A3-75B4-4044-B267-88F4E7705712}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/TP Blazor/App.razor b/TP Blazor/App.razor
index 6fd3ed1..09ed54d 100644
--- a/TP Blazor/App.razor
+++ b/TP Blazor/App.razor
@@ -1,12 +1,14 @@
-
-
-
-
-
-
- Not found
-
- Sorry, there's nothing at this address.
-
-
-
+
+
+
+
+
+
+
+ Not found
+
+ Sorry, there's nothing at this address.
+
+
+
+
\ No newline at end of file
diff --git a/TP Blazor/Components/Card.razor b/TP Blazor/Components/Card.razor
new file mode 100644
index 0000000..e15f762
--- /dev/null
+++ b/TP Blazor/Components/Card.razor
@@ -0,0 +1,10 @@
+@using TP_Blazor.Models
+
+
Card
+
+@typeparam TItem
+
+ @CardHeader(Item)
+ @CardBody(Item)
+ @CardFooter
+
diff --git a/TP Blazor/Components/Card.razor.cs b/TP Blazor/Components/Card.razor.cs
new file mode 100644
index 0000000..cd9a3a5
--- /dev/null
+++ b/TP Blazor/Components/Card.razor.cs
@@ -0,0 +1,16 @@
+using Microsoft.AspNetCore.Components;
+
+namespace TP_Blazor.Components
+{
+ public partial class Card
+ {
+ [Parameter]
+ public RenderFragment CardBody { get; set; }
+
+ [Parameter]
+ public RenderFragment CardHeader { get; set; }
+
+ [Parameter]
+ public RenderFragment CardFooter { get; set; }
+ }
+}
diff --git a/TP Blazor/Controllers/CultureController.cs b/TP Blazor/Controllers/CultureController.cs
new file mode 100644
index 0000000..60e6389
--- /dev/null
+++ b/TP Blazor/Controllers/CultureController.cs
@@ -0,0 +1,19 @@
+using Microsoft.AspNetCore.Localization;
+using Microsoft.AspNetCore.Mvc;
+
+namespace TP_Blazor.Controllers;
+
+[Route("[controller]/[action]")]
+public class CultureController:Controller
+{
+ public IActionResult SetCulture(string culture, string returnUrl)
+ {
+ if(culture != null)
+ {
+ this.HttpContext.Response.Cookies.Append(
+ CookieRequestCultureProvider.DefaultCookieName,
+ CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)));
+ }
+ return this.LocalRedirect(returnUrl);
+ }
+}
\ No newline at end of file
diff --git a/TP Blazor/Factories/ItemFactory.cs b/TP Blazor/Factories/ItemFactory.cs
new file mode 100644
index 0000000..c716645
--- /dev/null
+++ b/TP Blazor/Factories/ItemFactory.cs
@@ -0,0 +1,48 @@
+using TP_Blazor.Models;
+
+namespace TP_Blazor.Factories;
+
+public static class ItemFactory
+{
+ public static ItemModel ToModel(Item item, byte[] imageContent)
+ {
+ return new ItemModel
+ {
+ Id = item.Id,
+ DisplayName = item.DisplayName,
+ Name = item.Name,
+ RepairWith = item.RepairWith,
+ EnchantCategories = item.EnchantCategories,
+ MaxDurability = item.MaxDurability,
+ StackSize = item.StackSize,
+ ImageContent = imageContent
+ };
+ }
+
+ public static Item Create(ItemModel model)
+ {
+ return new Item
+ {
+ Id = model.Id,
+ DisplayName = model.DisplayName,
+ Name = model.Name,
+ RepairWith = model.RepairWith,
+ EnchantCategories = model.EnchantCategories,
+ MaxDurability = model.MaxDurability,
+ StackSize = model.StackSize,
+ CreatedDate = DateTime.Now
+ };
+ }
+
+ public static void Update(Item item, ItemModel model)
+ {
+ item.DisplayName = model.DisplayName;
+ item.Name = model.Name;
+ item.RepairWith = model.RepairWith;
+ item.EnchantCategories = model.EnchantCategories;
+ item.MaxDurability = model.MaxDurability;
+ item.StackSize = model.StackSize;
+ item.UpdatedDate = DateTime.Now;
+ }
+
+}
\ No newline at end of file
diff --git a/TP Blazor/Modals/DeleteConfirmation.razor b/TP Blazor/Modals/DeleteConfirmation.razor
new file mode 100644
index 0000000..561147f
--- /dev/null
+++ b/TP Blazor/Modals/DeleteConfirmation.razor
@@ -0,0 +1,10 @@
+
\ No newline at end of file
diff --git a/TP Blazor/Modals/DeleteConfirmation.razor.cs b/TP Blazor/Modals/DeleteConfirmation.razor.cs
new file mode 100644
index 0000000..b46bc18
--- /dev/null
+++ b/TP Blazor/Modals/DeleteConfirmation.razor.cs
@@ -0,0 +1,37 @@
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using Microsoft.AspNetCore.Components;
+using TP_Blazor.Models;
+using TP_Blazor.Services;
+
+namespace TP_Blazor.Modals;
+
+public partial class DeleteConfirmation
+{
+ [CascadingParameter]
+ public BlazoredModalInstance ModalInstance { get; set; }
+
+ [Inject]
+ public IDataService DataService { get; set; }
+
+ [Parameter]
+ public int Id { get; set; }
+
+ private Item item = new Item();
+
+ protected override async Task OnInitializedAsync()
+ {
+ // Get the item
+ item = await DataService.GetById(Id);
+ }
+
+ void ConfirmDelete()
+ {
+ ModalInstance.CloseAsync(ModalResult.Ok(true));
+ }
+
+ void Cancel()
+ {
+ ModalInstance.CancelAsync();
+ }
+}
\ No newline at end of file
diff --git a/TP Blazor/Models/ItemModel.cs b/TP Blazor/Models/ItemModel.cs
new file mode 100644
index 0000000..ef96511
--- /dev/null
+++ b/TP Blazor/Models/ItemModel.cs
@@ -0,0 +1,38 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace TP_Blazor.Models;
+
+public class ItemModel
+{
+ public int Id { get; set; }
+
+ [Required]
+ [StringLength(50, ErrorMessage = "Le nom affiché ne doit pas dépasser 50 caractères.")]
+ public string DisplayName { get; set; }
+
+ [Required]
+ [StringLength(50, ErrorMessage = "Le nom ne doit pas dépasser 50 caractères.")]
+ [RegularExpression(@"^[a-z''-'\s]{1,40}$", ErrorMessage = "Seulement les caractères en minuscule sont acceptées.")]
+ 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 EnchantCategories { get; set; }
+
+ public List RepairWith { get; set; }
+
+ [Required]
+ [Range(typeof(bool), "true", "true", ErrorMessage = "Vous devez accepter les conditions.")]
+ public bool AcceptCondition { get; set; }
+
+ [Required(ErrorMessage = "L'image de l'item est obligatoire !")]
+ public byte[] ImageContent { get; set; }
+}
+
diff --git a/TP Blazor/Pages/Add.razor b/TP Blazor/Pages/Add.razor
new file mode 100644
index 0000000..d696ca7
--- /dev/null
+++ b/TP Blazor/Pages/Add.razor
@@ -0,0 +1,72 @@
+@page "/add"
+
+Add
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Enchant categories:
+
+ @foreach (var item in enchantCategories)
+ {
+
+ }
+
+
+
+ Repair with:
+
+ @foreach (var item in repairWith)
+ {
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TP Blazor/Pages/Add.razor.cs b/TP Blazor/Pages/Add.razor.cs
new file mode 100644
index 0000000..8f7cf02
--- /dev/null
+++ b/TP Blazor/Pages/Add.razor.cs
@@ -0,0 +1,116 @@
+using System;
+using System.Security.Cryptography;
+using Blazored.LocalStorage;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+using TP_Blazor.Models;
+using TP_Blazor.Services;
+
+namespace TP_Blazor.Pages;
+
+public partial class Add
+{
+ [Inject]
+ ILocalStorageService LocalStorage { get; set; }
+
+ [Inject]
+ IWebHostEnvironment WebHostEnvironment { get; set; }
+
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
+
+ private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" };
+ private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" };
+
+ [Inject]
+ private IDataService DataService{ get; set; }
+
+
+
+
+ private ItemModel itemModel = new()
+ {
+ EnchantCategories = new List(),
+ RepairWith = new List()
+ };
+
+ private async void OnHandleValidSubmit()
+ {
+ /*var currentData = await LocalStorage.GetItemAsync>("data");
+ itemModel.Id = currentData.Max(s => s.Id) + 1;
+ 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
+ });
+
+ var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images");
+
+ if (!imagePathInfo.Exists)
+ {
+ imagePathInfo.Create();
+ }
+
+ var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
+ await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
+
+ await LocalStorage.SetItemAsync("data", currentData);
+ NavigationManager.NavigateTo("list");*/
+ await DataService.Add(itemModel);
+ NavigationManager.NavigateTo("list");
+ }
+
+ private async Task LoadImage(InputFileChangeEventArgs e)
+ {
+
+ 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);
+ }
+ }
+
+}
+
diff --git a/TP Blazor/Pages/Edit.razor b/TP Blazor/Pages/Edit.razor
new file mode 100644
index 0000000..9aecaa2
--- /dev/null
+++ b/TP Blazor/Pages/Edit.razor
@@ -0,0 +1,81 @@
+@page "/edit/{Id:int}"
+
+Edit
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Enchant categories:
+
+ @foreach (var item in enchantCategories)
+ {
+
+ }
+
+
+
+ Repair with:
+
+ @foreach (var item in repairWith)
+ {
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/TP Blazor/Pages/Edit.razor.cs b/TP Blazor/Pages/Edit.razor.cs
new file mode 100644
index 0000000..99ee15c
--- /dev/null
+++ b/TP Blazor/Pages/Edit.razor.cs
@@ -0,0 +1,106 @@
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+using TP_Blazor.Factories;
+using TP_Blazor.Models;
+using TP_Blazor.Services;
+
+namespace TP_Blazor.Pages;
+
+public partial class Edit
+{
+ [Parameter]
+ public int Id { get; set; }
+
+ private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" };
+
+ ///
+ /// The current item model
+ ///
+ private ItemModel itemModel = new()
+ {
+ EnchantCategories = new List(),
+ RepairWith = new List()
+ };
+
+ ///
+ /// The default repair with.
+ ///
+ private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" };
+
+ [Inject]
+ public IDataService DataService { get; set; }
+
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
+
+ [Inject]
+ public IWebHostEnvironment WebHostEnvironment { get; set; }
+
+ protected override async Task OnInitializedAsync()
+ {
+ var item = await DataService.GetById(Id);
+
+ var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png");
+
+ if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png"))
+ {
+ fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png");
+ }
+
+ // Set the model with the item
+ itemModel =ItemFactory.ToModel(item, fileContent);
+ }
+
+ private async void HandleValidSubmit()
+ {
+ await DataService.Update(Id, itemModel);
+
+ 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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/TP Blazor/Pages/Index.razor b/TP Blazor/Pages/Index.razor
index 6085c4a..d545980 100644
--- a/TP Blazor/Pages/Index.razor
+++ b/TP Blazor/Pages/Index.razor
@@ -1,9 +1,28 @@
@page "/"
+@using System.Globalization
+@using TP_Blazor.Components
Index
Hello, world!
+
+ CurrentCulture: @CultureInfo.CurrentCulture
+
-Welcome to your new app.
-
-
+
+
+
+
+
+
+
Template Component Body
+
+
+
+
+
+
diff --git a/TP Blazor/Pages/List.razor b/TP Blazor/Pages/List.razor
index 94fc595..1656ee0 100644
--- a/TP Blazor/Pages/List.razor
+++ b/TP Blazor/Pages/List.razor
@@ -1,8 +1,12 @@
@page "/list"
@using TP_Blazor.Models
-Liste
+@Localizer["Title"]
-@*@if (items!=null)
{
+@*@if (items!=null)
+ {
+
+
+
Id |
Display Name |
Stack Size |
@@ -10,7 +14,9 @@
Enchant Categories |
Repair With |
Created Date |
-
+
+
+
@foreach (var item in items)
{
@@ -23,8 +29,24 @@
@item.CreatedDate.ToShortDateString() |
}
-
}*@
+
+
+
+ }*@
+
+
+
+ @Localizer["btnTitle"]
+
+
+
+
+
+
+
+
+
@@ -39,4 +61,10 @@
+
+
+ Editer
+
+
+
\ No newline at end of file
diff --git a/TP Blazor/Pages/List.razor.cs b/TP Blazor/Pages/List.razor.cs
index 58790ce..fc9879b 100644
--- a/TP Blazor/Pages/List.razor.cs
+++ b/TP Blazor/Pages/List.razor.cs
@@ -2,6 +2,13 @@
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
using TP_Blazor.Models;
+using Blazored;
+using Blazored.LocalStorage;
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using TP_Blazor.Modals;
+using TP_Blazor.Services;
+using Microsoft.Extensions.Localization;
namespace TP_Blazor.Pages;
@@ -15,31 +22,77 @@ public partial class List
private int totalItem;
[Inject]
- public HttpClient Http { get; set; }
+ public IStringLocalizer Localizer { get; set; }
+ [Inject]
+ public HttpClient HttpClient { get; set; }
+
+ [Inject]
+ public IDataService DataService { get; set; }
+
+ [CascadingParameter]
+ public IModalService Modal { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
+ [Inject]
+ IWebHostEnvironment WebHostEnvironment { get; set; }
+
+ [Inject]
+ public ILocalStorageService LocalStorage { get; set; }
+
+
+
private async Task OnReadData(DataGridReadDataEventArgs- e)
{
if (e.CancellationToken.IsCancellationRequested)
{
return;
}
- var response = (await Http.GetFromJsonAsync
- ($"{NavigationManager.BaseUri}fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
+ //var response = (await HttpClient.GetFromJsonAsync
- ($"{NavigationManager.BaseUri}fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested)
{
- totalItem = (await Http.GetFromJsonAsync
>($"{NavigationManager.BaseUri}fake-data.json")).Count;
- items = new List- (response); // an actual data for the current page
+ //totalItem = (await HttpClient.GetFromJsonAsync
>($"{NavigationManager.BaseUri}fake-data.json")).Count;
+ //items = new List- (response); // an actual data for the current page
+ items = await DataService.List(e.Page, e.PageSize);
+ totalItem = await DataService.Count();
}
}
- protected override void OnAfterRender(bool firstRender)
+ protected override async Task OnAfterRenderAsync(bool firstRender)
{
- base.OnAfterRender(firstRender);
+ if (!firstRender)
+ {
+ return;
+ }
+
+ var currentData = await LocalStorage.GetItemAsync
- ("data");
+
+ if (currentData==null)
+ {
+ var originalData = HttpClient.GetFromJsonAsync
- ($"{NavigationManager.BaseUri}fake-data.json").Result;
+ await LocalStorage.SetItemAsync("data", originalData);
+ }
}
+
+ private async void OnDelete(int id)
+ {
+ var parameters = new ModalParameters();
+ parameters.Add(nameof(Item.Id), id);
+
+ var modal = Modal.Show("Delete Confirmation", parameters);
+ var result = await modal.Result;
+ if (result.Cancelled)
+ {
+ return;
+ }
+ await DataService.Delete(id);
+
+ // Reload the page
+ NavigationManager.NavigateTo("list", true);
+ }
}
diff --git a/TP Blazor/Pages/_Host.cshtml b/TP Blazor/Pages/_Host.cshtml
index b6b8f8c..cbad39c 100644
--- a/TP Blazor/Pages/_Host.cshtml
+++ b/TP Blazor/Pages/_Host.cshtml
@@ -16,6 +16,7 @@
+
@@ -34,6 +35,7 @@
🗙
-
+
+