weird error

master
Lilian BRETON 2 years ago
parent f0d89e4874
commit 8e96c6addd

@ -19,5 +19,4 @@
<InventoryItem Index="15" /> <InventoryItem Index="15" />
<InventoryItem Index="16" /> <InventoryItem Index="16" />
<InventoryItem Index="17" /> <InventoryItem Index="17" />
</div> </div>

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

@ -1,75 +1,75 @@
using System; using System;
using Blazored.Modal; using Blazored.Modal;
using Blazored.Modal.Services; 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.Factories;
using myBlazorApp.Modals; using myBlazorApp.Modals;
using myBlazorApp.Models; using myBlazorApp.Models;
using myBlazorApp.Services; using myBlazorApp.Services;
namespace myBlazorApp.Pages namespace myBlazorApp.Pages
{ {
public partial class Inventory public partial class Inventory
{ {
[Parameter] [Parameter]
public int Id { get; set; } public int Id { get; set; }
private ItemModel itemModel = new() private ItemModel itemModel = new()
{ {
EnchantCategories = new List<string>(), EnchantCategories = new List<string>(),
RepairWith = new List<string>() RepairWith = new List<string>()
}; };
private List<Item> items = new List<Item>(); private List<Item> items = new List<Item>();
private int totalItem; private int totalItem;
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
[Inject] [Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; } public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[CascadingParameter] [CascadingParameter]
public IModalService Modal { get; set; } public IModalService Modal { get; set; }
private async Task OnReadData(DataGridReadDataEventArgs<Item> e) private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
{ {
if (e.CancellationToken.IsCancellationRequested) if (e.CancellationToken.IsCancellationRequested)
{ {
return; return;
} }
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
items = await DataService.List(e.Page, e.PageSize); items = await DataService.List(e.Page, e.PageSize);
totalItem = await DataService.Count(); totalItem = await DataService.Count();
} }
} }
private async void OnDelete(int id) private async void OnDelete(int id)
{ {
var parameters = new ModalParameters(); var parameters = new ModalParameters();
parameters.Add(nameof(Item.Id), id); parameters.Add(nameof(Item.Id), id);
var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters); var modal = Modal.Show<DeleteConfirmation>("Delete Confirmation", parameters);
var result = await modal.Result; var result = await modal.Result;
if (result.Cancelled) if (result.Cancelled)
{ {
return; return;
} }
await DataService.Delete(id); await DataService.Delete(id);
// Reload the page // Reload the page
NavigationManager.NavigateTo("list", true); NavigationManager.NavigateTo("list", true);
} }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
@ -86,7 +86,7 @@ namespace myBlazorApp.Pages
// Set the model with the item // Set the model with the item
itemModel = ItemFactory.ToModel(item, fileContent); itemModel = ItemFactory.ToModel(item, fileContent);
} }
} }
} }

@ -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;
} }
@ -21,40 +22,40 @@ namespace myBlazorApp.Services
var item = ItemFactory.Create(model); var item = ItemFactory.Create(model);
// Save the data // Save the data
await _http.PostAsJsonAsync("https://localhost:8635/api/Crafting/", item); await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item);
} }
public async Task<int> Count() public async Task<int> Count()
{ {
return await _http.GetFromJsonAsync<int>("https://localhost:8635/api/Crafting/count"); return await _http.GetFromJsonAsync<int>("https://localhost:7234/api/Crafting/count");
} }
public async Task<List<Item>> List(int currentPage, int pageSize) public async Task<List<Item>> List(int currentPage, int pageSize)
{ {
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:8635/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}"); return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
} }
public async Task<Item> GetById(int id) public async Task<Item> GetById(int id)
{ {
return await _http.GetFromJsonAsync<Item>($"https://localhost:8635/api/Crafting/{id}"); return await _http.GetFromJsonAsync<Item>($"https://localhost:7234/api/Crafting/{id}");
} }
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);
await _http.PutAsJsonAsync($"https://localhost:8635/api/Crafting/{id}", item); await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item);
} }
public async Task Delete(int id) public async Task Delete(int id)
{ {
await _http.DeleteAsync($"https://localhost:8635/api/Crafting/{id}"); await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}");
} }
public async Task<List<CraftingRecipe>> GetRecipes() public async Task<List<CraftingRecipe>> GetRecipes()
{ {
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:8635/api/Crafting/recipe"); return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe");
} }
} }
} }

@ -1,39 +1,39 @@
 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 image // Save the image
@ -49,62 +49,62 @@ namespace myBlazorApp.Services
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
// Write the file content // Write the file content
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task<int> Count() public async Task<int> Count()
{ {
return (await _localStorage.GetItemAsync<Item[]>("data")).Length; return (await _localStorage.GetItemAsync<Item[]>("data")).Length;
} }
public async Task<List<Item>> List(int currentPage, int pageSize) public async Task<List<Item>> List(int currentPage, int pageSize)
{ {
// Load data from local storage // Load data from local storage
var currentData = await _localStorage.GetItemAsync<Item[]>("data"); var currentData = await _localStorage.GetItemAsync<Item[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Item[]>($"{_navigationManager.BaseUri}fake-data.json"); var originalData = await _http.GetFromJsonAsync<Item[]>($"{_navigationManager.BaseUri}fake-data.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Item[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); return (await _localStorage.GetItemAsync<Item[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
} }
public async Task<Item> GetById(int id) public async Task<Item> GetById(int id)
{ {
// 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; return item;
} }
public async Task Update(int id, ItemModel model) 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}");
} }
// Save the image // Save the image
@ -131,24 +131,24 @@ namespace myBlazorApp.Services
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
// Write the file content // Write the file content
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
// Modify the content of the item // Modify the content of the item
ItemFactory.Update(item, model); ItemFactory.Update(item, model);
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task Delete(int id) public async Task Delete(int id)
{ {
// 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);
// Delete item in // Delete item in
currentData.Remove(item); currentData.Remove(item);
// Delete the image // Delete the image
@ -158,29 +158,29 @@ namespace myBlazorApp.Services
if (fileName.Exists) if (fileName.Exists)
{ {
File.Delete(fileName.FullName); File.Delete(fileName.FullName);
} }
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public Task<List<CraftingRecipe>> GetRecipes() public Task<List<CraftingRecipe>> GetRecipes()
{ {
var items = new List<CraftingRecipe> var items = new List<CraftingRecipe>
{ {
new CraftingRecipe new CraftingRecipe
{ {
Give = new Item { DisplayName = "Diamond", Name = "diamond" }, Give = new Item { DisplayName = "Diamond", Name = "diamond" },
Have = new List<List<string>> Have = new List<List<string>>
{ {
new List<string> { "dirt", "dirt", "dirt" }, new List<string> { "dirt", "dirt", "dirt" },
new List<string> { "dirt", null, "dirt" }, new List<string> { "dirt", null, "dirt" },
new List<string> { "dirt", "dirt", "dirt" } new List<string> { "dirt", "dirt", "dirt" }
} }
} }
}; };
return Task.FromResult(items); return Task.FromResult(items);
} }
} }
} }

Loading…
Cancel
Save