using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; using System.Net.Http.Json; using ValblazeProject.Components; using ValblazeProject.Factories; using ValblazeProject.Models; namespace ValblazeProject.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; } /************************ Crafting ************************/ 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); } public async Task Delete(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); // Delete item in currentData.Remove(item); // Delete the image var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); var fileName = new FileInfo($"{imagePathInfo}/{item.Name}.png"); if (fileName.Exists) { File.Delete(fileName.FullName); } // Save the data await _localStorage.SetItemAsync("data", currentData); } public Task> GetRecipes() { var items = new List { new CraftingRecipe { Give = new Item { DisplayName = "Diamond", Name = "diamond" }, Have = new List> { new List { "dirt", "dirt", "dirt" }, new List { "dirt", null, "dirt" }, new List { "dirt", "dirt", "dirt" } } } }; return Task.FromResult(items); } public async Task> List() { // 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")).ToList(); } /************************ Inventory ************************/ public async Task SupprInventory(Inventory itemSearch) { // Get the current data var currentData = await _localStorage.GetItemAsync>("KeyInvent"); // Get the item int the list var item = currentData.FirstOrDefault(w => w.position == itemSearch.position); // Delete item in currentData.Remove(item); // Save the data await _localStorage.SetItemAsync("data", currentData); } public async Task> GetInventory() { // Get the current data var currentData = await _localStorage.GetItemAsync>("KeyInvent"); return currentData; } public async Task PutInventory(Inventory invent) { // Get the current data var currentData = await _localStorage.GetItemAsync>("KeyInvent"); // Get the item int the list var item = currentData.FirstOrDefault(w => w.position == invent.position); // Check if item exist if (item == null) { throw new Exception($"Unable to found the item with ID: {item.position}"); } item.position = invent.position; item.numberItem = invent.numberItem; item.itemName = invent.itemName; // Save the data await _localStorage.SetItemAsync("KeyInvent", currentData); } public async Task PostInventory(Inventory invent) { // Get the current data var currentData = await _localStorage.GetItemAsync>("KeyInvent"); // Add the item to the current data currentData.Add(invent); // Save the data await _localStorage.SetItemAsync("KeyInvent", currentData); } } }