diff --git a/BlazorProject/App.razor b/BlazorProject/App.razor
index 623580d..6346f35 100644
--- a/BlazorProject/App.razor
+++ b/BlazorProject/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/BlazorProject/BlazorProject.csproj b/BlazorProject/BlazorProject.csproj
index 5b70e3e..039219d 100644
--- a/BlazorProject/BlazorProject.csproj
+++ b/BlazorProject/BlazorProject.csproj
@@ -8,12 +8,14 @@
+
+
diff --git a/BlazorProject/Factories/ItemFactory.cs b/BlazorProject/Factories/ItemFactory.cs
new file mode 100644
index 0000000..ac3659e
--- /dev/null
+++ b/BlazorProject/Factories/ItemFactory.cs
@@ -0,0 +1,47 @@
+using BlazorProject.Models;
+
+namespace BlazorProject.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/BlazorProject/Modals/DeleteConfirmation.razor b/BlazorProject/Modals/DeleteConfirmation.razor
new file mode 100644
index 0000000..561147f
--- /dev/null
+++ b/BlazorProject/Modals/DeleteConfirmation.razor
@@ -0,0 +1,10 @@
+
\ No newline at end of file
diff --git a/BlazorProject/Modals/DeleteConfirmation.razor.cs b/BlazorProject/Modals/DeleteConfirmation.razor.cs
new file mode 100644
index 0000000..3b6dfe5
--- /dev/null
+++ b/BlazorProject/Modals/DeleteConfirmation.razor.cs
@@ -0,0 +1,37 @@
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using BlazorProject.Models;
+using BlazorProject.Services;
+using Microsoft.AspNetCore.Components;
+
+namespace BlazorProject.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/BlazorProject/Pages/Add.razor.cs b/BlazorProject/Pages/Add.razor.cs
index 7311e5a..54aceaf 100644
--- a/BlazorProject/Pages/Add.razor.cs
+++ b/BlazorProject/Pages/Add.razor.cs
@@ -1,5 +1,6 @@
using Blazored.LocalStorage;
using BlazorProject.Models;
+using BlazorProject.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
@@ -16,7 +17,9 @@ public partial class Add
[Inject]
public IWebAssemblyHostEnvironment WebHostEnvironment { get; set; }
-
+
+ [Inject]
+ public IDataService DataService { get; set; }
///
/// The default enchant categories.
///
@@ -38,43 +41,9 @@ public partial class Add
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.BaseAddress}/images");
-
- // Check if the folder "images" exist
- if (!imagePathInfo.Exists)
- {
- imagePathInfo.Create();
- }
-
- // Determine the image name
- var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
+ await DataService.Add(itemModel);
- // Write the file content
- await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
-
- // Save the data
- await LocalStorage.SetItemAsync("data", currentData);
- NavigationManager.NavigateTo("List.razor");
+ NavigationManager.NavigateTo("list");
}
private async Task LoadImage(InputFileChangeEventArgs e)
diff --git a/BlazorProject/Pages/Edit.razor b/BlazorProject/Pages/Edit.razor
new file mode 100644
index 0000000..4de5444
--- /dev/null
+++ b/BlazorProject/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/BlazorProject/Pages/Edit.razor.cs b/BlazorProject/Pages/Edit.razor.cs
new file mode 100644
index 0000000..5e4300d
--- /dev/null
+++ b/BlazorProject/Pages/Edit.razor.cs
@@ -0,0 +1,109 @@
+using BlazorProject.Factories;
+using BlazorProject.Models;
+using BlazorProject.Services;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
+
+namespace BlazorProject.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 IWebAssemblyHostEnvironment WebHostEnvironment { get; set; }
+
+ protected override async Task OnInitializedAsync()
+ {
+ var item = await DataService.GetById(Id);
+
+ var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.BaseAddress}/images/default.png");
+
+ if (File.Exists($"{WebHostEnvironment.BaseAddress}/images/{itemModel.Name}.png"))
+ {
+ fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.BaseAddress}/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/BlazorProject/Pages/List.razor b/BlazorProject/Pages/List.razor
index 25659f0..7ab3f3f 100644
--- a/BlazorProject/Pages/List.razor
+++ b/BlazorProject/Pages/List.razor
@@ -17,16 +17,16 @@
Responsive>
-
- @if (File.Exists($"{WebHostEnvironment.BaseAddress}/images/{context.Name}.png"))
- {
-
- }
- else
- {
-
- }
-
+
+ @if (File.Exists($"{WebHostEnvironment.BaseAddress}/images/{context.Name}.png"))
+ {
+
+ }
+ else
+ {
+
+ }
+
@@ -42,4 +42,10 @@
+
+
+ Editer
+
+
+
\ No newline at end of file
diff --git a/BlazorProject/Pages/List.razor.cs b/BlazorProject/Pages/List.razor.cs
index 06c531b..7f5412d 100644
--- a/BlazorProject/Pages/List.razor.cs
+++ b/BlazorProject/Pages/List.razor.cs
@@ -1,7 +1,11 @@
using System.Net.Http.Json;
using Blazored.LocalStorage;
+using Blazored.Modal;
+using Blazored.Modal.Services;
using Blazorise.DataGrid;
+using BlazorProject.Modals;
using BlazorProject.Models;
+using BlazorProject.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
@@ -24,6 +28,12 @@ public partial class List
[Inject]
public NavigationManager NavigationManager { get; set; }
+
+ [CascadingParameter]
+ public IModalService Modal { get; set; }
+
+ [Inject]
+ public IDataService DataService { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
@@ -51,14 +61,24 @@ public partial class List
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 LocalStorage.GetItemAsync- ("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
-
if (!e.CancellationToken.IsCancellationRequested)
{
- totalItem = (await LocalStorage.GetItemAsync
>("data")).Count;
- items = new List- (response); // an actual data for the current page
+ items = await DataService.List(e.Page, e.PageSize);
+ totalItem = await DataService.Count();
}
}
+
+ private void OnDelete(int id)
+ {
+ var parameters = new ModalParameters();
+ parameters.Add(nameof(Item.Id), id);
+
+ var modal = Modal.Show("Delete Confirmation", parameters);
+ var result = modal.Result;
+
+ DataService.Delete(id);
+
+ // Reload the page
+ NavigationManager.NavigateTo("list", true);
+ }
}
\ No newline at end of file
diff --git a/BlazorProject/Program.cs b/BlazorProject/Program.cs
index bd3f936..1672370 100644
--- a/BlazorProject/Program.cs
+++ b/BlazorProject/Program.cs
@@ -1,14 +1,18 @@
using Blazored.LocalStorage;
+using Blazored.Modal;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using BlazorProject;
+using BlazorProject.Services;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddBlazoredLocalStorage();
+builder.Services.AddBlazoredModal();
+builder.Services.AddScoped();
builder.Services
.AddBlazorise()
.AddBootstrapProviders()
@@ -20,4 +24,4 @@ builder.RootComponents.Add("head::after");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
-await builder.Build().RunAsync();
+await builder.Build().RunAsync();
\ No newline at end of file
diff --git a/BlazorProject/Services/DataLocalService.cs b/BlazorProject/Services/DataLocalService.cs
new file mode 100644
index 0000000..50ed6e9
--- /dev/null
+++ b/BlazorProject/Services/DataLocalService.cs
@@ -0,0 +1,178 @@
+using System.Net.Http.Json;
+using Blazored.LocalStorage;
+using BlazorProject.Factories;
+using BlazorProject.Models;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
+
+namespace BlazorProject.Services;
+
+public class DataLocalService : IDataService
+{
+ private readonly HttpClient _http;
+ private readonly ILocalStorageService _localStorage;
+ private readonly NavigationManager _navigationManager;
+ private readonly IWebAssemblyHostEnvironment _webHostEnvironment;
+
+ public DataLocalService(
+ ILocalStorageService localStorage,
+ HttpClient http,
+ IWebAssemblyHostEnvironment webHostEnvironment,
+ NavigationManager navigationManager)
+ {
+ _localStorage = localStorage;
+ _http = http;
+ _webHostEnvironment = webHostEnvironment;
+ _navigationManager = navigationManager;
+ }
+
+ public async Task Add(ItemModel model)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync
>("data");
+
+ // Simulate the Id
+ model.Id = currentData.Max(s => s.Id) + 1;
+
+ // Add the item to the current data
+ currentData.Add(ItemFactory.Create(model));
+
+ // Save the image
+ var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.BaseAddress}/images");
+
+ // Check if the folder "images" exist
+ if (!imagePathInfo.Exists)
+ {
+ imagePathInfo.Create();
+ }
+
+ // Determine the image name
+ var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
+
+ // Write the file content
+ await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
+
+ public async Task Count()
+ {
+ // Load data from the local storage
+ var currentData = await _localStorage.GetItemAsync- ("data");
+
+ // Check if data exist in the local storage
+ if (currentData == null)
+ {
+ // this code add in the local storage the fake data
+ var originalData = await _http.GetFromJsonAsync
- ($"{_navigationManager.BaseUri}fake-data.json");
+ await _localStorage.SetItemAsync("data", originalData);
+ }
+
+ return (await _localStorage.GetItemAsync
- ("data")).Length;
+ }
+
+ public async Task
> List(int currentPage, int pageSize)
+ {
+ // Load data from the local storage
+ var currentData = await _localStorage.GetItemAsync- ("data");
+
+ // Check if data exist in the local storage
+ if (currentData == null)
+ {
+ // this code add in the local storage the fake data
+ var originalData = await _http.GetFromJsonAsync
- ($"{_navigationManager.BaseUri}fake-data.json");
+ await _localStorage.SetItemAsync("data", originalData);
+ }
+
+ 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.BaseAddress}/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
+ ItemFactory.Update(item, model);
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
+
+ public async Task Delete(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);
+
+ // Delete item in
+ currentData.Remove(item);
+
+ // Delete the image
+ var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.BaseAddress}/images");
+ var fileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
+
+ if (fileName.Exists)
+ {
+ File.Delete(fileName.FullName);
+ }
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
+}
\ No newline at end of file
diff --git a/BlazorProject/Services/IDataService.cs b/BlazorProject/Services/IDataService.cs
new file mode 100644
index 0000000..09be42c
--- /dev/null
+++ b/BlazorProject/Services/IDataService.cs
@@ -0,0 +1,18 @@
+using BlazorProject.Models;
+
+namespace BlazorProject.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);
+
+ Task Delete(int id);
+}
\ No newline at end of file
diff --git a/BlazorProject/_Imports.razor b/BlazorProject/_Imports.razor
index d8f7d71..1fc356d 100644
--- a/BlazorProject/_Imports.razor
+++ b/BlazorProject/_Imports.razor
@@ -9,3 +9,5 @@
@using BlazorProject
@using BlazorProject.Layout
@using Blazorise.DataGrid
+@using Blazored.Modal
+@using Blazored.Modal.Services
\ No newline at end of file
diff --git a/BlazorProject/wwwroot/images/avatar.png b/BlazorProject/wwwroot/images/avatar.png
new file mode 100644
index 0000000..162ef9e
Binary files /dev/null and b/BlazorProject/wwwroot/images/avatar.png differ
diff --git a/BlazorProject/wwwroot/images/rick&morty.png b/BlazorProject/wwwroot/images/rickandmorty.png
similarity index 100%
rename from BlazorProject/wwwroot/images/rick&morty.png
rename to BlazorProject/wwwroot/images/rickandmorty.png
diff --git a/BlazorProject/wwwroot/index.html b/BlazorProject/wwwroot/index.html
index a424197..72e7e61 100644
--- a/BlazorProject/wwwroot/index.html
+++ b/BlazorProject/wwwroot/index.html
@@ -13,6 +13,8 @@
+
+