Lucie Bedouret 2 years ago
commit 41f56ceba2

@ -1,6 +1,7 @@
<h3>My Inventory</h3> <h3>My Inventory</h3>
<div class="inventory-items"> <div class="inventory-items">
@foreach (var item in ItemsInventory) @foreach (var item in ItemsInventory)
{ {
<InventoryItem Index="@index" Item="item" /> <InventoryItem Index="@index" Item="item" />
@ -12,3 +13,4 @@
} }
</div> </div>

@ -1,6 +1,5 @@
.inventory-items { .inventory-items {
grid-template-columns: repeat(6,minmax(0,1fr)); grid-template-columns: repeat(6,minmax(0,1fr));
gap: 10px; gap: 5px;
display: grid; display: grid;
width: 40%;
} }

@ -1,55 +1,45 @@
@page "/inventory" @page "/inventory"
@using myBlazorApp.Components @using myBlazorApp.Components
@using System.Globalization @using System.Globalization
@using myBlazorApp.Models; @using myBlazorApp.Models;
<div class="inventory"> <div class="body">
<MyInventory ItemsInventory="@itemsInv"/> <div class="inventory">
</div> <MyInventory ItemsInventory="@itemsInv" />
</div>
<div id="ItemList">
<div>
<h3>List of Items</h3>
@*SearchBar*@
<p>
<b>CurrentCulture</b>: @CultureInfo.CurrentCulture
</p>
</div>
<div id="ItemList" class="align-end">
<div>
<p>
List of items
</p>
@*SearchBar*@
</div> <div id="ItemList">
<div id="ItemList" class="align-end">
<DataGrid TItem="Item" <div>
Data="@items" <p>
ReadData="@OnReadData" List of items
TotalItems="@totalItem" </p>
PageSize="10" @*SearchBar*@
ShowPager
Responsive> </div>
<DataGridColumn TItem="Item" Field=""/>
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name" /> <DataGrid TItem="Item"
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image"> Data="@items"
<DisplayTemplate> ReadData="@OnReadData"
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) TotalItems="@totalItem"
{ PageSize="10"
<img src="images/@(context.Name).png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px"/> ShowPager
} Responsive>
else <DataGridColumn TItem="Item" Field="" />
{ <DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name" />
<img src="images/default.png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px"/> <DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image">
} <DisplayTemplate>
</DisplayTemplate> @if (!string.IsNullOrWhiteSpace(context.ImageBase64))
</DataGridColumn> {
</DataGrid> <img src="data:image/png;base64, @(context.ImageBase64)" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="min-width: 50px; max-width: 150px" />
</div> }
</div> else
{
<img src="images/default.png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" />
}
</DisplayTemplate>
</DataGridColumn>
</DataGrid>
</div>
</div>
</div>

@ -4,6 +4,7 @@ using Blazored.Modal.Services;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using myBlazorApp.Factories;
using myBlazorApp.Modals; using myBlazorApp.Modals;
using myBlazorApp.Models; using myBlazorApp.Models;
using myBlazorApp.Services; using myBlazorApp.Services;
@ -16,6 +17,16 @@ namespace myBlazorApp.Pages
private List<Item> items = new List<Item>(); private List<Item> items = new List<Item>();
/*[Parameter]
public int Id { get; set; }
*/
private ItemModel itemModel = new()
{
EnchantCategories = new List<string>(),
RepairWith = new List<string>()
};
private int totalItem; private int totalItem;
[Inject] [Inject]
@ -63,7 +74,6 @@ namespace myBlazorApp.Pages
// Reload the page // Reload the page
NavigationManager.NavigateTo("list", true); NavigationManager.NavigateTo("list", true);
} }
}
} }
}

@ -1,21 +1,17 @@
.body { .body {
display: flex; display: flex;
width: 100%;
} }
.inventory-items { .inventory {
grid-template-columns: repeat(6,minmax(0,1fr));
gap: 10px;
display: grid;
width: 40%;
}
.align-start {
align-items: flex-start; align-items: flex-start;
justify-content: start justify-content: start;
width: 45%;
margin-right: 5%;
} }
.align-end { .ItemList {
align-content: flex-end; align-content: flex-end;
justify-content: end; justify-content: end;
width: 400px; width: 45%;
} }

