Add service inventory

master
Louis DUFOUR 2 years ago
parent cd1b27d120
commit 0875e1118a

@ -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; }
}
}

@ -15,6 +15,7 @@ namespace ValblazeProject.Services
_http = http; _http = http;
} }
/************ Crafting ************/
public async Task Add(ItemModel model) public async Task Add(ItemModel model)
{ {
// Get the item // Get the item
@ -61,5 +62,29 @@ namespace ValblazeProject.Services
{ {
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe"); return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe");
} }
/************ Inventory ************
public async Task supprInventory()
{
await _http.DeleteAsync("https://localhost:7234/api/Inventory");
}
public async Task<Inventory> getInventory()
{
return await _http.GetFromJsonAsync<Inventory>("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");
}*/
} }
} }

@ -1,5 +1,6 @@
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;
using ValblazeProject.Components; using ValblazeProject.Components;
using ValblazeProject.Factories; using ValblazeProject.Factories;
using ValblazeProject.Models; using ValblazeProject.Models;
@ -25,6 +26,7 @@ namespace ValblazeProject.Services
_navigationManager = navigationManager; _navigationManager = navigationManager;
} }
/************************ Crafting ************************/
public async Task Add(ItemModel model) public async Task Add(ItemModel model)
{ {
// Get the current data // Get the current data
@ -198,5 +200,124 @@ namespace ValblazeProject.Services
return (await _localStorage.GetItemAsync<Item[]>("data")).ToList(); return (await _localStorage.GetItemAsync<Item[]>("data")).ToList();
} }
/************************ Inventory ************************
public async Task supprInventory()
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Item>>("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<Inventory> getInventory()
{
// Get the current data
var currentData = await _localStorage.GetItemAsync<Inventory>("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<List<Item>>("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<List<Item>>("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);
}*/
} }
} }

@ -5,6 +5,7 @@ namespace ValblazeProject.Services
{ {
public interface IDataService public interface IDataService
{ {
// Crafting
Task Add(ItemModel model); Task Add(ItemModel model);
Task<int> Count(); Task<int> Count();
Task<List<Item>> List(int currentPage, int pageSize); Task<List<Item>> List(int currentPage, int pageSize);
@ -13,5 +14,11 @@ namespace ValblazeProject.Services
Task Update(int id, ItemModel model); Task Update(int id, ItemModel model);
Task Delete(int id); Task Delete(int id);
Task<List<CraftingRecipe>> GetRecipes(); Task<List<CraftingRecipe>> GetRecipes();
/* Inventory
Task putInventory();
Task<Inventory> getInventory();
Task postInventory();
Task supprInventory();*/
} }
} }

Loading…
Cancel
Save