From d191f9bf1f01650a7212fc442488b0eb6aabe496 Mon Sep 17 00:00:00 2001 From: remi Date: Tue, 15 Nov 2022 11:28:21 +0100 Subject: [PATCH] add delete item function --- Blazor/Blazor/App.razor | 26 +++++----- Blazor/Blazor/Blazor.csproj | 1 + Blazor/Blazor/Factories/ItemFactory.cs | 48 +++++++++++++++++++ Blazor/Blazor/Modals/DeleteConfirmation.razor | 10 ++++ .../Blazor/Modals/DeleteConfirmation.razor.cs | 38 +++++++++++++++ Blazor/Blazor/Pages/Edit.razor.cs | 15 ++---- Blazor/Blazor/Pages/List.razor | 11 +++-- Blazor/Blazor/Pages/List.razor.cs | 27 ++++++++++- Blazor/Blazor/Pages/_Layout.cshtml | 9 ++-- Blazor/Blazor/Program.cs | 3 ++ Blazor/Blazor/Services/DataLocalService.cs | 47 ++++++++++-------- Blazor/Blazor/Services/IDataService.cs | 2 + Blazor/Blazor/_Imports.razor | 2 + 13 files changed, 186 insertions(+), 53 deletions(-) create mode 100644 Blazor/Blazor/Factories/ItemFactory.cs create mode 100644 Blazor/Blazor/Modals/DeleteConfirmation.razor create mode 100644 Blazor/Blazor/Modals/DeleteConfirmation.razor.cs diff --git a/Blazor/Blazor/App.razor b/Blazor/Blazor/App.razor index 623580d..54966a1 100644 --- a/Blazor/Blazor/App.razor +++ b/Blazor/Blazor/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/Blazor/Blazor/Blazor.csproj b/Blazor/Blazor/Blazor.csproj index 613ed31..05b5d69 100644 --- a/Blazor/Blazor/Blazor.csproj +++ b/Blazor/Blazor/Blazor.csproj @@ -8,6 +8,7 @@ + diff --git a/Blazor/Blazor/Factories/ItemFactory.cs b/Blazor/Blazor/Factories/ItemFactory.cs new file mode 100644 index 0000000..a46ec2d --- /dev/null +++ b/Blazor/Blazor/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/Blazor/Blazor/Modals/DeleteConfirmation.razor b/Blazor/Blazor/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..b565665 --- /dev/null +++ b/Blazor/Blazor/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/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..846860b --- /dev/null +++ b/Blazor/Blazor/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,38 @@ +using Blazor.Models; +using Blazor.Services; +using Blazored.Modal; +using Blazored.Modal.Services; +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/Blazor/Blazor/Pages/Edit.razor.cs b/Blazor/Blazor/Pages/Edit.razor.cs index ec3f108..442d2b0 100644 --- a/Blazor/Blazor/Pages/Edit.razor.cs +++ b/Blazor/Blazor/Pages/Edit.razor.cs @@ -1,4 +1,5 @@ -using Blazor.Models; +using Blazor.Factories; +using Blazor.Models; using Blazor.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; @@ -50,17 +51,7 @@ namespace Blazor.Pages } // 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() diff --git a/Blazor/Blazor/Pages/List.razor b/Blazor/Blazor/Pages/List.razor index 021548b..f0029b4 100644 --- a/Blazor/Blazor/Pages/List.razor +++ b/Blazor/Blazor/Pages/List.razor @@ -28,11 +28,6 @@ @context.DisplayName } - - - - Editer - @@ -48,4 +43,10 @@ + + + Editer + + + \ No newline at end of file diff --git a/Blazor/Blazor/Pages/List.razor.cs b/Blazor/Blazor/Pages/List.razor.cs index 5b31d3e..d8b686f 100644 --- a/Blazor/Blazor/Pages/List.razor.cs +++ b/Blazor/Blazor/Pages/List.razor.cs @@ -1,6 +1,9 @@ -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; @@ -21,6 +24,9 @@ namespace Blazor.Pages [Inject] public NavigationManager NavigationManager { get; set; } + [CascadingParameter] + public IModalService Modal { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) @@ -34,5 +40,24 @@ namespace Blazor.Pages totalItem = await DataService.Count(); } } + + 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); + } } } \ No newline at end of file diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml index 6612268..839e817 100644 --- a/Blazor/Blazor/Pages/_Layout.cshtml +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -11,6 +11,10 @@ + + + + @@ -28,10 +32,7 @@ + - - - - diff --git a/Blazor/Blazor/Program.cs b/Blazor/Blazor/Program.cs index 8525776..95494ac 100644 --- a/Blazor/Blazor/Program.cs +++ b/Blazor/Blazor/Program.cs @@ -6,6 +6,7 @@ using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Blazored.Modal; var builder = WebApplication.CreateBuilder(args); @@ -23,6 +24,8 @@ builder.Services builder.Services.AddBlazoredLocalStorage(); builder.Services.AddScoped(); +builder.Services.AddBlazoredModal(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index ca4d383..4176cd1 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -1,4 +1,5 @@ -using Blazor.Models; +using Blazor.Factories; +using Blazor.Models; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; @@ -32,17 +33,7 @@ namespace Blazor.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"); @@ -142,13 +133,31 @@ namespace Blazor.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/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs index 809af5f..e7d0728 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/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/Blazor/Blazor/_Imports.razor b/Blazor/Blazor/_Imports.razor index 500d784..415add7 100644 --- a/Blazor/Blazor/_Imports.razor +++ b/Blazor/Blazor/_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