diff --git a/.vs/Blazor/v17/.suo b/.vs/Blazor/v17/.suo index 4869c96..e22e6e7 100644 Binary files a/.vs/Blazor/v17/.suo and b/.vs/Blazor/v17/.suo differ diff --git a/.vs/BlazorApp1/FileContentIndex/0a4b8764-b4b2-4dac-8581-8b63cebf66cc.vsidx b/.vs/BlazorApp1/FileContentIndex/0a4b8764-b4b2-4dac-8581-8b63cebf66cc.vsidx new file mode 100644 index 0000000..e749c44 Binary files /dev/null and b/.vs/BlazorApp1/FileContentIndex/0a4b8764-b4b2-4dac-8581-8b63cebf66cc.vsidx differ diff --git a/.vs/BlazorApp1/FileContentIndex/18e79921-6773-40b0-839c-9340e29b62e1.vsidx b/.vs/BlazorApp1/FileContentIndex/18e79921-6773-40b0-839c-9340e29b62e1.vsidx deleted file mode 100644 index 3284a0d..0000000 Binary files a/.vs/BlazorApp1/FileContentIndex/18e79921-6773-40b0-839c-9340e29b62e1.vsidx and /dev/null differ diff --git a/.vs/BlazorApp1/FileContentIndex/65038190-bba3-4350-bfcf-1447f2c1af3d.vsidx b/.vs/BlazorApp1/FileContentIndex/65038190-bba3-4350-bfcf-1447f2c1af3d.vsidx deleted file mode 100644 index dbd51fb..0000000 Binary files a/.vs/BlazorApp1/FileContentIndex/65038190-bba3-4350-bfcf-1447f2c1af3d.vsidx and /dev/null differ diff --git a/.vs/BlazorApp1/FileContentIndex/9594cce6-891d-4f0d-b045-e9a4da3276e5.vsidx b/.vs/BlazorApp1/FileContentIndex/9594cce6-891d-4f0d-b045-e9a4da3276e5.vsidx deleted file mode 100644 index 0defccd..0000000 Binary files a/.vs/BlazorApp1/FileContentIndex/9594cce6-891d-4f0d-b045-e9a4da3276e5.vsidx and /dev/null differ diff --git a/.vs/BlazorApp1/FileContentIndex/96ff63ed-84b1-4be9-894c-2b151224e050.vsidx b/.vs/BlazorApp1/FileContentIndex/96ff63ed-84b1-4be9-894c-2b151224e050.vsidx deleted file mode 100644 index 477bae7..0000000 Binary files a/.vs/BlazorApp1/FileContentIndex/96ff63ed-84b1-4be9-894c-2b151224e050.vsidx and /dev/null differ diff --git a/.vs/BlazorApp1/FileContentIndex/b13fb56c-5cd0-48a8-b071-6b8c4fd80768.vsidx b/.vs/BlazorApp1/FileContentIndex/b13fb56c-5cd0-48a8-b071-6b8c4fd80768.vsidx new file mode 100644 index 0000000..5bee6c7 Binary files /dev/null and b/.vs/BlazorApp1/FileContentIndex/b13fb56c-5cd0-48a8-b071-6b8c4fd80768.vsidx differ diff --git a/.vs/BlazorApp1/v17/.suo b/.vs/BlazorApp1/v17/.suo index b73e363..65222f3 100644 Binary files a/.vs/BlazorApp1/v17/.suo and b/.vs/BlazorApp1/v17/.suo differ diff --git a/.vs/ProjectEvaluation/blazorapp1.metadata.v2 b/.vs/ProjectEvaluation/blazorapp1.metadata.v2 index 78c0b5e..ad2ed6e 100644 Binary files a/.vs/ProjectEvaluation/blazorapp1.metadata.v2 and b/.vs/ProjectEvaluation/blazorapp1.metadata.v2 differ diff --git a/.vs/ProjectEvaluation/blazorapp1.projects.v2 b/.vs/ProjectEvaluation/blazorapp1.projects.v2 index 26fe1a6..7459264 100644 Binary files a/.vs/ProjectEvaluation/blazorapp1.projects.v2 and b/.vs/ProjectEvaluation/blazorapp1.projects.v2 differ diff --git a/BlazorApp1/BlazorApp1.csproj b/BlazorApp1/BlazorApp1.csproj index 5a078bb..40bd5d5 100644 --- a/BlazorApp1/BlazorApp1.csproj +++ b/BlazorApp1/BlazorApp1.csproj @@ -8,6 +8,7 @@ + diff --git a/BlazorApp1/Factories/ItemFactory.cs b/BlazorApp1/Factories/ItemFactory.cs new file mode 100644 index 0000000..8d96097 --- /dev/null +++ b/BlazorApp1/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using BlazorApp1.Models; + +namespace BlazorApp1.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/BlazorApp1/Pages/Edit.razor b/BlazorApp1/Pages/Edit.razor index 08eb818..57cedae 100644 --- a/BlazorApp1/Pages/Edit.razor +++ b/BlazorApp1/Pages/Edit.razor @@ -2,4 +2,81 @@