@ -11,7 +11,7 @@
"myBlazorApp": { "myBlazorApp": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"applicationUrl": "https://localhost:7235;http://localhost:5065", "applicationUrl": "https://localhost:7236;http://localhost:5065",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

@ -6,11 +6,12 @@ using static System.Net.WebRequestMethods;
namespace myBlazorApp.Services namespace myBlazorApp.Services
{ {
public class DataApiService : IDataService public class DataApiService : IDataService
{ {
private readonly HttpClient _http; private readonly HttpClient _http;
public DataApiService(HttpClient http) public DataApiService(
HttpClient http)
{ {
_http = http; _http = http;
} }
@ -40,7 +41,7 @@ namespace myBlazorApp.Services
} }
public async Task Update(int id, ItemModel model) public async Task Update(int id, ItemModel model)
{ {
// Get the item // Get the item
var item = ItemFactory.Create(model); var item = ItemFactory.Create(model);

@ -1,136 +1,186 @@
 using System;  using System;
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using myBlazorApp.Components; using myBlazorApp.Components;
using myBlazorApp.Factories; using myBlazorApp.Factories;
using myBlazorApp.Models; using myBlazorApp.Models;
namespace myBlazorApp.Services namespace myBlazorApp.Services
{ {
public class DataLocalService : IDataService public class DataLocalService : IDataService
{ {
private readonly HttpClient _http; private readonly HttpClient _http;
private readonly ILocalStorageService _localStorage; private readonly ILocalStorageService _localStorage;
private readonly NavigationManager _navigationManager; private readonly NavigationManager _navigationManager;
private readonly IWebHostEnvironment _webHostEnvironment; private readonly IWebHostEnvironment _webHostEnvironment;
public DataLocalService(ILocalStorageService localStorage, public DataLocalService(ILocalStorageService localStorage,
HttpClient http, HttpClient http,
IWebHostEnvironment webHostEnvironment, IWebHostEnvironment webHostEnvironment,
NavigationManager navigationManager) NavigationManager navigationManager)
{ {
_localStorage = localStorage; _localStorage = localStorage;
_http = http; _http = http;
_webHostEnvironment = webHostEnvironment; _webHostEnvironment = webHostEnvironment;
_navigationManager = navigationManager; _navigationManager = navigationManager;
} }
public async Task Add(ItemModel model) public async Task Add(ItemModel model)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Item>>("data"); var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
//Simulate the Id //Simulate the Id
model.Id = currentData.Max(s => s.Id) + 1; model.Id = currentData.Max(s => s.Id) + 1;
/// Add the item to the current data /// Add the item to the current data
currentData.Add(ItemFactory.Create(model)); currentData.Add(ItemFactory.Create(model));
// Save the data // Save the image
await _localStorage.SetItemAsync("data", currentData); var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
}
// Check if the folder "images" exist
public async Task<int> Count() if (!imagePathInfo.Exists)
{ {
return (await _localStorage.GetItemAsync<Item[]>("data")).Length; imagePathInfo.Create();
} }
public async Task<List<Item>> List(int currentPage, int pageSize) // Determine the image name
{ var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
// Load data from local storage
var currentData = await _localStorage.GetItemAsync<Item[]>("data"); // Write the file content
// Check if data exist in the local storage await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
if (currentData == null)
{
// this code add in the local storage the fake data // Save the data
var originalData = await _http.GetFromJsonAsync<Item[]>($"{_navigationManager.BaseUri}fake-data.json"); await _localStorage.SetItemAsync("data", currentData);
await _localStorage.SetItemAsync("data", originalData); }
}
public async Task<int> Count()
return (await _localStorage.GetItemAsync<Item[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); {
} return (await _localStorage.GetItemAsync<Item[]>("data")).Length;
}
public async Task<Item> GetById(int id)
{ public async Task<List<Item>> List(int currentPage, int pageSize)
// Get the current data {
var currentData = await _localStorage.GetItemAsync<List<Item>>("data"); // Load data from local storage
var currentData = await _localStorage.GetItemAsync<Item[]>("data");
// Get the item int the list // Check if data exist in the local storage
var item = currentData.FirstOrDefault(w => w.Id == id); if (currentData == null)
{
// Check if item exist // this code add in the local storage the fake data
if (item == null) var originalData = await _http.GetFromJsonAsync<Item[]>($"{_navigationManager.BaseUri}fake-data.json");
{ await _localStorage.SetItemAsync("data", originalData);
throw new Exception($"Unable to found the item with ID: {id}"); }
}
return (await _localStorage.GetItemAsync<Item[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
return item; }
}
public async Task<Item> GetById(int id)
public async Task Update(int id, ItemModel model) {
{ // Get the current data
// Get the current data var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
// Get the item int the list
// Get the item int the list var item = currentData.FirstOrDefault(w => w.Id == id);
var item = currentData.FirstOrDefault(w => w.Id == id);
// Check if item exist
// Check if item exist if (item == null)
if (item == null) {
{ throw new Exception($"Unable to found the item with ID: {id}");
throw new Exception($"Unable to found the item with ID: {id}"); }
}
return item;
// Modify the content of the item }
ItemFactory.Update(item, model);
public async Task Update(int id, ItemModel model)
// Save the data {
await _localStorage.SetItemAsync("data", currentData); // Get the current data
} var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
public async Task Delete(int id) // Get the item int the list
{ var item = currentData.FirstOrDefault(w => w.Id == id);
// Get the current data
var currentData = await _localStorage.GetItemAsync<List<Item>>("data"); // Check if item exist
if (item == null)
// Get the item int the list {
var item = currentData.FirstOrDefault(w => w.Id == id); throw new Exception($"Unable to found the item with ID: {id}");
}
// Delete item in
currentData.Remove(item); // Save the image
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
// Save the data // Check if the folder "images" exist
await _localStorage.SetItemAsync("data", currentData); if (!imagePathInfo.Exists)
} {
imagePathInfo.Create();
public Task<List<CraftingRecipe>> GetRecipes() }
{
var items = new List<CraftingRecipe> // Delete the previous image
{ if (item.Name != model.Name)
new CraftingRecipe {
{ var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
Give = new Item { DisplayName = "Diamond", Name = "diamond" },
Have = new List<List<string>> if (oldFileName.Exists)
{ {
new List<string> { "dirt", "dirt", "dirt" }, File.Delete(oldFileName.FullName);
new List<string> { "dirt", null, "dirt" }, }
new List<string> { "dirt", "dirt", "dirt" } }
}
} // Determine the image name
}; var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
return Task.FromResult(items); // 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<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 Task<List<CraftingRecipe>> GetRecipes()
{
var items = new List<CraftingRecipe>
{
new CraftingRecipe
{
Give = new Item { DisplayName = "Diamond", Name = "diamond" },
Have = new List<List<string>>
{
new List<string> { "dirt", "dirt", "dirt" },
new List<string> { "dirt", null, "dirt" },
new List<string> { "dirt", "dirt", "dirt" }
}
}
};
return Task.FromResult(items);
}
}
}

Loading…
Cancel
Save