diff --git a/Sources/MyFirstBlazor/Models/ItemModel.cs b/Sources/MyFirstBlazor/Models/ItemModel.cs new file mode 100644 index 0000000..0db9927 --- /dev/null +++ b/Sources/MyFirstBlazor/Models/ItemModel.cs @@ -0,0 +1,37 @@ +using System.ComponentModel.DataAnnotations; + +namespace MyFirstBlazor.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/Sources/MyFirstBlazor/MyFirstBlazor.csproj b/Sources/MyFirstBlazor/MyFirstBlazor.csproj index 6033f3c..5a078bb 100644 --- a/Sources/MyFirstBlazor/MyFirstBlazor.csproj +++ b/Sources/MyFirstBlazor/MyFirstBlazor.csproj @@ -7,6 +7,7 @@ + diff --git a/Sources/MyFirstBlazor/Pages/Add.razor b/Sources/MyFirstBlazor/Pages/Add.razor new file mode 100644 index 0000000..89a6b0f --- /dev/null +++ b/Sources/MyFirstBlazor/Pages/Add.razor @@ -0,0 +1,70 @@ +@page "/add" +@using MyFirstBlazor.Models; + +

Add

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/Sources/MyFirstBlazor/Pages/Add.razor.cs b/Sources/MyFirstBlazor/Pages/Add.razor.cs new file mode 100644 index 0000000..ccb911e --- /dev/null +++ b/Sources/MyFirstBlazor/Pages/Add.razor.cs @@ -0,0 +1,126 @@ +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; +using MyFirstBlazor.Models; + +namespace MyFirstBlazor.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/Sources/MyFirstBlazor/Pages/BlazorRoute.razor b/Sources/MyFirstBlazor/Pages/BlazorRoute.razor new file mode 100644 index 0000000..40307dd --- /dev/null +++ b/Sources/MyFirstBlazor/Pages/BlazorRoute.razor @@ -0,0 +1,4 @@ +@page "/BlazorRoute" +@page "/DifferentBlazorRoute" + +

Blazor routing

\ No newline at end of file diff --git a/Sources/MyFirstBlazor/Pages/List.razor b/Sources/MyFirstBlazor/Pages/List.razor index d8b62e5..db80728 100644 --- a/Sources/MyFirstBlazor/Pages/List.razor +++ b/Sources/MyFirstBlazor/Pages/List.razor @@ -4,6 +4,12 @@

List

+
+ + Ajouter + +
+ + + + @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + @@ -27,8 +45,3 @@ - -@code{ - @using MyFirstBlazor.Models; - private List? items; -} \ No newline at end of file diff --git a/Sources/MyFirstBlazor/Pages/List.razor.cs b/Sources/MyFirstBlazor/Pages/List.razor.cs index bb67b0d..8270368 100644 --- a/Sources/MyFirstBlazor/Pages/List.razor.cs +++ b/Sources/MyFirstBlazor/Pages/List.razor.cs @@ -1,34 +1,63 @@ -using Blazorise.DataGrid; +using Blazored.LocalStorage; +using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; using MyFirstBlazor.Models; - -public partial class List +namespace MyFirstBlazor.Pages { - private List? items; + public partial class List + { + private List items; - private int totalItem; + private int totalItem; - [Inject] - public HttpClient Http { get; set; } + [Inject] + public HttpClient Http { get; set; } - [Inject] - public NavigationManager NavigationManager { get; set; } + [Inject] + public ILocalStorageService LocalStorage { get; set; } - private async Task OnReadData(DataGridReadDataEventArgs e) - { - if (e.CancellationToken.IsCancellationRequested) + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + protected override async Task OnAfterRenderAsync(bool firstRender) { - return; - } + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } - // 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}FakeData.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + var currentData = await LocalStorage.GetItemAsync("data"); - if (!e.CancellationToken.IsCancellationRequested) + // 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}FakeData.json").Result; + await LocalStorage.SetItemAsync("data", originalData); + } + } + + private async Task OnReadData(DataGridReadDataEventArgs e) { - totalItem = (await Http.GetFromJsonAsync>($"{NavigationManager.BaseUri}FakeData.json")).Count; - items = new List(response); // an actual data for the current page + if (e.CancellationToken.IsCancellationRequested) + { + return; + } + + // 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 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); // an actual data for the current page + } } } -} \ No newline at end of file + +} diff --git a/Sources/MyFirstBlazor/Program.cs b/Sources/MyFirstBlazor/Program.cs index b5ca7cd..9f2acc8 100644 --- a/Sources/MyFirstBlazor/Program.cs +++ b/Sources/MyFirstBlazor/Program.cs @@ -1,3 +1,4 @@ +using Blazored.LocalStorage; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; @@ -16,6 +17,7 @@ builder.Services .AddBlazorise() .AddBootstrapProviders() .AddFontAwesomeIcons(); +builder.Services.AddBlazoredLocalStorage(); var app = builder.Build(); diff --git a/Sources/MyFirstBlazor/wwwroot/images/bob.png b/Sources/MyFirstBlazor/wwwroot/images/bob.png new file mode 100644 index 0000000..a12d412 Binary files /dev/null and b/Sources/MyFirstBlazor/wwwroot/images/bob.png differ diff --git a/Sources/MyFirstBlazor/wwwroot/images/default.png b/Sources/MyFirstBlazor/wwwroot/images/default.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/Sources/MyFirstBlazor/wwwroot/images/default.png differ diff --git a/Sources/MyFirstBlazor/wwwroot/images/magicmetal.png b/Sources/MyFirstBlazor/wwwroot/images/magicmetal.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/Sources/MyFirstBlazor/wwwroot/images/magicmetal.png differ