From b6a4183733e66313b2c04e88536e47b16a397de9 Mon Sep 17 00:00:00 2001 From: Noemie VARJABEDIAN Date: Tue, 15 Nov 2022 12:06:37 +0100 Subject: [PATCH] TP3 --- BlazorTuto/Pages/Add.razor.cs | 61 +++------ BlazorTuto/Pages/List.razor | 17 +++ BlazorTuto/Pages/List.razor.cs | 3 + BlazorTuto/Program.cs | 3 +- BlazorTuto/Services/DataLocalService.cs | 157 ++++++++++++++++++++++++ BlazorTuto/Services/IDataService.cs | 17 +++ BlazorTuto/wwwroot/images/index.png | Bin 0 -> 2267 bytes 7 files changed, 211 insertions(+), 47 deletions(-) create mode 100644 BlazorTuto/Services/DataLocalService.cs create mode 100644 BlazorTuto/Services/IDataService.cs create mode 100644 BlazorTuto/wwwroot/images/index.png diff --git a/BlazorTuto/Pages/Add.razor.cs b/BlazorTuto/Pages/Add.razor.cs index 8e6c958..2d054e8 100644 --- a/BlazorTuto/Pages/Add.razor.cs +++ b/BlazorTuto/Pages/Add.razor.cs @@ -2,27 +2,17 @@ using BlazorTuto.Models; using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Components; +using BlazorTuto.Services; 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 /// @@ -32,44 +22,22 @@ namespace BlazorTuto.Pages 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"); + /// + /// 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" }; - // Check if the folder "images" exist - if (!imagePathInfo.Exists) - { - imagePathInfo.Create(); - } + [Inject] + public IDataService DataService { get; set; } - // Determine the image name - var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png"); + [Inject] + public NavigationManager NavigationManager { get; set; } - // Write the file content - await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent); + private async void HandleValidSubmit() + { + await DataService.Add(itemModel); - // Save the data - await LocalStorage.SetItemAsync("data", currentData); + NavigationManager.NavigateTo("list"); } private async Task LoadImage(InputFileChangeEventArgs e) @@ -117,5 +85,6 @@ namespace BlazorTuto.Pages itemModel.RepairWith.Remove(item); } } + } -} +} \ No newline at end of file diff --git a/BlazorTuto/Pages/List.razor b/BlazorTuto/Pages/List.razor index 90c6b61..0dc02b3 100644 --- a/BlazorTuto/Pages/List.razor +++ b/BlazorTuto/Pages/List.razor @@ -17,6 +17,18 @@ ShowPager Responsive> + + + @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + @@ -31,4 +43,9 @@ + + + Editer + + \ No newline at end of file diff --git a/BlazorTuto/Pages/List.razor.cs b/BlazorTuto/Pages/List.razor.cs index 0f0db7a..04b34d9 100644 --- a/BlazorTuto/Pages/List.razor.cs +++ b/BlazorTuto/Pages/List.razor.cs @@ -17,6 +17,9 @@ namespace BlazorTuto.Pages [Inject] public ILocalStorageService LocalStorage { get; set; } + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] public NavigationManager NavigationManager { get; set; } diff --git a/BlazorTuto/Program.cs b/BlazorTuto/Program.cs index 24aa501..a231309 100644 --- a/BlazorTuto/Program.cs +++ b/BlazorTuto/Program.cs @@ -3,6 +3,7 @@ using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using BlazorTuto.Data; +using BlazorTuto.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; @@ -17,7 +18,7 @@ builder.Services .AddBlazorise() .AddBootstrapProviders() .AddFontAwesomeIcons(); - +builder.Services.AddScoped(); builder.Services.AddBlazoredLocalStorage(); var app = builder.Build(); diff --git a/BlazorTuto/Services/DataLocalService.cs b/BlazorTuto/Services/DataLocalService.cs new file mode 100644 index 0000000..b632736 --- /dev/null +++ b/BlazorTuto/Services/DataLocalService.cs @@ -0,0 +1,157 @@ +using Blazored.LocalStorage; +using BlazorTuto.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTuto.Services +{ + public class DataLocalService : IDataService + { + private readonly HttpClient _http; + private readonly ILocalStorageService _localStorage; + private readonly NavigationManager _navigationManager; + private readonly IWebHostEnvironment _webHostEnvironment; + + public DataLocalService( + ILocalStorageService localStorage, + HttpClient http, + IWebHostEnvironment webHostEnvironment, + NavigationManager navigationManager) + { + _localStorage = localStorage; + _http = http; + _webHostEnvironment = webHostEnvironment; + _navigationManager = navigationManager; + } + + public async Task Add(ItemModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Simulate the Id + model.Id = currentData.Max(s => s.Id) + 1; + + // Add the item to the current data + currentData.Add(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 + }); + + // 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}/{model.Name}.png"); + + // Write the file content + await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + public async Task Count() + { + return (await _localStorage.GetItemAsync("data")).Length; + } + + public async Task> List(int currentPage, int pageSize) + { + // Load data from the local storage + 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 + var originalData = await _http.GetFromJsonAsync($"{_navigationManager.BaseUri}fake-data.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); + } + + public async Task GetById(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); + + // Check if item exist + if (item == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + return item; + } + + public async Task Update(int id, ItemModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the item int the list + var item = currentData.FirstOrDefault(w => w.Id == id); + + // Check if item exist + if (item == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + // Save the image + var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); + + // Check if the folder "images" exist + if (!imagePathInfo.Exists) + { + imagePathInfo.Create(); + } + + // Delete the previous image + if (item.Name != model.Name) + { + var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png"); + + if (oldFileName.Exists) + { + File.Delete(oldFileName.FullName); + } + } + + // Determine the image name + var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); + + // Write the file content + await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); + + // Modify the content of the item + 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; + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + } +} diff --git a/BlazorTuto/Services/IDataService.cs b/BlazorTuto/Services/IDataService.cs new file mode 100644 index 0000000..97ce172 --- /dev/null +++ b/BlazorTuto/Services/IDataService.cs @@ -0,0 +1,17 @@ +using BlazorTuto.Models; + +namespace BlazorTuto.Services +{ + public interface IDataService + { + Task Add(ItemModel model); + + Task Count(); + + Task> List(int currentPage, int pageSize); + + Task GetById(int id); + + Task Update(int id, ItemModel model); + } +} diff --git a/BlazorTuto/wwwroot/images/index.png b/BlazorTuto/wwwroot/images/index.png new file mode 100644 index 0000000000000000000000000000000000000000..a7446c9e8a8c755f1e21faf389634136d44fed6d GIT binary patch literal 2267 zcmeHIXBIq!$_ocGH~zGZI(lvb1m006)n z*5;1;HEW*%QWE<)?4{2);XtUPl_>z%dun#Sk$ecXg8~56=`uSWhX4Qxz%5&6i`?AY zyu7@(Z{OzU=NA+d6c-nll$4;+=+e^Cva+)B^74v`3JeB=#bPTfD{(knRaF%pkFTz- zCJ+dpKYy;Rt*xu8BNB=A_4N%64ULVBBoe8mrKPpCwXLnKy}iA&v$MOqyQin8x3{;i zudlzqe_&vMLZJ)}4pOPqp`jrfjYg-_hlht53FMd2nHe6BH#<8!H#f)U^XKR17Zw&47Z;b7mX?>7S5{U80>SF) z>e|}c`uh6D#>VF6rcfx{+S(F{MBCfjJ3BjKv3PfPcW-ZR-2fMBIvBA&SM#k#iJoyiib{34ADdDcA_31T)KpX{irS?~{BklHj+M+J% zdw&s15$x2E%ww_b0H zGN`Wrd|F^q*B=xc1K~Lt??CkcoFt51b6of2;ey0^_r$9zTQI|#vY7M?ow*q>9SF8- z@0iVrUJnQmYRvoR7rLH3!`KV^4KZPJv+ZSUf{CD-c{CJf7_T?DikxnFe$i3IWg0g2 z8v2|y1wY;cR@ycOa~`B6D4Xd0Y?F=Zy2WL(Sz}{QZDAy5>($Mi!c@bBw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