using Blazored.LocalStorage; using BlazorTp1.Models; using Microsoft.AspNetCore.Components; 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(); } }