From 6776ca6a0c45cce086cf02fd13ff7794563b7ad8 Mon Sep 17 00:00:00 2001
From: Alexis DRAI
Date: Mon, 23 Jan 2023 15:13:06 +0100
Subject: [PATCH] :sparkles: Add edit feature
---
.gitignore | 2 +
blazor_lab/Factories/ItemFactory.cs | 48 ++++++++++
blazor_lab/Pages/Edit.razor | 89 ++++++++++++++++++
blazor_lab/Pages/Edit.razor.cs | 116 ++++++++++++++++++++++++
blazor_lab/Pages/List.razor | 8 +-
blazor_lab/Services/DataLocalService.cs | 60 ++++++++----
blazor_lab/Services/IDataService.cs | 2 +
7 files changed, 308 insertions(+), 17 deletions(-)
create mode 100644 blazor_lab/Factories/ItemFactory.cs
create mode 100644 blazor_lab/Pages/Edit.razor
create mode 100644 blazor_lab/Pages/Edit.razor.cs
diff --git a/.gitignore b/.gitignore
index 3b84adc..0b2715d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,8 @@
# Created by https://www.toptal.com/developers/gitignore/api/dotnetcore,visualstudio,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=dotnetcore,visualstudio,visualstudiocode
+/wwwroot/images/*.png
+
### DotnetCore ###
# .NET Core build folders
bin/
diff --git a/blazor_lab/Factories/ItemFactory.cs b/blazor_lab/Factories/ItemFactory.cs
new file mode 100644
index 0000000..e7e2a1c
--- /dev/null
+++ b/blazor_lab/Factories/ItemFactory.cs
@@ -0,0 +1,48 @@
+using blazor_lab.Models;
+
+namespace blazor_lab.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;
+ }
+ }
+}
diff --git a/blazor_lab/Pages/Edit.razor b/blazor_lab/Pages/Edit.razor
new file mode 100644
index 0000000..585dca0
--- /dev/null
+++ b/blazor_lab/Pages/Edit.razor
@@ -0,0 +1,89 @@
+@page "/edit/{Id:int}"
+
+Edit
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Enchant categories:
+
+ @foreach (var enchant in enchantCategories)
+ {
+
+ }
+
+
+
+ Repair with:
+
+ @foreach (var material in repairWith)
+ {
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/blazor_lab/Pages/Edit.razor.cs b/blazor_lab/Pages/Edit.razor.cs
new file mode 100644
index 0000000..dfba438
--- /dev/null
+++ b/blazor_lab/Pages/Edit.razor.cs
@@ -0,0 +1,116 @@
+using blazor_lab.Factories;
+using blazor_lab.Models;
+using blazor_lab.Services;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+
+namespace blazor_lab.Pages
+{
+ public partial class Edit
+ {
+ [Parameter]
+ public int Id { get; set; }
+
+ ///
+ /// The default enchant categories.
+ ///
+ private readonly List enchantCategories = new() { "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 readonly List repairWith = new() { "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);
+
+ byte[] fileContent;
+
+ if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png"))
+ {
+ fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png");
+ }
+ else
+ {
+ fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png");
+ }
+
+ // Set the model with the item
+ itemModel = ItemFactory.ToModel(item, fileContent);
+ }
+
+ private async Task 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/blazor_lab/Pages/List.razor b/blazor_lab/Pages/List.razor
index 1dcbc5d..4869c01 100644
--- a/blazor_lab/Pages/List.razor
+++ b/blazor_lab/Pages/List.razor
@@ -50,7 +50,13 @@
Caption="Created on"
DisplayFormat="{0:d}"
DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" />
-
+
+
+
+ Editer
+
+
+
}
diff --git a/blazor_lab/Services/DataLocalService.cs b/blazor_lab/Services/DataLocalService.cs
index d4c05d4..b47c29e 100644
--- a/blazor_lab/Services/DataLocalService.cs
+++ b/blazor_lab/Services/DataLocalService.cs
@@ -1,4 +1,5 @@
-using blazor_lab.Models;
+using blazor_lab.Factories;
+using blazor_lab.Models;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
@@ -29,26 +30,16 @@ namespace blazor_lab.Services
pathToFakeData = $"{_navigationManager.BaseUri}fake-data.json";
}
- public async Task Add(ItemModel itemModel)
+ public async Task Add(ItemModel model)
{
// Get the current data
var currentData = await _localStorageService.GetItemAsync>("data");
// Simulate the Id
- itemModel.Id = currentData.Max(item => item.Id) + 1;
+ model.Id = currentData.Max(item => item.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
- });
+ currentData.Add(ItemFactory.Create(model));
// Save the image
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
@@ -60,10 +51,10 @@ namespace blazor_lab.Services
}
// Determine the image name
- var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
+ var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
// Write the file content
- await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
+ await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
// Save the data
await _localStorageService.SetItemAsync("data", currentData);
@@ -83,6 +74,18 @@ namespace blazor_lab.Services
return (await _localStorageService.GetItemAsync- ("data")).Length;
}
+ public async Task
- GetById(int id)
+ {
+ var item = (await _localStorageService.GetItemAsync
>("data")).FirstOrDefault(w => w.Id == id);
+
+ if (item == null)
+ {
+ throw new FileNotFoundException($"could not find item #{id}");
+ }
+
+ return item;
+ }
+
public async Task> List(int currentPage, int pageSize)
{
if (await _localStorageService.GetItemAsync- ("data") == null)
@@ -95,5 +98,30 @@ namespace blazor_lab.Services
return (await _localStorageService.GetItemAsync
- ("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
+
+ public async Task Update(int id, ItemModel model)
+ {
+ var currentData = await _localStorageService.GetItemAsync
>("data");
+ var item = await GetById(id);
+ var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
+ if (!imagePathInfo.Exists)
+ {
+ imagePathInfo.Create();
+ }
+ if (item.Name != model.Name)
+ {
+ var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
+ if (oldFileName.Exists)
+ {
+ File.Delete(oldFileName.FullName);
+ }
+ }
+ var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
+ await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
+
+ ItemFactory.Update(item, model);
+
+ await _localStorageService.SetItemAsync("data", currentData);
+ }
}
}
diff --git a/blazor_lab/Services/IDataService.cs b/blazor_lab/Services/IDataService.cs
index 5e2c307..4d79843 100644
--- a/blazor_lab/Services/IDataService.cs
+++ b/blazor_lab/Services/IDataService.cs
@@ -7,5 +7,7 @@ namespace blazor_lab.Services
Task Add(ItemModel itemModel);
Task Count();
Task> List(int currentPage, int pageSize);
+ Task- GetById(int id);
+ Task Update(int id, ItemModel model);
}
}