diff --git a/Sources/BlazorApp/.vs/BlazorApp/DesignTimeBuild/.dtbcache.v2 b/Sources/BlazorApp/.vs/BlazorApp/DesignTimeBuild/.dtbcache.v2
index 04da628..b255ca1 100644
Binary files a/Sources/BlazorApp/.vs/BlazorApp/DesignTimeBuild/.dtbcache.v2 and b/Sources/BlazorApp/.vs/BlazorApp/DesignTimeBuild/.dtbcache.v2 differ
diff --git a/Sources/BlazorApp/.vs/BlazorApp/v17/.suo b/Sources/BlazorApp/.vs/BlazorApp/v17/.suo
index 7f0de77..372aa25 100644
Binary files a/Sources/BlazorApp/.vs/BlazorApp/v17/.suo and b/Sources/BlazorApp/.vs/BlazorApp/v17/.suo differ
diff --git a/Sources/BlazorApp/BlazorApp/App.razor b/Sources/BlazorApp/BlazorApp/App.razor
index 6fd3ed1..09ed54d 100644
--- a/Sources/BlazorApp/BlazorApp/App.razor
+++ b/Sources/BlazorApp/BlazorApp/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/Sources/BlazorApp/BlazorApp/BlazorApp.csproj b/Sources/BlazorApp/BlazorApp/BlazorApp.csproj
index 6cdc62d..a064816 100644
--- a/Sources/BlazorApp/BlazorApp/BlazorApp.csproj
+++ b/Sources/BlazorApp/BlazorApp/BlazorApp.csproj
@@ -1,4 +1,4 @@
-
+
net6.0
@@ -8,20 +8,17 @@
-
-
-
-
-
-
-
+
+
+
+
diff --git a/Sources/BlazorApp/BlazorApp/Factories/ItemFactory.cs b/Sources/BlazorApp/BlazorApp/Factories/ItemFactory.cs
new file mode 100644
index 0000000..03be288
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Factories/ItemFactory.cs
@@ -0,0 +1,45 @@
+using BlazorApp.Models;
+
+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/Sources/BlazorApp/BlazorApp/Modals/DeleteConfirmation.razor b/Sources/BlazorApp/BlazorApp/Modals/DeleteConfirmation.razor
new file mode 100644
index 0000000..93f7a46
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Modals/DeleteConfirmation.razor
@@ -0,0 +1,10 @@
+
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Modals/DeleteConfirmation.razor.cs b/Sources/BlazorApp/BlazorApp/Modals/DeleteConfirmation.razor.cs
new file mode 100644
index 0000000..68a8628
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Modals/DeleteConfirmation.razor.cs
@@ -0,0 +1,35 @@
+using BlazorApp.Models;
+using BlazorApp.Services;
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using Microsoft.AspNetCore.Components;
+namespace BlazorApp.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/Sources/BlazorApp/BlazorApp/Pages/Add.razor.cs b/Sources/BlazorApp/BlazorApp/Pages/Add.razor.cs
index 7952afb..8c31ccc 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/Add.razor.cs
+++ b/Sources/BlazorApp/BlazorApp/Pages/Add.razor.cs
@@ -1,30 +1,15 @@
-namespace BlazorApp.Pages;
-using BlazorApp.Models;
-using Blazored.LocalStorage;
-using Microsoft.AspNetCore.Components.Forms;
+using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
+using BlazorApp.Services;
+namespace BlazorApp.Pages;
public partial class Add
{
- [Inject]
- public ILocalStorageService LocalStorage { get; set; }
-
- [Inject]
- public NavigationManager NavigationManager { get; set; }
-
- [Inject]
- public IWebHostEnvironment WebHostEnvironment { get; set; }
-
///
/// The default enchant categories.
///
private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" };
- ///
- /// 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" };
-
///
/// The current item model
///
@@ -34,44 +19,20 @@ public partial class Add
RepairWith = new List()
};
- private async void HandleValidSubmit()
- {
- // Get the current data
- var currentData = await LocalStorage.GetItemAsync>("data");
-
- // Simulate the Id
- itemModel.Id = currentData.Max(s => s.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
- });
-
- // Save the image
- var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images");
-
- // Check if the folder "images" exist
- if (!imagePathInfo.Exists)
- {
- imagePathInfo.Create();
- }
+ ///
+ /// 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" };
- // Determine the image name
- var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
+ [Inject]
+ public IDataService DataService { get; set; }
- // Write the file content
- await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
- // Save the data
- await LocalStorage.SetItemAsync("data", currentData);
+ private async void HandleValidSubmit()
+ {
+ await DataService.Add(itemModel);
NavigationManager.NavigateTo("list");
}
diff --git a/Sources/BlazorApp/BlazorApp/Pages/Edit.razor b/Sources/BlazorApp/BlazorApp/Pages/Edit.razor
new file mode 100644
index 0000000..2bdb3ca
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/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/Sources/BlazorApp/BlazorApp/Pages/Edit.razor.cs b/Sources/BlazorApp/BlazorApp/Pages/Edit.razor.cs
new file mode 100644
index 0000000..ae1a07c
--- /dev/null
+++ b/Sources/BlazorApp/BlazorApp/Pages/Edit.razor.cs
@@ -0,0 +1,106 @@
+using BlazorApp.Services;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+
+namespace BlazorApp.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);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/List.razor b/Sources/BlazorApp/BlazorApp/Pages/List.razor
index d719d00..26dd94a 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/List.razor
+++ b/Sources/BlazorApp/BlazorApp/Pages/List.razor
@@ -43,4 +43,10 @@
+
+
+ Editer
+
+
+
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs b/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs
index 77f8ec9..668d889 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs
+++ b/Sources/BlazorApp/BlazorApp/Pages/List.razor.cs
@@ -1,8 +1,14 @@
-namespace BlazorApp.Pages;
+using BlazorApp.Modals;
using BlazorApp.Models;
+using BlazorApp.Services;
using Blazored.LocalStorage;
+using Blazored.Modal;
+using Blazored.Modal.Services;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
+using Microsoft.Extensions.Localization;
+
+namespace BlazorApp.Pages;
public partial class List
{
@@ -11,49 +17,46 @@ public partial class List
private int totalItem;
[Inject]
- public HttpClient Http { get; set; }
-
- [Inject]
- public ILocalStorageService LocalStorage { get; set; }
+ public IDataService DataService { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
-
[Inject]
public NavigationManager NavigationManager { get; set; }
- protected override async Task OnAfterRenderAsync(bool firstRender)
+ [CascadingParameter]
+ public IModalService Modal { get; set; }
+
+ private async Task OnReadData(DataGridReadDataEventArgs- e)
{
- // Do not treat this action if is not the first render
- if (!firstRender)
+ if (e.CancellationToken.IsCancellationRequested)
{
return;
}
- var currentData = await LocalStorage.GetItemAsync
- ("data");
-
- // Check if data exist in the local storage
- if (currentData == null)
+ if (!e.CancellationToken.IsCancellationRequested)
{
- // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
- var originalData = Http.GetFromJsonAsync
- ($"{NavigationManager.BaseUri}fake-data.json").Result;
- await LocalStorage.SetItemAsync("data", originalData);
+ items = await DataService.List(e.Page, e.PageSize);
+ totalItem = await DataService.Count();
}
- }
+ }
- private async Task OnReadData(DataGridReadDataEventArgs
- e)
+ private async void OnDelete(int id)
{
- if (e.CancellationToken.IsCancellationRequested)
+ 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;
}
- // When you use a real API, we use this follow code
- //var response = await Http.GetJsonAsync
- ( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
- var response = (await Http.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
- }
+
+ await DataService.Delete(id);
+
+ // Reload the page
+ NavigationManager.NavigateTo("list", true);
}
}
\ No newline at end of file
diff --git a/Sources/BlazorApp/BlazorApp/Pages/_Layout.cshtml b/Sources/BlazorApp/BlazorApp/Pages/_Layout.cshtml
index bef526c..bf95d08 100644
--- a/Sources/BlazorApp/BlazorApp/Pages/_Layout.cshtml
+++ b/Sources/BlazorApp/BlazorApp/Pages/_Layout.cshtml
@@ -33,5 +33,8 @@
+
+
+