From 6019c2dc73de2753754374c03456f4b86e38a9c9 Mon Sep 17 00:00:00 2001 From: aujault Date: Sun, 13 Nov 2022 17:00:17 +0100 Subject: [PATCH] update --- App.razor | 4 +- Blazor.csproj | 9 ++- Factories/ItemFactory.cs | 48 ++++++++++++ Modals/DeleteConfirmation.razor | 10 +++ Modals/DeleteConfirmation.razor.cs | 38 ++++++++++ Models/Item.cs | 2 + Pages/Add.razor.cs | 10 +-- Pages/Edit.razor | 82 ++++++++++++++++++++ Pages/Edit.razor.cs | 118 +++++++++++++++++++++++++++++ Pages/List.razor | 8 +- Pages/List.razor.cs | 33 +++++++- Pages/_Layout.cshtml | 2 + Program.cs | 5 +- Services/DataLocalService.cs | 30 +++++++- Services/IDataService.cs | 2 + _Imports.razor | 2 + 16 files changed, 386 insertions(+), 17 deletions(-) create mode 100644 Factories/ItemFactory.cs create mode 100644 Modals/DeleteConfirmation.razor create mode 100644 Modals/DeleteConfirmation.razor.cs create mode 100644 Pages/Edit.razor create mode 100644 Pages/Edit.razor.cs diff --git a/App.razor b/App.razor index 6fd3ed1..5a38683 100644 --- a/App.razor +++ b/App.razor @@ -1,4 +1,5 @@ - + + @@ -10,3 +11,4 @@ + \ No newline at end of file diff --git a/Blazor.csproj b/Blazor.csproj index 7b31672..1801469 100644 --- a/Blazor.csproj +++ b/Blazor.csproj @@ -7,10 +7,11 @@ - - - - + + + + + diff --git a/Factories/ItemFactory.cs b/Factories/ItemFactory.cs new file mode 100644 index 0000000..8c95402 --- /dev/null +++ b/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using Blazor.Models; + +namespace Blazor.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/Modals/DeleteConfirmation.razor b/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..93f7a46 --- /dev/null +++ b/Modals/DeleteConfirmation.razor @@ -0,0 +1,10 @@ +
+ +

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

+ + + + +
\ No newline at end of file diff --git a/Modals/DeleteConfirmation.razor.cs b/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..6f3dda9 --- /dev/null +++ b/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,38 @@ +using Blazor.Models; +using Blazor.Services; +using Blazored.Modal.Services; +using Blazored.Modal; +using Microsoft.AspNetCore.Components; + +namespace Blazor.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/Models/Item.cs b/Models/Item.cs index 5480e60..916bca2 100644 --- a/Models/Item.cs +++ b/Models/Item.cs @@ -1,3 +1,5 @@ +namespace Blazor.Models +{ public class Item { public int Id { get; set; } diff --git a/Pages/Add.razor.cs b/Pages/Add.razor.cs index 6a677bf..7dd0a1f 100644 --- a/Pages/Add.razor.cs +++ b/Pages/Add.razor.cs @@ -1,8 +1,8 @@ -using Blazored.LocalStorage; -using Blazor.Models; -using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.Forms; +using Blazor.Models; using Blazor.Services; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; + namespace Blazor.Pages { @@ -86,4 +86,4 @@ namespace Blazor.Pages } } } -} +} \ No newline at end of file diff --git a/Pages/Edit.razor b/Pages/Edit.razor new file mode 100644 index 0000000..74d3978 --- /dev/null +++ b/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/Pages/Edit.razor.cs b/Pages/Edit.razor.cs new file mode 100644 index 0000000..b01bea7 --- /dev/null +++ b/Pages/Edit.razor.cs @@ -0,0 +1,118 @@ +using Blazor.Models; +using Blazor.Services; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; +using Blazor.Factories; + +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 = new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = fileContent + }; + 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/Pages/List.razor b/Pages/List.razor index 29b72d8..a67e444 100644 --- a/Pages/List.razor +++ b/Pages/List.razor @@ -1,5 +1,5 @@ @page "/list" -@using Models +@using Models;

List

@@ -48,4 +48,10 @@ + + + Editer + + + \ No newline at end of file diff --git a/Pages/List.razor.cs b/Pages/List.razor.cs index 0de6458..2c14712 100644 --- a/Pages/List.razor.cs +++ b/Pages/List.razor.cs @@ -1,11 +1,12 @@ -using Blazor.Models; +using Blazor.Modals; +using Blazor.Models; using Blazor.Services; -using Blazored.LocalStorage; +using Blazored.Modal; +using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; -namespace Blazor.Pages -{ +namespace Blazor.Pages{ public partial class List { private List items; @@ -18,6 +19,30 @@ namespace Blazor.Pages [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] + public NavigationManager NavigationManager { get; set; } + + [CascadingParameter] + public IModalService Modal { get; set; } + + private async void 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); + } private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) diff --git a/Pages/_Layout.cshtml b/Pages/_Layout.cshtml index 834f647..001f014 100644 --- a/Pages/_Layout.cshtml +++ b/Pages/_Layout.cshtml @@ -32,6 +32,8 @@ + + diff --git a/Program.cs b/Program.cs index 6351e83..77ed601 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ using Blazored.LocalStorage; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; +using Blazored.Modal; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; @@ -18,7 +19,8 @@ builder.Services.AddHttpClient(); builder.Services .AddBlazorise() .AddBootstrapProviders() - .AddFontAwesomeIcons(); + .AddFontAwesomeIcons() + .AddBlazoredModal(); builder.Services.AddBlazoredLocalStorage(); builder.Services.AddScoped(); @@ -41,5 +43,6 @@ app.UseRouting(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); +builder.Services.AddScoped(); app.Run(); diff --git a/Services/DataLocalService.cs b/Services/DataLocalService.cs index b52ffb9..5282bf3 100644 --- a/Services/DataLocalService.cs +++ b/Services/DataLocalService.cs @@ -1,4 +1,5 @@ -using Blazor.Models; +using Blazor.Factories; +using Blazor.Models; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; @@ -31,6 +32,8 @@ namespace Blazor.Services // Simulate the Id model.Id = currentData.Max(s => s.Id) + 1; + currentData.Add(ItemFactory.Create(model)); + // Add the item to the current data currentData.Add(new Item { @@ -141,6 +144,8 @@ namespace Blazor.Services // Write the file content await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); + ItemFactory.Update(item, model); + // Modify the content of the item item.DisplayName = model.DisplayName; item.Name = model.Name; @@ -150,6 +155,29 @@ namespace Blazor.Services item.StackSize = model.StackSize; item.UpdatedDate = DateTime.Now; + // 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/Services/IDataService.cs b/Services/IDataService.cs index dde14c0..2d7dd8f 100644 --- a/Services/IDataService.cs +++ b/Services/IDataService.cs @@ -13,5 +13,7 @@ namespace Blazor.Services Task GetById(int id); Task Update(int id, ItemModel model); + + Task Delete(int id); } } diff --git a/_Imports.razor b/_Imports.razor index 0bfa3cd..8491506 100644 --- a/_Imports.razor +++ b/_Imports.razor @@ -9,3 +9,5 @@ @using Blazor @using Blazor.Shared @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services \ No newline at end of file