diff --git a/Sources/MyFirstBlazor/Factories/ItemFactory.cs b/Sources/MyFirstBlazor/Factories/ItemFactory.cs index abfaadf..590f578 100644 --- a/Sources/MyFirstBlazor/Factories/ItemFactory.cs +++ b/Sources/MyFirstBlazor/Factories/ItemFactory.cs @@ -15,7 +15,8 @@ namespace MyFirstBlazor.Factories EnchantCategories = item.EnchantCategories, MaxDurability = item.MaxDurability, StackSize = item.StackSize, - ImageContent = imageContent + ImageContent = imageContent, + ImageBase64 = string.IsNullOrWhiteSpace(item.ImageBase64) ? Convert.ToBase64String(imageContent) : item.ImageBase64 }; } @@ -30,7 +31,8 @@ namespace MyFirstBlazor.Factories EnchantCategories = model.EnchantCategories, MaxDurability = model.MaxDurability, StackSize = model.StackSize, - CreatedDate = DateTime.Now + CreatedDate = DateTime.Now, + ImageBase64 = Convert.ToBase64String(model.ImageContent) }; } @@ -43,6 +45,7 @@ namespace MyFirstBlazor.Factories item.MaxDurability = model.MaxDurability; item.StackSize = model.StackSize; item.UpdatedDate = DateTime.Now; + item.ImageBase64 = Convert.ToBase64String(model.ImageContent); } } } diff --git a/Sources/MyFirstBlazor/Pages/Edit.razor b/Sources/MyFirstBlazor/Pages/Edit.razor index 5544f1f..f85132e 100644 --- a/Sources/MyFirstBlazor/Pages/Edit.razor +++ b/Sources/MyFirstBlazor/Pages/Edit.razor @@ -55,14 +55,7 @@
diff --git a/Sources/MyFirstBlazor/Pages/Edit.razor.cs b/Sources/MyFirstBlazor/Pages/Edit.razor.cs
index 6ac52b2..bf54c9d 100644
--- a/Sources/MyFirstBlazor/Pages/Edit.razor.cs
+++ b/Sources/MyFirstBlazor/Pages/Edit.razor.cs
@@ -45,11 +45,6 @@ namespace MyFirstBlazor.Pages
var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png");
- if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png"))
- {
- fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png");
- }
-
// Set the model with the item
itemModel = ItemFactory.ToModel(item, fileContent);
}
diff --git a/Sources/MyFirstBlazor/Pages/List.razor b/Sources/MyFirstBlazor/Pages/List.razor
index a20c808..10cab50 100644
--- a/Sources/MyFirstBlazor/Pages/List.razor
+++ b/Sources/MyFirstBlazor/Pages/List.razor
@@ -20,9 +20,9 @@
+
}
else
{
diff --git a/Sources/MyFirstBlazor/Program.cs b/Sources/MyFirstBlazor/Program.cs
index 4083799..ee3c03e 100644
--- a/Sources/MyFirstBlazor/Program.cs
+++ b/Sources/MyFirstBlazor/Program.cs
@@ -10,6 +10,7 @@ using Blazored.Modal;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using Microsoft.Extensions.Options;
+using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args);
@@ -26,6 +27,9 @@ builder.Services
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddScoped
> List(int currentPage, int pageSize)
+ {
+ return await _http.GetFromJsonAsync
>($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
+ }
+
+ public async Task
> GetRecipes()
+ {
+ return await _http.GetFromJsonAsync
>("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/recipe");
+ }
+ }
+}
diff --git a/Sources/MyFirstBlazor/Services/DataLocalService.cs b/Sources/MyFirstBlazor/Services/DataLocalService.cs
index 0f27546..0dbecdb 100644
--- a/Sources/MyFirstBlazor/Services/DataLocalService.cs
+++ b/Sources/MyFirstBlazor/Services/DataLocalService.cs
@@ -27,24 +27,21 @@ namespace MyFirstBlazor.Services
public async Task Add(ItemModel model)
{
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync
>("data");
- // Save the image
- var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
-
- // Check if the folder "images" exist
- if (!imagePathInfo.Exists)
- {
- imagePathInfo.Create();
- }
+ // Simulate the Id
+ model.Id = currentData.Max(s => s.Id) + 1;
- // Determine the image name
- var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
+ // Add the item to the current data
+ currentData.Add(ItemFactory.Create(model));
- // Write the file content
- await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
}
+
public async Task
>("data");
- // Save the image
- var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
+ // Get the item int the list
+ var item = currentData.FirstOrDefault(w => w.Id == id);
- // Check if the folder "images" exist
- if (!imagePathInfo.Exists)
- {
- imagePathInfo.Create();
- }
- // Delete the previous image
- if (item.Name != model.Name)
+ // Check if item exist
+ if (item == null)
{
- var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
-
- if (oldFileName.Exists)
- {
- File.Delete(oldFileName.FullName);
- }
+ throw new Exception($"Unable to found the item with ID: {id}");
}
- // 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
@@ -127,15 +110,6 @@ namespace MyFirstBlazor.Services
// 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);
}
diff --git a/Sources/MyFirstBlazor/wwwroot/appsettings.json b/Sources/MyFirstBlazor/wwwroot/appsettings.json
new file mode 100644
index 0000000..ef6372d
--- /dev/null
+++ b/Sources/MyFirstBlazor/wwwroot/appsettings.json
@@ -0,0 +1,9 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Trace",
+ "Microsoft": "Warning",
+ "Microsoft.Hosting.Lifetime": "Information"
+ }
+ }
+}
\ No newline at end of file