From 0c1ab868c432fe893da1e6438e15f5ee1996b829 Mon Sep 17 00:00:00 2001 From: Noemie VARJABEDIAN Date: Fri, 28 Oct 2022 15:31:13 +0200 Subject: [PATCH] TP2 --- BlazorTuto/BlazorTuto.csproj | 3 +- BlazorTuto/Models/ItemModel.cs | 37 ++++++++++ BlazorTuto/Pages/Add.razor | 69 ++++++++++++++++++ BlazorTuto/Pages/Add.razor.cs | 121 ++++++++++++++++++++++++++++++++ BlazorTuto/Pages/List.razor | 60 ++++++++-------- BlazorTuto/Pages/List.razor.cs | 57 ++++++++++++--- BlazorTuto/Pages/_Layout.cshtml | 5 ++ BlazorTuto/Program.cs | 3 + BlazorTuto/_Imports.razor | 4 ++ 9 files changed, 319 insertions(+), 40 deletions(-) create mode 100644 BlazorTuto/Models/ItemModel.cs create mode 100644 BlazorTuto/Pages/Add.razor create mode 100644 BlazorTuto/Pages/Add.razor.cs diff --git a/BlazorTuto/BlazorTuto.csproj b/BlazorTuto/BlazorTuto.csproj index 6033f3c..93b929d 100644 --- a/BlazorTuto/BlazorTuto.csproj +++ b/BlazorTuto/BlazorTuto.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,6 +7,7 @@ + diff --git a/BlazorTuto/Models/ItemModel.cs b/BlazorTuto/Models/ItemModel.cs new file mode 100644 index 0000000..5a2aeff --- /dev/null +++ b/BlazorTuto/Models/ItemModel.cs @@ -0,0 +1,37 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlazorTuto.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/BlazorTuto/Pages/Add.razor b/BlazorTuto/Pages/Add.razor new file mode 100644 index 0000000..0ed932c --- /dev/null +++ b/BlazorTuto/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/BlazorTuto/Pages/Add.razor.cs b/BlazorTuto/Pages/Add.razor.cs new file mode 100644 index 0000000..8e6c958 --- /dev/null +++ b/BlazorTuto/Pages/Add.razor.cs @@ -0,0 +1,121 @@ +using Blazored.LocalStorage; +using BlazorTuto.Models; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; + +namespace BlazorTuto.Pages +{ + public partial class Add + { + [Inject] + public ILocalStorageService LocalStorage { 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); + } + + 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/BlazorTuto/Pages/List.razor b/BlazorTuto/Pages/List.razor index b916cb8..90c6b61 100644 --- a/BlazorTuto/Pages/List.razor +++ b/BlazorTuto/Pages/List.razor @@ -1,34 +1,34 @@ @page "/list" +@using BlazorTuto.Models

List

-@if (items != null) -{ - - - - - - - - - - - - - - @foreach (var item in items) - { - - - - - - - - - - } - -
IdDisplay NameStack SizeMaximum DurabilityEnchant CategoriesRepair WithCreated Date
@item.Id@item.DisplayName@item.StackSize@item.MaxDurability@(string.Join(", ", item.EnchantCategories))@(string.Join(", ", item.RepairWith))@item.CreatedDate.ToShortDateString()
-} \ No newline at end of file +
+ + Ajouter + +
+ + + + + + + + + @(string.Join(", ", ((Item)context).EnchantCategories)) + + + + + @(string.Join(", ", ((Item)context).RepairWith)) + + + + \ No newline at end of file diff --git a/BlazorTuto/Pages/List.razor.cs b/BlazorTuto/Pages/List.razor.cs index f2827b3..0f0db7a 100644 --- a/BlazorTuto/Pages/List.razor.cs +++ b/BlazorTuto/Pages/List.razor.cs @@ -1,21 +1,60 @@ -using BlazorTuto.Models; +using Blazored.LocalStorage; +using Blazorise.DataGrid; +using BlazorTuto.Models; using Microsoft.AspNetCore.Components; namespace BlazorTuto.Pages { - public partial class List + public partial class List + { + private List items; + + private int totalItem; + + [Inject] + public HttpClient Http { get; set; } + + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + protected override async Task OnAfterRenderAsync(bool firstRender) { - private Item[] items; + // Do not treat this action if is not the first render + if (!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); - [Inject] - public HttpClient Http { get; set; } + // 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) + { + return; + } - [Inject] - public NavigationManager NavigationManager { get; set; } + // 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(); - protected override async Task OnInitializedAsync() + if (!e.CancellationToken.IsCancellationRequested) { - items = await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json"); + totalItem = (await LocalStorage.GetItemAsync>("data")).Count; + items = new List(response); // an actual data for the current page } } + } } diff --git a/BlazorTuto/Pages/_Layout.cshtml b/BlazorTuto/Pages/_Layout.cshtml index 7843c63..384459a 100644 --- a/BlazorTuto/Pages/_Layout.cshtml +++ b/BlazorTuto/Pages/_Layout.cshtml @@ -28,5 +28,10 @@ + + + + + diff --git a/BlazorTuto/Program.cs b/BlazorTuto/Program.cs index 677a992..24aa501 100644 --- a/BlazorTuto/Program.cs +++ b/BlazorTuto/Program.cs @@ -1,3 +1,4 @@ +using Blazored.LocalStorage; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; @@ -17,6 +18,8 @@ builder.Services .AddBootstrapProviders() .AddFontAwesomeIcons(); +builder.Services.AddBlazoredLocalStorage(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/BlazorTuto/_Imports.razor b/BlazorTuto/_Imports.razor index 458d156..a65b08a 100644 --- a/BlazorTuto/_Imports.razor +++ b/BlazorTuto/_Imports.razor @@ -8,3 +8,7 @@ @using Microsoft.JSInterop @using BlazorTuto @using BlazorTuto.Shared +@using Blazorise.Bootstrap +@using Blazorise.Icons.FontAwesome +@using Blazorise.DataGrid +