diff --git a/BlazorApp1/BlazorApp1.csproj b/BlazorApp1/BlazorApp1.csproj index 3656a14..c0eacf6 100644 --- a/BlazorApp1/BlazorApp1.csproj +++ b/BlazorApp1/BlazorApp1.csproj @@ -7,6 +7,7 @@ + diff --git a/BlazorApp1/Models/Items.cs b/BlazorApp1/Models/Item.cs similarity index 95% rename from BlazorApp1/Models/Items.cs rename to BlazorApp1/Models/Item.cs index fa46fc6..4f47361 100644 --- a/BlazorApp1/Models/Items.cs +++ b/BlazorApp1/Models/Item.cs @@ -1,6 +1,6 @@ namespace BlazorApp1.Models { - public class Items + public class Item { public int Id { get; set; } public string DisplayName { get; set; } diff --git a/BlazorApp1/Models/ItemModel.cs b/BlazorApp1/Models/ItemModel.cs new file mode 100644 index 0000000..770ecf9 --- /dev/null +++ b/BlazorApp1/Models/ItemModel.cs @@ -0,0 +1,37 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlazorApp1.Models +{ + public class ItemModel + { + public int Id { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "The display name must not exceed 50 characters.")] + public string DisplayName { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "The name must not exceed 50 characters.")] + [RegularExpression(@"^[a-z''-'\s]{1,40}$", ErrorMessage = "Only lowercase characters are accepted.")] + public string Name { get; set; } + + [Required] + [Range(1, 64)] + public int StackSize { get; set; } + + [Required] + [Range(1, 125)] + public int MaxDurability { get; set; } + + public List EnchantCategories { get; set; } + + public List RepairWith { get; set; } + + [Required] + [Range(typeof(bool), "true", "true", ErrorMessage = "You must agree to the terms.")] + public bool AcceptCondition { get; set; } + + [Required(ErrorMessage = "The image of the item is mandatory!")] + public byte[] ImageContent { get; set; } + } +} diff --git a/BlazorApp1/Pages/Add.razor b/BlazorApp1/Pages/Add.razor new file mode 100644 index 0000000..64b1710 --- /dev/null +++ b/BlazorApp1/Pages/Add.razor @@ -0,0 +1,69 @@ +@page "/add" + +

