diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor b/myBlazorApp/myBlazorApp/Components/MyInventory.razor index 2c10786..6f4ccf3 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor @@ -1,6 +1,7 @@ 

My Inventory

+ @foreach (var item in ItemsInventory) { @@ -12,3 +13,4 @@ }
+ diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.css b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.css index 34491e9..2913fd8 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.css +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.css @@ -1,6 +1,5 @@ .inventory-items { grid-template-columns: repeat(6,minmax(0,1fr)); - gap: 10px; + gap: 5px; display: grid; - width: 40%; } diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor b/myBlazorApp/myBlazorApp/Pages/Inventory.razor index 38a76f0..aa0bf41 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor @@ -1,55 +1,45 @@ -@page "/inventory" -@using myBlazorApp.Components -@using System.Globalization +@page "/inventory" +@using myBlazorApp.Components +@using System.Globalization @using myBlazorApp.Models; -
- -
- -
-
-

List of Items

- @*SearchBar*@ - - -

- CurrentCulture: @CultureInfo.CurrentCulture -

- - -
- -
-
-

- List of items -

- @*SearchBar*@ +
+
+ +
-
- - - - - - - @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) - { - @context.DisplayName - } - else - { - @context.DisplayName - } - - - -
-
+
+
+
+

+ List of items +

+ @*SearchBar*@ + +
+ + + + + + + @if (!string.IsNullOrWhiteSpace(context.ImageBase64)) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + + +
+
+
diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs index 1ab3ee8..9fcb34a 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs @@ -4,6 +4,7 @@ using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; +using myBlazorApp.Factories; using myBlazorApp.Modals; using myBlazorApp.Models; using myBlazorApp.Services; @@ -16,6 +17,16 @@ namespace myBlazorApp.Pages private List items = new List(); + /*[Parameter] + public int Id { get; set; } + */ + + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + private int totalItem; [Inject] @@ -63,7 +74,6 @@ namespace myBlazorApp.Pages // Reload the page NavigationManager.NavigateTo("list", true); } - - } -} - + } +} + diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.css b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.css index c3bcf2e..f97b46f 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.css +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.css @@ -1,21 +1,17 @@ .body { display: flex; + width: 100%; } -.inventory-items { - grid-template-columns: repeat(6,minmax(0,1fr)); - gap: 10px; - display: grid; - width: 40%; -} - -.align-start { +.inventory { align-items: flex-start; - justify-content: start + justify-content: start; + width: 45%; + margin-right: 5%; } -.align-end { +.ItemList { align-content: flex-end; justify-content: end; - width: 400px; + width: 45%; } \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Properties/launchSettings.json b/myBlazorApp/myBlazorApp/Properties/launchSettings.json index c4d4e19..2ea012a 100644 --- a/myBlazorApp/myBlazorApp/Properties/launchSettings.json +++ b/myBlazorApp/myBlazorApp/Properties/launchSettings.json @@ -11,7 +11,7 @@ "myBlazorApp": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:7235;http://localhost:5065", + "applicationUrl": "https://localhost:7236;http://localhost:5065", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/myBlazorApp/myBlazorApp/Services/DataApiService.cs b/myBlazorApp/myBlazorApp/Services/DataApiService.cs index 7027759..ca17b30 100644 --- a/myBlazorApp/myBlazorApp/Services/DataApiService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataApiService.cs @@ -6,11 +6,12 @@ using static System.Net.WebRequestMethods; namespace myBlazorApp.Services { - public class DataApiService : IDataService - { + public class DataApiService : IDataService + { private readonly HttpClient _http; - public DataApiService(HttpClient http) + public DataApiService( + HttpClient http) { _http = http; } @@ -40,7 +41,7 @@ namespace myBlazorApp.Services } public async Task Update(int id, ItemModel model) - { + { // Get the item var item = ItemFactory.Create(model); diff --git a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs index 835852a..2bbe9d9 100644 --- a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs @@ -1,136 +1,186 @@ - using System; -using Blazored.LocalStorage; -using Microsoft.AspNetCore.Components; -using myBlazorApp.Components; -using myBlazorApp.Factories; -using myBlazorApp.Models; - -namespace myBlazorApp.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 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); - - - // 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); - } - } -} + using System; +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using myBlazorApp.Components; +using myBlazorApp.Factories; +using myBlazorApp.Models; + +namespace myBlazorApp.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 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 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); + } + } +}