diff --git a/ValblazeProject/Models/Inventory.cs b/ValblazeProject/Models/Inventory.cs new file mode 100644 index 0000000..8b7371f --- /dev/null +++ b/ValblazeProject/Models/Inventory.cs @@ -0,0 +1,9 @@ +namespace ValblazeProject.Models +{ + public class Inventory + { + public string itemName { get; set; } + public int numberItem { get; set; } + public int position { get; set; } + } +} diff --git a/ValblazeProject/Pages/Services/DataApiService.cs b/ValblazeProject/Pages/Services/DataApiService.cs index c8cde04..d3ee4a6 100644 --- a/ValblazeProject/Pages/Services/DataApiService.cs +++ b/ValblazeProject/Pages/Services/DataApiService.cs @@ -15,6 +15,7 @@ namespace ValblazeProject.Services _http = http; } + /************ Crafting ************/ public async Task Add(ItemModel model) { // Get the item @@ -61,5 +62,29 @@ namespace ValblazeProject.Services { return await _http.GetFromJsonAsync>("https://localhost:7234/api/Crafting/recipe"); } + + /************ Inventory ************ + public async Task supprInventory() + { + await _http.DeleteAsync("https://localhost:7234/api/Inventory"); + } + + public async Task getInventory() + { + return await _http.GetFromJsonAsync("https://localhost:7234/api/Inventory"); + } + + public async Task putInventory() + { + await _http.PutAsJsonAsync($"https://localhost:7234/api/Inventory"); + } + public async Task postInventory() + { + // Get the item + //var item = ItemFactory.Create(model); + + // Save the data + await _http.PostAsJsonAsync("https://localhost:7234/api/Inventory"); + }*/ } } diff --git a/ValblazeProject/Pages/Services/DataLocalService.cs b/ValblazeProject/Pages/Services/DataLocalService.cs index 94a1880..0788b6b 100644 --- a/ValblazeProject/Pages/Services/DataLocalService.cs +++ b/ValblazeProject/Pages/Services/DataLocalService.cs @@ -1,5 +1,6 @@ using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; +using System.Net.Http.Json; using ValblazeProject.Components; using ValblazeProject.Factories; using ValblazeProject.Models; @@ -25,6 +26,7 @@ namespace ValblazeProject.Services _navigationManager = navigationManager; } + /************************ Crafting ************************/ public async Task Add(ItemModel model) { // Get the current data @@ -198,5 +200,124 @@ namespace ValblazeProject.Services return (await _localStorage.GetItemAsync("data")).ToList(); } + + /************************ Inventory ************************ + public async Task supprInventory() + { + // 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 async Task getInventory() + { + // 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 putInventory() + { + // 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 postInventory() + { + // 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); + }*/ } } diff --git a/ValblazeProject/Pages/Services/IDataService.cs b/ValblazeProject/Pages/Services/IDataService.cs index 37b5321..02f79ba 100644 --- a/ValblazeProject/Pages/Services/IDataService.cs +++ b/ValblazeProject/Pages/Services/IDataService.cs @@ -5,6 +5,7 @@ namespace ValblazeProject.Services { public interface IDataService { + // Crafting Task Add(ItemModel model); Task Count(); Task> List(int currentPage, int pageSize); @@ -13,5 +14,11 @@ namespace ValblazeProject.Services Task Update(int id, ItemModel model); Task Delete(int id); Task> GetRecipes(); + + /* Inventory + Task putInventory(); + Task getInventory(); + Task postInventory(); + Task supprInventory();*/ } }