Edit

-
My paremeter: @Id
\ No newline at end of file + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

+ @foreach (var item in enchantCategories) + { + + } +
+

+

+ Repair with: +

+ @foreach (var item in repairWith) + { + + } +
+

+

+ +

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/BlazorApp1/Pages/Edit.razor.cs b/BlazorApp1/Pages/Edit.razor.cs index b557f74..adc909f 100644 --- a/BlazorApp1/Pages/Edit.razor.cs +++ b/BlazorApp1/Pages/Edit.razor.cs @@ -1,4 +1,8 @@ -using Microsoft.AspNetCore.Components; +using BlazorApp1.Factories; +using BlazorApp1.Models; +using BlazorApp1.Sevices; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; namespace BlazorApp1.Pages { @@ -6,5 +10,101 @@ namespace BlazorApp1.Pages { [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/BlazorApp1/Pages/List.razor b/BlazorApp1/Pages/List.razor index 3d32f93..c35b8d1 100644 --- a/BlazorApp1/Pages/List.razor +++ b/BlazorApp1/Pages/List.razor @@ -45,6 +45,7 @@ Editer + \ No newline at end of file diff --git a/BlazorApp1/Pages/List.razor.cs b/BlazorApp1/Pages/List.razor.cs index 41e06e6..e9a5cc3 100644 --- a/BlazorApp1/Pages/List.razor.cs +++ b/BlazorApp1/Pages/List.razor.cs @@ -2,6 +2,7 @@ using Blazored.LocalStorage; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; +// namespace BlazorApp1.Pages { @@ -30,6 +31,11 @@ namespace BlazorApp1.Pages totalItem = await DataService.Count(); } } + + private void OnDelete(int id) + { + + } } /*public partial class List diff --git a/BlazorApp1/Pages/ParameterParent.razor b/BlazorApp1/Pages/ParameterParent.razor new file mode 100644 index 0000000..6ed9cff --- /dev/null +++ b/BlazorApp1/Pages/ParameterParent.razor @@ -0,0 +1,9 @@ +@page "/parameter-parent" + +

Child component (without attribute values)

+ + + +

Child component (with attribute values)

+ + \ No newline at end of file diff --git a/BlazorApp1/Pages/ParameterParent2.razor b/BlazorApp1/Pages/ParameterParent2.razor new file mode 100644 index 0000000..f93305c --- /dev/null +++ b/BlazorApp1/Pages/ParameterParent2.razor @@ -0,0 +1,24 @@ +@page "/parameter-parent-2" + + + + + + + + + +@code { + private string title = "From Parent field"; + private PanelData panelData = new(); + + private string GetTitle() + { + return "From Parent method"; + } + + private class PanelData + { + public string Title { get; set; } = "From Parent object"; + } +} \ No newline at end of file diff --git a/BlazorApp1/Pages/ParameterParent3.razor b/BlazorApp1/Pages/ParameterParent3.razor new file mode 100644 index 0000000..3056b4c --- /dev/null +++ b/BlazorApp1/Pages/ParameterParent3.razor @@ -0,0 +1,14 @@ +@page "/parameter-parent-3" + + + +@code { + private PanelData panelData = new(); + + private string GetTitle() => $"Set by {panelData.Title}"; + + private class PanelData + { + public string Title { get; set; } = "Parent"; + } +} \ No newline at end of file diff --git a/BlazorApp1/Pages/RouteParameter.razor b/BlazorApp1/Pages/RouteParameter.razor index 46ba1d2..f53cb71 100644 --- a/BlazorApp1/Pages/RouteParameter.razor +++ b/BlazorApp1/Pages/RouteParameter.razor @@ -1,8 +1,13 @@ -@page "/RouteParameter/{text}" +@page "/RouteParameter/{text?}"

Blazor is @Text!

@code { [Parameter] public string? Text { get; set; } + + protected override void OnParametersSet() + { + Text = Text ?? "fantastic"; + } } \ No newline at end of file diff --git a/BlazorApp1/Pages/User.razor b/BlazorApp1/Pages/User.razor new file mode 100644 index 0000000..d826c7f --- /dev/null +++ b/BlazorApp1/Pages/User.razor @@ -0,0 +1,17 @@ +@page "/user/{Id:int}/{Option:bool?}" + +

+ Id: @Id +

+ +

+ Option: @Option +

+ +@code { + [Parameter] + public int Id { get; set; } + + [Parameter] + public bool Option { get; set; } +} \ No newline at end of file diff --git a/BlazorApp1/PanelBody.cs b/BlazorApp1/PanelBody.cs new file mode 100644 index 0000000..0fcc049 --- /dev/null +++ b/BlazorApp1/PanelBody.cs @@ -0,0 +1,8 @@ +namespace BlazorApp1 +{ + public class PanelBody + { + public string? Text { get; set; } + public string? Style { get; set; } + } +} diff --git a/BlazorApp1/Program.cs b/BlazorApp1/Program.cs index 2fbe013..44d34bb 100644 --- a/BlazorApp1/Program.cs +++ b/BlazorApp1/Program.cs @@ -7,6 +7,7 @@ using Blazorise.DataGrid; using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Blazored.Modal; var builder = WebApplication.CreateBuilder(args); @@ -26,6 +27,7 @@ builder.Services .AddFontAwesomeIcons(); builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBlazoredModal(); var app = builder.Build(); diff --git a/BlazorApp1/Sevices/DataLocalService.cs b/BlazorApp1/Sevices/DataLocalService.cs index 91c5c45..dae506e 100644 --- a/BlazorApp1/Sevices/DataLocalService.cs +++ b/BlazorApp1/Sevices/DataLocalService.cs @@ -1,4 +1,5 @@ -using BlazorApp1.Models; +using BlazorApp1.Factories; +using BlazorApp1.Models; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; @@ -32,17 +33,7 @@ namespace BlazorApp1.Sevices 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 BlazorApp1.Sevices 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 datas + 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/BlazorApp1/Sevices/IDataService.cs b/BlazorApp1/Sevices/IDataService.cs index 2f72d9b..d613315 100644 --- a/BlazorApp1/Sevices/IDataService.cs +++ b/BlazorApp1/Sevices/IDataService.cs @@ -13,5 +13,7 @@ namespace BlazorApp1.Sevices Task GetById(int id); Task Update(int id, ItemModel model); + + Task Delete(int id); } } diff --git a/BlazorApp1/Shared/NavMenu.razor b/BlazorApp1/Shared/NavMenu.razor index 142a4d2..4aefb2a 100644 --- a/BlazorApp1/Shared/NavMenu.razor +++ b/BlazorApp1/Shared/NavMenu.razor @@ -14,6 +14,16 @@ List + +