diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Factories/ItemFactory.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Factories/ItemFactory.cs new file mode 100644 index 0000000..e6e1ca9 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using CetteAppliVaMarcher.Models; + +namespace CetteAppliVaMarcher.Factories +{ + public static class ItemFactory + { + public static ItemModel ToModel(Item item, byte[] imageContent) + { + return new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = imageContent + }; + } + + public static Item Create(ItemModel model) + { + return 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 + }; + } + + public static void Update(Item item, ItemModel model) + { + 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; + } + } +} diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Add.razor.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Add.razor.cs index 04539e1..1302a04 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Add.razor.cs +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Add.razor.cs @@ -1,5 +1,6 @@ using Blazored.LocalStorage; using CetteAppliVaMarcher.Models; +using CetteAppliVaMarcher.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; @@ -7,25 +8,11 @@ namespace CetteAppliVaMarcher.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 /// @@ -35,45 +22,21 @@ namespace CetteAppliVaMarcher.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"); } diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Edit.razor b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Edit.razor new file mode 100644 index 0000000..57cedae --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Edit.razor @@ -0,0 +1,82 @@ +@page "/edit/{Id:int}" + +

Edit

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Edit.razor.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Edit.razor.cs new file mode 100644 index 0000000..586d603 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/Edit.razor.cs @@ -0,0 +1,110 @@ +using CetteAppliVaMarcher.Factories; +using CetteAppliVaMarcher.Models; +using CetteAppliVaMarcher.Services; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; + +namespace CetteAppliVaMarcher.Pages +{ + public partial class Edit + { + [Parameter] + public int Id { get; set; } + + /// + /// The default enchant categories. + /// + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + + /// + /// The current item model + /// + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + /// + /// 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" }; + + [Inject] + public IDataService DataService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + protected override async Task OnInitializedAsync() + { + var item = await DataService.GetById(Id); + + var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png"); + + if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png")) + { + fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png"); + } + + // Set the model with the item + itemModel = ItemFactory.ToModel(item,fileContent); + } + + private async void HandleValidSubmit() + { + await DataService.Update(Id, itemModel); + + 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/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor index cf3628f..42d4f39 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor @@ -43,4 +43,10 @@ + + + + Editer + + \ No newline at end of file diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor.cs index aae9118..2c12831 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor.cs +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Pages/List.razor.cs @@ -1,6 +1,7 @@ using Blazored.LocalStorage; using Blazorise.DataGrid; using CetteAppliVaMarcher.Models; +using CetteAppliVaMarcher.Services; using Microsoft.AspNetCore.Components; namespace CetteAppliVaMarcher.Pages @@ -12,36 +13,11 @@ namespace CetteAppliVaMarcher.Pages private int totalItem; [Inject] - public HttpClient Http { get; set; } - - [Inject] - public ILocalStorageService LocalStorage { get; set; } + public IDataService DataService { get; set; } [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } - [Inject] - public NavigationManager NavigationManager { get; set; } - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - // Do not treat this action if is not the first render - if (!firstRender) - { - return; - } - - 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 (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) @@ -49,14 +25,10 @@ namespace CetteAppliVaMarcher.Pages 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 + items = await DataService.List(e.Page, e.PageSize); + totalItem = await DataService.Count(); } } } diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Program.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Program.cs index 21672d3..72af469 100644 --- a/CetteAppliVaMarcher/CetteAppliVaMarcher/Program.cs +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Program.cs @@ -5,6 +5,8 @@ using CetteAppliVaMarcher.Data; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using CetteAppliVaMarcher.Services; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -17,7 +19,7 @@ builder.Services .AddBlazorise() .AddBootstrapProviders() .AddFontAwesomeIcons(); - +builder.Services.AddScoped(); builder.Services.AddBlazoredLocalStorage(); var app = builder.Build(); diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs new file mode 100644 index 0000000..955ed3c --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/DataLocalService.cs @@ -0,0 +1,140 @@ +using Blazored.LocalStorage; +using CetteAppliVaMarcher.Factories; +using CetteAppliVaMarcher.Models; +using Microsoft.AspNetCore.Components; + +namespace CetteAppliVaMarcher.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(ItemFactory.Create(model)); + + // 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 + ItemFactory.Update(item, model); + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + } +} diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs new file mode 100644 index 0000000..9389095 --- /dev/null +++ b/CetteAppliVaMarcher/CetteAppliVaMarcher/Services/IDataServices.cs @@ -0,0 +1,13 @@ +using CetteAppliVaMarcher.Models; + +namespace CetteAppliVaMarcher.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); + } +} \ No newline at end of file diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/wwwroot/images/cactus.png b/CetteAppliVaMarcher/CetteAppliVaMarcher/wwwroot/images/cactus.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/CetteAppliVaMarcher/CetteAppliVaMarcher/wwwroot/images/cactus.png differ diff --git a/CetteAppliVaMarcher/CetteAppliVaMarcher/wwwroot/images/matt.png b/CetteAppliVaMarcher/CetteAppliVaMarcher/wwwroot/images/matt.png new file mode 100644 index 0000000..d31e6a2 Binary files /dev/null and b/CetteAppliVaMarcher/CetteAppliVaMarcher/wwwroot/images/matt.png differ