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 0000000..a7446c9 Binary files /dev/null and b/BlazorTuto/wwwroot/images/index.png differ