From 31f2e5a9a6f51cb222a81d1734672e80dc3efba9 Mon Sep 17 00:00:00 2001 From: remrem Date: Fri, 9 Dec 2022 10:03:47 +0100 Subject: [PATCH] add await get all list item --- Blazor/Blazor/Components/InventoryList.razor | 17 +- .../Blazor/Components/InventoryList.razor.cs | 16 +- Blazor/Blazor/Pages/Inventory.razor | 11 +- Blazor/Blazor/Pages/Inventory.razor.css | 14 ++ Blazor/Blazor/Pages/_Layout.cshtml | 1 + Blazor/Blazor/Services/DataApiService.cs | 5 + Blazor/Blazor/Services/DataLocalService.cs | 145 ------------------ Blazor/Blazor/Services/IDataService.cs | 2 + 8 files changed, 38 insertions(+), 173 deletions(-) create mode 100644 Blazor/Blazor/Pages/Inventory.razor.css delete mode 100644 Blazor/Blazor/Services/DataLocalService.cs diff --git a/Blazor/Blazor/Components/InventoryList.razor b/Blazor/Blazor/Components/InventoryList.razor index 3b9affa..0b51280 100644 --- a/Blazor/Blazor/Components/InventoryList.razor +++ b/Blazor/Blazor/Components/InventoryList.razor @@ -3,7 +3,6 @@ @if (!string.IsNullOrWhiteSpace(context.ImageBase64)) { - @context.DisplayName + @context.DisplayName } else { - @context.DisplayName + @context.DisplayName } - - - -@code { -} diff --git a/Blazor/Blazor/Components/InventoryList.razor.cs b/Blazor/Blazor/Components/InventoryList.razor.cs index 1929509..f893b86 100644 --- a/Blazor/Blazor/Components/InventoryList.razor.cs +++ b/Blazor/Blazor/Components/InventoryList.razor.cs @@ -29,18 +29,12 @@ namespace Blazor.Components [Inject] public IStringLocalizer Localizer { get; set; } - private async Task OnReadData(DataGridReadDataEventArgs e) + protected override async Task OnInitializedAsync() { - if (e.CancellationToken.IsCancellationRequested) - { - return; - } - - if (!e.CancellationToken.IsCancellationRequested) - { - items = await DataService.List(e.Page, e.PageSize); - totalItem = await DataService.Count(); - } + + items = await DataService.getAll(); + totalItem = await DataService.Count(); + await base.OnInitializedAsync(); } } } \ No newline at end of file diff --git a/Blazor/Blazor/Pages/Inventory.razor b/Blazor/Blazor/Pages/Inventory.razor index 9391990..b4f1aa5 100644 --- a/Blazor/Blazor/Pages/Inventory.razor +++ b/Blazor/Blazor/Pages/Inventory.razor @@ -6,6 +6,13 @@

Inventory

-
- +
+
+ Ici c'est l'inventaire +
+ + +
+ +
diff --git a/Blazor/Blazor/Pages/Inventory.razor.css b/Blazor/Blazor/Pages/Inventory.razor.css new file mode 100644 index 0000000..f778a72 --- /dev/null +++ b/Blazor/Blazor/Pages/Inventory.razor.css @@ -0,0 +1,14 @@ +.inventory-list { + width: 40%; +} + +.inventory { + width: 40%; +} + +#master { + display: flex; + flex-direction: row; + justify-content: space-between; + width: 100%; +} diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml index 1fdfc6e..8b87a04 100644 --- a/Blazor/Blazor/Pages/_Layout.cshtml +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -8,6 +8,7 @@ @**@ + diff --git a/Blazor/Blazor/Services/DataApiService.cs b/Blazor/Blazor/Services/DataApiService.cs index 8694471..a569ed7 100644 --- a/Blazor/Blazor/Services/DataApiService.cs +++ b/Blazor/Blazor/Services/DataApiService.cs @@ -32,6 +32,11 @@ namespace Blazor.Services return await _http.GetFromJsonAsync>($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}"); } + public async Task> getAll() + { + return await _http.GetFromJsonAsync>($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/?currentPage=0&pageSize=100000"); + } + public async Task GetById(int id) { return await _http.GetFromJsonAsync($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/{id}"); diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs deleted file mode 100644 index c92584c..0000000 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ /dev/null @@ -1,145 +0,0 @@ -using Blazor.Components; -using Blazor.Factories; -using Blazor.Models; -using Blazored.LocalStorage; -using Microsoft.AspNetCore.Components; - -namespace Blazor.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; - } - - 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 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}"); - } - - // 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); - } - } -} diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs index cf8c5db..307c90f 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/Services/IDataService.cs @@ -11,6 +11,8 @@ namespace Blazor.Services Task> List(int currentPage, int pageSize); + Task> getAll(); + Task GetById(int id); Task Update(int id, ItemModel model);