From 894df66bf066abf3f468855ff9fbff0165727130 Mon Sep 17 00:00:00 2001
From: Louwar
Date: Mon, 14 Nov 2022 09:50:52 +0100
Subject: [PATCH] Add edit page (manque facotry)
---
ValblazeProject/Pages/Edit.razor | 82 +++++++++++++
ValblazeProject/Pages/Edit.razor.cs | 119 +++++++++++++++++++
ValblazeProject/Pages/List.razor | 5 +
ValblazeProject/Services/DataLocalService.cs | 70 +++++++++++
ValblazeProject/Services/IDataService.cs | 4 +-
5 files changed, 278 insertions(+), 2 deletions(-)
create mode 100644 ValblazeProject/Pages/Edit.razor
create mode 100644 ValblazeProject/Pages/Edit.razor.cs
diff --git a/ValblazeProject/Pages/Edit.razor b/ValblazeProject/Pages/Edit.razor
new file mode 100644
index 0000000..e3a7890
--- /dev/null
+++ b/ValblazeProject/Pages/Edit.razor
@@ -0,0 +1,82 @@
+@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/ValblazeProject/Pages/Edit.razor.cs b/ValblazeProject/Pages/Edit.razor.cs
new file mode 100644
index 0000000..5d3a739
--- /dev/null
+++ b/ValblazeProject/Pages/Edit.razor.cs
@@ -0,0 +1,119 @@
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+using ValblazeProject.Models;
+using ValblazeProject.Services;
+
+namespace ValblazeProject.Pages
+{
+ public partial class Edit
+ {
+ [Parameter]
+ public int Id { get; set; }
+
+ ///
+ /// The default enchant categories.
+ ///
+ 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 = new ItemModel
+ {
+ Id = item.Id,
+ DisplayName = item.DisplayName,
+ Name = item.Name,
+ RepairWith = item.RepairWith,
+ EnchantCategories = item.EnchantCategories,
+ MaxDurability = item.MaxDurability,
+ StackSize = item.StackSize,
+ ImageContent = 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);
+ }
+ }
+ }
+}
diff --git a/ValblazeProject/Pages/List.razor b/ValblazeProject/Pages/List.razor
index b1c6e43..d0e4a23 100644
--- a/ValblazeProject/Pages/List.razor
+++ b/ValblazeProject/Pages/List.razor
@@ -43,4 +43,9 @@
+
+
+ Editer
+
+
\ No newline at end of file
diff --git a/ValblazeProject/Services/DataLocalService.cs b/ValblazeProject/Services/DataLocalService.cs
index 03a50ef..b99eff6 100644
--- a/ValblazeProject/Services/DataLocalService.cs
+++ b/ValblazeProject/Services/DataLocalService.cs
@@ -83,5 +83,75 @@ namespace ValblazeProject.Services
return (await _localStorage.GetItemAsync- ("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
+
+ public async Task
- GetById(int id)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync
>("data");
+
+ // Get the item int the list
+ var item = currentData.FirstOrDefault(w => w.Id == id);
+
+ // Check if item exist
+ if (item == null)
+ {
+ throw new Exception($"Unable to found the item with ID: {id}");
+ }
+
+ return item;
+ }
+
+ public async Task Update(int id, ItemModel model)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync>("data");
+
+ // Get the item int the list
+ var item = currentData.FirstOrDefault(w => w.Id == id);
+
+ // Check if item exist
+ if (item == null)
+ {
+ throw new Exception($"Unable to found the item with ID: {id}");
+ }
+
+ // Save the image
+ var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
+
+ // Check if the folder "images" exist
+ if (!imagePathInfo.Exists)
+ {
+ imagePathInfo.Create();
+ }
+
+ // Delete the previous image
+ if (item.Name != model.Name)
+ {
+ var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
+
+ if (oldFileName.Exists)
+ {
+ File.Delete(oldFileName.FullName);
+ }
+ }
+
+ // Determine the image name
+ var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
+
+ // Write the file content
+ await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
+
+ // Modify the content of the item
+ 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;
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
}
}
diff --git a/ValblazeProject/Services/IDataService.cs b/ValblazeProject/Services/IDataService.cs
index d7f645c..b0fd707 100644
--- a/ValblazeProject/Services/IDataService.cs
+++ b/ValblazeProject/Services/IDataService.cs
@@ -5,9 +5,9 @@ namespace ValblazeProject.Services
public interface IDataService
{
Task Add(ItemModel model);
-
Task Count();
-
Task> List(int currentPage, int pageSize);
+ Task- GetById(int id);
+ Task Update(int id, ItemModel model);
}
}