From 81b2650d5385a1990dd8bec603cdb46a02b55357 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Victor=20P=C3=A9rez=20Ngounou?=
Date: Sun, 15 Jan 2023 02:07:35 +0100
Subject: [PATCH 1/7] Add new Feature(Edit, Update, Service and globalization)
---
TP Blazor/App.razor | 26 ++--
TP Blazor/Factories/ItemFactory.cs | 48 +++++++
TP Blazor/Modals/DeleteConfirmation.razor | 10 ++
TP Blazor/Modals/DeleteConfirmation.razor.cs | 37 +++++
TP Blazor/Pages/Edit.razor | 81 +++++++++++
TP Blazor/Pages/Edit.razor.cs | 106 +++++++++++++++
TP Blazor/Pages/List.razor | 34 ++++-
TP Blazor/Pages/List.razor.cs | 60 ++++++++-
TP Blazor/Pages/_Host.cshtml | 4 +-
TP Blazor/Program.cs | 29 +++-
TP Blazor/Services/DataLocalService.cs | 134 +++++++++++++++++++
TP Blazor/Services/IDataService.cs | 13 ++
TP Blazor/TP Blazor.csproj | 2 +
TP Blazor/_Imports.razor | 2 +
14 files changed, 563 insertions(+), 23 deletions(-)
create mode 100644 TP Blazor/Factories/ItemFactory.cs
create mode 100644 TP Blazor/Modals/DeleteConfirmation.razor
create mode 100644 TP Blazor/Modals/DeleteConfirmation.razor.cs
create mode 100644 TP Blazor/Pages/Edit.razor
create mode 100644 TP Blazor/Pages/Edit.razor.cs
create mode 100644 TP Blazor/Services/DataLocalService.cs
create mode 100644 TP Blazor/Services/IDataService.cs
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/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/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/List.razor b/TP Blazor/Pages/List.razor
index 94fc595..adfbf1f 100644
--- a/TP Blazor/Pages/List.razor
+++ b/TP Blazor/Pages/List.razor
@@ -2,7 +2,11 @@
@using TP_Blazor.Models
Liste
-@*@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() |
}
-
}*@
+
+
+
+ }*@
+
+
+
+ Ajouter
+
+
+
+
+
+
+
+
+
@@ -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..52454df 100644
--- a/TP Blazor/Pages/List.razor.cs
+++ b/TP Blazor/Pages/List.razor.cs
@@ -2,6 +2,12 @@
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;
namespace TP_Blazor.Pages;
@@ -15,31 +21,73 @@ public partial class List
private int totalItem;
[Inject]
- public HttpClient Http { get; set; }
+ 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 @@
🗙
-
+
+