using System; using BlazorT.Composants; using BlazorT.Models; using Microsoft.Extensions.Logging; namespace BlazorT.Services; public class InventoryDataService : IInventoryDataService { private readonly HttpClient _http; private ILogger _logger; /// /// Initializes a new instance of the class. /// /// The HttpClient used for HTTP requests. /// The ILogger used for logging. public InventoryDataService(HttpClient http, ILogger logger) { _http = http; _logger = logger; } /// /// Adds a new item to the inventory. /// /// The item model to add. public async Task Add(ItemModel model) { // Get the item var item = ItemFactory.Create(model); _logger.LogInformation($"Creating Element with id....... <{item.Name}>"); // Save the data await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item); } /// /// Retrieves the number of items in the inventory. /// /// The number of items in the inventory. public async Task Count() { return await _http.GetFromJsonAsync("https://localhost:7234/api/Crafting/count"); } /// /// Retrieves a list of items in the inventory. /// /// The current page number. /// The number of items per page. /// A list of items in the inventory. public async Task> List(int currentPage, int pageSize) { _logger.LogInformation($".......List fetching........ <{currentPage}>;<{pageSize}>"); return await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}"); } /// /// Retrieves an item in the inventory by its ID. /// /// The ID of the item to retrieve. /// The item with the specified ID. public async Task GetById(int id) { _logger.LogInformation($"Element with id....... <{id}>"); return await _http.GetFromJsonAsync($"https://localhost:7234/api/Crafting/{id}"); } /// /// Updates an item in the inventory. /// /// The ID of the item to update. /// The updated item model. public async Task Update(int id, ItemModel model) { // Get the item var item = ItemFactory.Create(model); _logger.LogInformation($"Update ---- Element with id....... <{id}>"); await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item); } /// /// Deletes an item from the inventory. /// /// The ID of the item to delete. public async Task Delete(int id) { _logger.LogInformation($"Deleting Element with id....... <{id}>"); await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}"); } /// /// public async Task> GetRecipes() { return await _http.GetFromJsonAsync>("https://localhost:7234/api/Crafting/recipe"); } }