diff --git a/App.razor b/App.razor index 111b4cd..beba7bf 100644 --- a/App.razor +++ b/App.razor @@ -1,4 +1,5 @@ - + + @@ -6,3 +7,4 @@

Sorry, there's nothing at this address.

+
\ No newline at end of file diff --git a/Factories/ItemFactory.cs.cs b/Factories/ItemFactory.cs.cs new file mode 100644 index 0000000..c07693f --- /dev/null +++ b/Factories/ItemFactory.cs.cs @@ -0,0 +1,48 @@ +using ProjetBlaser.Models; + +namespace ProjetBlaser.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/Pages/Edit.razor b/Pages/Edit.razor index d201ae0..3512c00 100644 --- a/Pages/Edit.razor +++ b/Pages/Edit.razor @@ -1,3 +1,82 @@ @page "/edit/{Id:int}" +

Edit

-
Mon paramètre: @Id
+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+

+ +

+ + +
diff --git a/Pages/Edit.razor.cs b/Pages/Edit.razor.cs index 39df9c7..ff58b5f 100644 --- a/Pages/Edit.razor.cs +++ b/Pages/Edit.razor.cs @@ -1,10 +1,123 @@ using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using ProjetBlaser.Factories; +using ProjetBlaser.Models; +using ProjetBlaser.Services; namespace ProjetBlaser.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 = 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 7b223e8..4abdf7a 100644 --- a/Pages/List.razor +++ b/Pages/List.razor @@ -44,6 +44,7 @@ Editer + \ No newline at end of file diff --git a/Pages/List.razor.cs b/Pages/List.razor.cs index c71334b..6c0e92e 100644 --- a/Pages/List.razor.cs +++ b/Pages/List.razor.cs @@ -31,5 +31,9 @@ namespace ProjetBlaser.Pages totalItem = await DataService.Count(); } } + private void OnDelete(int id) + { + + } } } diff --git a/Pages/ParameterParen.razor b/Pages/ParameterParen.razor index 7b5ae80..66aacb0 100644 --- a/Pages/ParameterParen.razor +++ b/Pages/ParameterParen.razor @@ -1,4 +1,5 @@ @page "/parameter-parent" +@using ProjetBlaser.Models

Child component (without attribute values)

diff --git a/Pages/User.razor b/Pages/User.razor index 907e345..d08987f 100644 --- a/Pages/User.razor +++ b/Pages/User.razor @@ -1,4 +1,4 @@ -@page "/user/{Id:int}/{Option:bool?}" +@page "/user/{Id:int}"

Id: @Id diff --git a/Pages/_Layout.cshtml b/Pages/_Layout.cshtml index 73357e2..b7ebeb0 100644 --- a/Pages/_Layout.cshtml +++ b/Pages/_Layout.cshtml @@ -29,7 +29,9 @@ + + diff --git a/Program.cs b/Program.cs index 98dbedc..052e2f8 100644 --- a/Program.cs +++ b/Program.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using ProjetBlaser.Data; using ProjetBlaser.Services; +using Blazored.Modal; var builder = WebApplication.CreateBuilder(args); @@ -15,6 +16,7 @@ builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); builder.Services.AddHttpClient(); builder.Services.AddScoped(); +builder.Services.AddBlazoredModal(); builder.Services .AddBlazorise() .AddBootstrapProviders() diff --git a/ProjetBlaser.csproj b/ProjetBlaser.csproj index c0eacf6..6c93f4c 100644 --- a/ProjetBlaser.csproj +++ b/ProjetBlaser.csproj @@ -8,9 +8,14 @@ + + + + + diff --git a/Services/DataLocalService.cs b/Services/DataLocalService.cs index 0309c4d..f80a4c9 100644 --- a/Services/DataLocalService.cs +++ b/Services/DataLocalService.cs @@ -1,5 +1,6 @@ using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; +using ProjetBlaser.Factories; using ProjetBlaser.Models; namespace ProjetBlaser.Services @@ -30,6 +31,7 @@ namespace ProjetBlaser.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 @@ -107,6 +109,7 @@ namespace ProjetBlaser.Services // Get the item int the list var item = currentData.FirstOrDefault(w => w.Id == id); + ItemFactory.Update(item, model); // Check if item exist if (item == null) @@ -152,5 +155,29 @@ namespace ProjetBlaser.Services // 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 01c9c6b..db5f8fb 100644 --- a/Services/IDataService.cs +++ b/Services/IDataService.cs @@ -13,5 +13,6 @@ namespace ProjetBlaser.Services Task GetById(int id); Task Update(int id, ItemModel model); + Task Delete(int id); } } diff --git a/wwwroot/images/furnafix.png b/wwwroot/images/furnafix.png new file mode 100644 index 0000000..2b391df Binary files /dev/null and b/wwwroot/images/furnafix.png differ diff --git a/wwwroot/images/noe.png b/wwwroot/images/noe.png new file mode 100644 index 0000000..2b391df Binary files /dev/null and b/wwwroot/images/noe.png differ