Add

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ 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/Add.razor.cs b/BlazorApp1/Pages/Add.razor.cs new file mode 100644 index 0000000..68c51af --- /dev/null +++ b/BlazorApp1/Pages/Add.razor.cs @@ -0,0 +1,125 @@ +using BlazorApp1.Models; +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; + +namespace BlazorApp1.Pages +{ + public partial class Add + { + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + /// + /// The default enchant categories. + /// + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + + /// + /// 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" }; + + /// + /// The current item model + /// + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + private async void HandleValidSubmit() + { + // Get the current data + var currentData = await LocalStorage.GetItemAsync>("data"); + + // Simulate the Id + itemModel.Id = currentData.Max(s => s.Id) + 1; + + // Add the item to the current data + currentData.Add(new Item + { + Id = itemModel.Id, + DisplayName = itemModel.DisplayName, + Name = itemModel.Name, + RepairWith = itemModel.RepairWith, + EnchantCategories = itemModel.EnchantCategories, + MaxDurability = itemModel.MaxDurability, + StackSize = itemModel.StackSize, + CreatedDate = DateTime.Now + }); + + // Save the image + var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images"); + + // Check if the folder "images" exist + if (!imagePathInfo.Exists) + { + imagePathInfo.Create(); + } + + // Determine the image name + var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png"); + + // Write the file content + await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent); + + // Save the data + await LocalStorage.SetItemAsync("data", currentData); + 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/BlazorRoute.razor b/BlazorApp1/Pages/BlazorRoute.razor new file mode 100644 index 0000000..40307dd --- /dev/null +++ b/BlazorApp1/Pages/BlazorRoute.razor @@ -0,0 +1,4 @@ +@page "/BlazorRoute" +@page "/DifferentBlazorRoute" + +

Blazor routing

\ No newline at end of file diff --git a/BlazorApp1/Pages/List.razor b/BlazorApp1/Pages/List.razor index 7e0a6db..5ec4d95 100644 --- a/BlazorApp1/Pages/List.razor +++ b/BlazorApp1/Pages/List.razor @@ -3,26 +3,44 @@

List

- + + Ajouter + + + + - - - - - + + + + @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + + + + + - @(string.Join(", ", ((Items)context).EnchantCategories)) + @(string.Join(", ", ((Item)context).EnchantCategories)) - + - @(string.Join(", ", ((Items)context).RepairWith)) + @(string.Join(", ", ((Item)context).RepairWith)) - + \ No newline at end of file diff --git a/BlazorApp1/Pages/list.razor.cs b/BlazorApp1/Pages/list.razor.cs index 030589e..9d9a230 100644 --- a/BlazorApp1/Pages/list.razor.cs +++ b/BlazorApp1/Pages/list.razor.cs @@ -1,4 +1,5 @@ using BlazorApp1.Models; +using Blazored.LocalStorage; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; @@ -6,17 +7,42 @@ namespace BlazorApp1.Pages { public partial class List { - private List items; + private List items; private int totalItem; [Inject] public HttpClient Http { get; set; } + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] public NavigationManager NavigationManager { get; set; } - private async Task OnReadData(DataGridReadDataEventArgs e) + protected override async Task OnAfterRenderAsync(bool firstRender) + { + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); + + // Check if data exist in the local storage + if (currentData == null) + { + // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) + var originalData = Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json").Result; + await LocalStorage.SetItemAsync("data", originalData); + } + } + + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) { @@ -24,13 +50,13 @@ namespace BlazorApp1.Pages } // When you use a real API, we use this follow code - //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); - var response = (await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); + var response = (await LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); if (!e.CancellationToken.IsCancellationRequested) { - totalItem = (await Http.GetFromJsonAsync>($"{NavigationManager.BaseUri}fake-data.json")).Count; - items = new List(response); // an actual data for the current page + totalItem = (await LocalStorage.GetItemAsync>("data")).Count; + items = new List(response); // an actual data for the current page } } } diff --git a/BlazorApp1/Program.cs b/BlazorApp1/Program.cs index daf28f7..7f683e4 100644 --- a/BlazorApp1/Program.cs +++ b/BlazorApp1/Program.cs @@ -1,4 +1,5 @@ using BlazorApp1.Data; +using Blazored.LocalStorage; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; @@ -16,8 +17,9 @@ builder.Services .AddBlazorise() .AddBootstrapProviders() .AddFontAwesomeIcons(); +builder.Services.AddBlazoredLocalStorage(); -var app = builder.Build(); +var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) diff --git a/BlazorApp1/_Imports.razor b/BlazorApp1/_Imports.razor index 01cb17b..6ccf59e 100644 --- a/BlazorApp1/_Imports.razor +++ b/BlazorApp1/_Imports.razor @@ -8,4 +8,4 @@ @using Microsoft.JSInterop @using BlazorApp1 @using BlazorApp1.Shared -@using Blazorise.DataGrid +@using Blazorise.DataGrid \ No newline at end of file diff --git a/BlazorApp1/wwwroot/images/default.png b/BlazorApp1/wwwroot/images/default.png new file mode 100644 index 0000000..756dc18 Binary files /dev/null and b/BlazorApp1/wwwroot/images/default.png differ diff --git a/BlazorApp1/wwwroot/images/emeraude.png b/BlazorApp1/wwwroot/images/emeraude.png new file mode 100644 index 0000000..7e8b7c2 Binary files /dev/null and b/BlazorApp1/wwwroot/images/emeraude.png differ