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 @@ +
+ +

+ Are you sure you want to delete @item.DisplayName ? +

+ + + + +
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 @@ + + + diff --git a/BlazorTuto/Program.cs b/BlazorTuto/Program.cs index a231309..f48387e 100644 --- a/BlazorTuto/Program.cs +++ b/BlazorTuto/Program.cs @@ -6,6 +6,7 @@ using BlazorTuto.Data; using BlazorTuto.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Blazored.Modal; var builder = WebApplication.CreateBuilder(args); @@ -20,6 +21,7 @@ builder.Services .AddFontAwesomeIcons(); builder.Services.AddScoped(); builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBlazoredModal(); var app = builder.Build(); diff --git a/BlazorTuto/Services/DataLocalService.cs b/BlazorTuto/Services/DataLocalService.cs index b632736..d7853e5 100644 --- a/BlazorTuto/Services/DataLocalService.cs +++ b/BlazorTuto/Services/DataLocalService.cs @@ -1,4 +1,5 @@ using Blazored.LocalStorage; +using BlazorTuto.Factories; using BlazorTuto.Models; using Microsoft.AspNetCore.Components; @@ -32,21 +33,12 @@ namespace BlazorTuto.Services model.Id = currentData.Max(s => s.Id) + 1; // Add the item to the current data - currentData.Add(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 - }); + currentData.Add(ItemFactory.Create(model)); // Save the image var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); + // Check if the folder "images" exist if (!imagePathInfo.Exists) { @@ -142,13 +134,31 @@ namespace BlazorTuto.Services 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; + 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.WebRootPath}/images"); + var fileName = new FileInfo($"{imagePathInfo}/{item.Name}.png"); + + if (fileName.Exists) + { + File.Delete(fileName.FullName); + } // Save the data await _localStorage.SetItemAsync("data", currentData); diff --git a/BlazorTuto/Services/IDataService.cs b/BlazorTuto/Services/IDataService.cs index 97ce172..da59633 100644 --- a/BlazorTuto/Services/IDataService.cs +++ b/BlazorTuto/Services/IDataService.cs @@ -13,5 +13,6 @@ namespace BlazorTuto.Services Task GetById(int id); Task Update(int id, ItemModel model); + Task Delete(int id); } } diff --git a/BlazorTuto/_Imports.razor b/BlazorTuto/_Imports.razor index a65b08a..8f862f4 100644 --- a/BlazorTuto/_Imports.razor +++ b/BlazorTuto/_Imports.razor @@ -11,4 +11,6 @@ @using Blazorise.Bootstrap @using Blazorise.Icons.FontAwesome @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services