diff --git a/myBlazorApp/myBlazorApp/App.razor b/myBlazorApp/myBlazorApp/App.razor index 60f5744..d06cf24 100644 --- a/myBlazorApp/myBlazorApp/App.razor +++ b/myBlazorApp/myBlazorApp/App.razor @@ -1,13 +1,10 @@ - + - Not found - -

Sorry, there's nothing at this address.

-
+

Sorry, there's nothing at this address.

diff --git a/myBlazorApp/myBlazorApp/Models/ItemModel.cs b/myBlazorApp/myBlazorApp/Models/ItemModel.cs new file mode 100644 index 0000000..e15df61 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Models/ItemModel.cs @@ -0,0 +1,40 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace myBlazorApp.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/myBlazorApp/myBlazorApp/Pages/Add.razor b/myBlazorApp/myBlazorApp/Pages/Add.razor new file mode 100644 index 0000000..64b1710 --- /dev/null +++ b/myBlazorApp/myBlazorApp/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/myBlazorApp/myBlazorApp/Pages/Add.razor.cs b/myBlazorApp/myBlazorApp/Pages/Add.razor.cs new file mode 100644 index 0000000..306432e --- /dev/null +++ b/myBlazorApp/myBlazorApp/Pages/Add.razor.cs @@ -0,0 +1,128 @@ +using System; +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using myBlazorApp.Models; + +namespace myBlazorApp.Pages +{ + public partial class Add + { + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + [Inject] + public NavigationManager NavigationManager { 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/myBlazorApp/myBlazorApp/Pages/BlazorRoute.razor b/myBlazorApp/myBlazorApp/Pages/BlazorRoute.razor new file mode 100644 index 0000000..40307dd --- /dev/null +++ b/myBlazorApp/myBlazorApp/Pages/BlazorRoute.razor @@ -0,0 +1,4 @@ +@page "/BlazorRoute" +@page "/DifferentBlazorRoute" + +

Blazor routing

\ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Pages/List.razor b/myBlazorApp/myBlazorApp/Pages/List.razor index 0523745..a68a3ea 100644 --- a/myBlazorApp/myBlazorApp/Pages/List.razor +++ b/myBlazorApp/myBlazorApp/Pages/List.razor @@ -4,11 +4,32 @@

List

+
+ + Ajouter :) + +
+ + + + @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + diff --git a/myBlazorApp/myBlazorApp/Pages/List.razor.cs b/myBlazorApp/myBlazorApp/Pages/List.razor.cs index 519aff2..4f9f113 100644 --- a/myBlazorApp/myBlazorApp/Pages/List.razor.cs +++ b/myBlazorApp/myBlazorApp/Pages/List.razor.cs @@ -1,4 +1,6 @@ using System; +using Blazored.LocalStorage; +using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; using myBlazorApp.Models; @@ -6,17 +8,52 @@ namespace myBlazorApp.Pages { public partial class List { - private Item[] items; + private List items; + + private int totalItem { get; set; } [Inject] public HttpClient Http { get; set; } + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] public NavigationManager NavigationManager { get; set; } - protected override async Task OnInitializedAsync() + protected override async Task OnAfterRenderAsync(bool firstRender) { - items = await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json"); + if(!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); + + if (currentData == null) + { + 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) + { + return; + } + var response = (await LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + + if (!e.CancellationToken.IsCancellationRequested) + { + totalItem = (await LocalStorage.GetItemAsync>("data")).Count; + items = new List(response); + } } } } diff --git a/myBlazorApp/myBlazorApp/Program.cs b/myBlazorApp/myBlazorApp/Program.cs index 4511297..200afef 100644 --- a/myBlazorApp/myBlazorApp/Program.cs +++ b/myBlazorApp/myBlazorApp/Program.cs @@ -1,8 +1,10 @@ -using Blazorise; +using Blazored.LocalStorage; +using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Blazored.LocalStorage; using myBlazorApp.Data; var builder = WebApplication.CreateBuilder(args); @@ -15,6 +17,7 @@ builder.Services.AddHttpClient(); builder.Services.AddBlazorise(); builder.Services.AddBootstrapProviders(); builder.Services.AddFontAwesomeIcons(); +builder.Services.AddBlazoredLocalStorage(); var app = builder.Build(); diff --git a/myBlazorApp/myBlazorApp/myBlazorApp.csproj b/myBlazorApp/myBlazorApp/myBlazorApp.csproj index f21e32f..b50b14b 100644 --- a/myBlazorApp/myBlazorApp/myBlazorApp.csproj +++ b/myBlazorApp/myBlazorApp/myBlazorApp.csproj @@ -12,6 +12,7 @@ + @@ -21,5 +22,6 @@ + diff --git a/myBlazorApp/myBlazorApp/wwwroot/images/harry potter.png b/myBlazorApp/myBlazorApp/wwwroot/images/harry potter.png new file mode 100644 index 0000000..0bb8b4a Binary files /dev/null and b/myBlazorApp/myBlazorApp/wwwroot/images/harry potter.png differ diff --git a/myBlazorApp/myBlazorApp/wwwroot/images/hermione granger.png b/myBlazorApp/myBlazorApp/wwwroot/images/hermione granger.png new file mode 100644 index 0000000..e502beb Binary files /dev/null and b/myBlazorApp/myBlazorApp/wwwroot/images/hermione granger.png differ diff --git a/myBlazorApp/myBlazorApp/wwwroot/images/luna lovegood.png b/myBlazorApp/myBlazorApp/wwwroot/images/luna lovegood.png new file mode 100644 index 0000000..f279c43 Binary files /dev/null and b/myBlazorApp/myBlazorApp/wwwroot/images/luna lovegood.png differ diff --git a/myBlazorApp/myBlazorApp/wwwroot/images/ron weasley.png b/myBlazorApp/myBlazorApp/wwwroot/images/ron weasley.png new file mode 100644 index 0000000..a53df5f Binary files /dev/null and b/myBlazorApp/myBlazorApp/wwwroot/images/ron weasley.png differ