diff --git a/BlazorTuto/App.razor b/BlazorTuto/App.razor
index 623580d..54966a1 100644
--- a/BlazorTuto/App.razor
+++ b/BlazorTuto/App.razor
@@ -1,12 +1,14 @@
-
-
-
-
-
-
- Not found
-
- Sorry, there's nothing at this address.
-
-
-
+
+
+
+
+
+
+
+ Not found
+
+ Sorry, there's nothing at this address.
+
+
+
+
diff --git a/BlazorTuto/BlazorTuto.csproj b/BlazorTuto/BlazorTuto.csproj
index 93b929d..f2eba01 100644
--- a/BlazorTuto/BlazorTuto.csproj
+++ b/BlazorTuto/BlazorTuto.csproj
@@ -8,6 +8,7 @@
+
diff --git a/BlazorTuto/Factories/ItemFactory.cs b/BlazorTuto/Factories/ItemFactory.cs
new file mode 100644
index 0000000..12976fa
--- /dev/null
+++ b/BlazorTuto/Factories/ItemFactory.cs
@@ -0,0 +1,48 @@
+using BlazorTuto.Models;
+
+namespace BlazorTuto.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/BlazorTuto/Modals/DeleteConfirmation.razor b/BlazorTuto/Modals/DeleteConfirmation.razor
new file mode 100644
index 0000000..47ec04a
--- /dev/null
+++ b/BlazorTuto/Modals/DeleteConfirmation.razor
@@ -0,0 +1,10 @@
+
diff --git a/BlazorTuto/Modals/DeleteConfirmation.razor.cs b/BlazorTuto/Modals/DeleteConfirmation.razor.cs
new file mode 100644
index 0000000..bbe1d67
--- /dev/null
+++ b/BlazorTuto/Modals/DeleteConfirmation.razor.cs
@@ -0,0 +1,38 @@
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using BlazorTuto.Models;
+using BlazorTuto.Services;
+using Microsoft.AspNetCore.Components;
+
+namespace BlazorTuto.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();
+ }
+ }
+}
diff --git a/BlazorTuto/Pages/Edit.razor b/BlazorTuto/Pages/Edit.razor
new file mode 100644
index 0000000..57cedae
--- /dev/null
+++ b/BlazorTuto/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/BlazorTuto/Pages/Edit.razor.cs b/BlazorTuto/Pages/Edit.razor.cs
new file mode 100644
index 0000000..bac11c4
--- /dev/null
+++ b/BlazorTuto/Pages/Edit.razor.cs
@@ -0,0 +1,110 @@
+using BlazorTuto.Factories;
+using BlazorTuto.Models;
+using BlazorTuto.Services;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+
+namespace BlazorTuto.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 = 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);
+ }
+ }
+ }
+}
diff --git a/BlazorTuto/Pages/List.razor b/BlazorTuto/Pages/List.razor
index 0dc02b3..ac12fd2 100644
--- a/BlazorTuto/Pages/List.razor
+++ b/BlazorTuto/Pages/List.razor
@@ -43,9 +43,10 @@
-
+
Editer
+
\ No newline at end of file
diff --git a/BlazorTuto/Pages/List.razor.cs b/BlazorTuto/Pages/List.razor.cs
index 04b34d9..436e135 100644
--- a/BlazorTuto/Pages/List.razor.cs
+++ b/BlazorTuto/Pages/List.razor.cs
@@ -1,6 +1,10 @@
using Blazored.LocalStorage;
+using Blazored.Modal;
+using Blazored.Modal.Services;
using Blazorise.DataGrid;
+using BlazorTuto.Modals;
using BlazorTuto.Models;
+using BlazorTuto.Services;
using Microsoft.AspNetCore.Components;
namespace BlazorTuto.Pages
@@ -23,6 +27,12 @@ namespace BlazorTuto.Pages
[Inject]
public NavigationManager NavigationManager { get; set; }
+ [Inject]
+ public IDataService DataService { get; set; }
+
+ [CascadingParameter]
+ public IModalService Modal { get; set; }
+
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Do not treat this action if is not the first render
@@ -59,5 +69,24 @@ namespace BlazorTuto.Pages
items = new List- (response); // an actual data for the current page
}
}
+
+ private async Task 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/BlazorTuto/Pages/_Layout.cshtml b/BlazorTuto/Pages/_Layout.cshtml
index 384459a..6647878 100644
--- a/BlazorTuto/Pages/_Layout.cshtml
+++ b/BlazorTuto/Pages/_Layout.cshtml
@@ -33,5 +33,8 @@
+
+
+