From ed12e92b46f99e5846d1bb59c2fadb251046e751 Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 10:12:43 +0100 Subject: [PATCH 01/18] Creating a data service, editing pages and registering the data service --- BlazorTp1/BlazorTp1.csproj | 9 +++ BlazorTp1/Pages/Add.razor.cs | 60 ++++--------------- BlazorTp1/Pages/List.razor.cs | 13 +---- BlazorTp1/Program.cs | 2 + BlazorTp1/Services/DataLocalService.cs | 80 ++++++++++++++++++++++++++ BlazorTp1/Services/IDataService.cs | 10 ++++ 6 files changed, 115 insertions(+), 59 deletions(-) create mode 100644 BlazorTp1/Services/DataLocalService.cs create mode 100644 BlazorTp1/Services/IDataService.cs diff --git a/BlazorTp1/BlazorTp1.csproj b/BlazorTp1/BlazorTp1.csproj index c0eacf6..3105fce 100644 --- a/BlazorTp1/BlazorTp1.csproj +++ b/BlazorTp1/BlazorTp1.csproj @@ -6,6 +6,11 @@ enable + + + + + @@ -13,4 +18,8 @@ + + + + diff --git a/BlazorTp1/Pages/Add.razor.cs b/BlazorTp1/Pages/Add.razor.cs index 794e331..fc1fdf9 100644 --- a/BlazorTp1/Pages/Add.razor.cs +++ b/BlazorTp1/Pages/Add.razor.cs @@ -7,25 +7,11 @@ namespace BlazorTp1.Pages { public partial class Add { - [Inject] - public ILocalStorageService LocalStorage { get; set; } - - [Inject] - public NavigationManager NavigationManager { get; set; } - - [Inject] - public IWebHostEnvironment WebHostEnvironment { get; set; } - /// /// The default enchant categories. /// private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; - /// - /// The default repair with. - /// - private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" }; - /// /// The current item model /// @@ -35,44 +21,20 @@ namespace BlazorTp1.Pages RepairWith = new List() }; - private async void HandleValidSubmit() - { - // Get the current data - var currentData = await LocalStorage.GetItemAsync>("data"); - - // Simulate the Id - itemModel.Id = currentData.Max(s => s.Id) + 1; - - // Add the item to the current data - currentData.Add(new Item - { - Id = itemModel.Id, - DisplayName = itemModel.DisplayName, - Name = itemModel.Name, - RepairWith = itemModel.RepairWith, - EnchantCategories = itemModel.EnchantCategories, - MaxDurability = itemModel.MaxDurability, - StackSize = itemModel.StackSize, - CreatedDate = DateTime.Now - }); - - // Save the image - var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images"); - - // Check if the folder "images" exist - if (!imagePathInfo.Exists) - { - imagePathInfo.Create(); - } + /// + /// The default repair with. + /// + private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" }; - // Determine the image name - var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png"); + [Inject] + public IDataService DataService { get; set; } - // Write the file content - await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent); + [Inject] + public NavigationManager NavigationManager { get; set; } - // Save the data - await LocalStorage.SetItemAsync("data", currentData); + private async void HandleValidSubmit() + { + await DataService.Add(itemModel); NavigationManager.NavigateTo("list"); } diff --git a/BlazorTp1/Pages/List.razor.cs b/BlazorTp1/Pages/List.razor.cs index 4fd69ef..fbab704 100644 --- a/BlazorTp1/Pages/List.razor.cs +++ b/BlazorTp1/Pages/List.razor.cs @@ -12,14 +12,11 @@ namespace BlazorTp1.Pages private int totalItem; [Inject] - public HttpClient Http { get; set; } + public IDataService DataService { get; set; } [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } - [Inject] - public NavigationManager NavigationManager { get; set; } - private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) @@ -27,14 +24,10 @@ namespace BlazorTp1.Pages return; } - // When you use a real API, we use this follow code - //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); - var response = (await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); - if (!e.CancellationToken.IsCancellationRequested) { - totalItem = (await Http.GetFromJsonAsync>($"{NavigationManager.BaseUri}fake-data.json")).Count; - items = new List(response); // an actual data for the current page + items = await DataService.List(e.Page, e.PageSize); + totalItem = await DataService.Count(); } } } diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index 8d82ec6..99989cb 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -22,6 +22,8 @@ builder.Services builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs new file mode 100644 index 0000000..0fc6b16 --- /dev/null +++ b/BlazorTp1/Services/DataLocalService.cs @@ -0,0 +1,80 @@ +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(new Item + { + Id = model.Id, + DisplayName = model.DisplayName, + Name = model.Name, + RepairWith = model.RepairWith, + EnchantCategories = model.EnchantCategories, + MaxDurability = model.MaxDurability, + StackSize = model.StackSize, + CreatedDate = DateTime.Now + }); + + // 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 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(); + } +} \ No newline at end of file diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs new file mode 100644 index 0000000..a49c3c5 --- /dev/null +++ b/BlazorTp1/Services/IDataService.cs @@ -0,0 +1,10 @@ +using System; + +public interface IDataService +{ + Task Add(ItemModel model); + + Task Count(); + + Task> List(int currentPage, int pageSize); +} From a6d1e275fe12ff65b176fb517f119d3e22816e1c Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 10:16:16 +0100 Subject: [PATCH 02/18] fixed missing headers --- BlazorTp1/Program.cs | 2 +- BlazorTp1/Services/DataLocalService.cs | 6 +++++- BlazorTp1/Services/IDataService.cs | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index 99989cb..a4adb86 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -22,7 +22,7 @@ builder.Services builder.Services.AddBlazoredLocalStorage(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); var app = builder.Build(); diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 0fc6b16..ecde1df 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -1,4 +1,8 @@ -public class DataLocalService : IDataService +using Blazored.LocalStorage; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +public class DataLocalService : IDataService { private readonly HttpClient _http; private readonly ILocalStorageService _localStorage; diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs index a49c3c5..1b5ac23 100644 --- a/BlazorTp1/Services/IDataService.cs +++ b/BlazorTp1/Services/IDataService.cs @@ -1,4 +1,5 @@ -using System; +using BlazorTp1.Models; +using System; public interface IDataService { From 9f69b0c26c6326ef4bc76646171bafeec0464e66 Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 10:24:37 +0100 Subject: [PATCH 03/18] new methods to IDataService and DataLocalService --- BlazorTp1/Services/DataLocalService.cs | 74 ++++++++++++++++++++++++++ BlazorTp1/Services/IDataService.cs | 4 ++ 2 files changed, 78 insertions(+) diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index ecde1df..7268f8d 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -51,6 +51,8 @@ public class DataLocalService : IDataService imagePathInfo.Create(); } + + // Determine the image name var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); @@ -81,4 +83,76 @@ public class DataLocalService : IDataService return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); } + + //new methods + + 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 + item.DisplayName = model.DisplayName; + item.Name = model.Name; + item.RepairWith = model.RepairWith; + item.EnchantCategories = model.EnchantCategories; + item.MaxDurability = model.MaxDurability; + item.StackSize = model.StackSize; + item.UpdatedDate = DateTime.Now; + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } } \ No newline at end of file diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs index 1b5ac23..c489a15 100644 --- a/BlazorTp1/Services/IDataService.cs +++ b/BlazorTp1/Services/IDataService.cs @@ -8,4 +8,8 @@ public interface IDataService Task Count(); Task> List(int currentPage, int pageSize); + + Task GetById(int id); + + Task Update(int id, ItemModel item); } From 691f051327192c4652a6c422744e854b234837c8 Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 10:27:26 +0100 Subject: [PATCH 04/18] Edit action added --- BlazorTp1/Pages/List.razor | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/BlazorTp1/Pages/List.razor b/BlazorTp1/Pages/List.razor index 640d9f2..9080e62 100644 --- a/BlazorTp1/Pages/List.razor +++ b/BlazorTp1/Pages/List.razor @@ -42,5 +42,10 @@ @(string.Join(", ", ((Item)context).RepairWith)) - + + + + Editer + + From ed8e380f4e2cdcb704a1dc71883c117a1b32601b Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 11:08:32 +0100 Subject: [PATCH 05/18] Creation of the form + using the model --- BlazorTp1/Pages/Edit.cs | 118 +++++++++++++++++++++++++++++++++++++ BlazorTp1/Pages/Edit.razor | 82 ++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 BlazorTp1/Pages/Edit.cs create mode 100644 BlazorTp1/Pages/Edit.razor diff --git a/BlazorTp1/Pages/Edit.cs b/BlazorTp1/Pages/Edit.cs new file mode 100644 index 0000000..963ff13 --- /dev/null +++ b/BlazorTp1/Pages/Edit.cs @@ -0,0 +1,118 @@ +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; + +namespace BlazorTp1.Pages +{ + public partial class Edit + { + [Parameter] + public int Id { get; set; } + + /// + /// The default enchant categories. + /// + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + + /// + /// The current item model + /// + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + /// + /// The default repair with. + /// + private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" }; + + [Inject] + public IDataService DataService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + protected override async Task OnInitializedAsync() + { + var item = await DataService.GetById(Id); + + 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 = new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = fileContent + }; + } + + private async void HandleValidSubmit() + { + await DataService.Update(Id, itemModel); + + NavigationManager.NavigateTo("list"); + } + + private async Task LoadImage(InputFileChangeEventArgs e) + { + // Set the content of the image to the model + using (var memoryStream = new MemoryStream()) + { + await e.File.OpenReadStream().CopyToAsync(memoryStream); + itemModel.ImageContent = memoryStream.ToArray(); + } + } + + private void OnEnchantCategoriesChange(string item, object checkedValue) + { + if ((bool)checkedValue) + { + if (!itemModel.EnchantCategories.Contains(item)) + { + itemModel.EnchantCategories.Add(item); + } + + return; + } + + if (itemModel.EnchantCategories.Contains(item)) + { + itemModel.EnchantCategories.Remove(item); + } + } + + private void OnRepairWithChange(string item, object checkedValue) + { + if ((bool)checkedValue) + { + if (!itemModel.RepairWith.Contains(item)) + { + itemModel.RepairWith.Add(item); + } + + return; + } + + if (itemModel.RepairWith.Contains(item)) + { + itemModel.RepairWith.Remove(item); + } + } + } +} diff --git a/BlazorTp1/Pages/Edit.razor b/BlazorTp1/Pages/Edit.razor new file mode 100644 index 0000000..c18f772 --- /dev/null +++ b/BlazorTp1/Pages/Edit.razor @@ -0,0 +1,82 @@ +@page "/edit/{Id:int}" + +

Edit

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

+ @foreach (var item in enchantCategories) + { + + } +
+

+

+ Repair with: +

+ @foreach (var item in repairWith) + { + + } +
+

+

+ +

+

+ +

+

+ +

+ + +
From 076c444c705ff61da33037eb003fe193e6eed7c8 Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 11:16:33 +0100 Subject: [PATCH 06/18] Factory pattern created and implemented in Edit.cs --- BlazorTp1/Factories/ItemFactory.cs | 48 ++++++++++++++++++++++++++ BlazorTp1/Pages/Edit.cs | 15 ++------ BlazorTp1/Services/DataLocalService.cs | 23 ++---------- 3 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 BlazorTp1/Factories/ItemFactory.cs diff --git a/BlazorTp1/Factories/ItemFactory.cs b/BlazorTp1/Factories/ItemFactory.cs new file mode 100644 index 0000000..2a7e9fe --- /dev/null +++ b/BlazorTp1/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using BlazorTp1.Models; + +namespace BlazorTp1.Factories +{ + public static class ItemFactory + { + public static ItemModel ToModel(Item item, byte[] imageContent) + { + return new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = imageContent + }; + } + + public static Item Create(ItemModel model) + { + return new Item + { + Id = model.Id, + DisplayName = model.DisplayName, + Name = model.Name, + RepairWith = model.RepairWith, + EnchantCategories = model.EnchantCategories, + MaxDurability = model.MaxDurability, + StackSize = model.StackSize, + CreatedDate = DateTime.Now + }; + } + + public static void Update(Item item, ItemModel model) + { + item.DisplayName = model.DisplayName; + item.Name = model.Name; + item.RepairWith = model.RepairWith; + item.EnchantCategories = model.EnchantCategories; + item.MaxDurability = model.MaxDurability; + item.StackSize = model.StackSize; + item.UpdatedDate = DateTime.Now; + } + } +} diff --git a/BlazorTp1/Pages/Edit.cs b/BlazorTp1/Pages/Edit.cs index 963ff13..cdf97ab 100644 --- a/BlazorTp1/Pages/Edit.cs +++ b/BlazorTp1/Pages/Edit.cs @@ -1,4 +1,5 @@ -using BlazorTp1.Models; +using BlazorTp1.Factories; +using BlazorTp1.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; @@ -49,17 +50,7 @@ namespace BlazorTp1.Pages } // Set the model with the item - itemModel = new ItemModel - { - Id = item.Id, - DisplayName = item.DisplayName, - Name = item.Name, - RepairWith = item.RepairWith, - EnchantCategories = item.EnchantCategories, - MaxDurability = item.MaxDurability, - StackSize = item.StackSize, - ImageContent = fileContent - }; + itemModel = ItemFactory.ToModel(item, fileContent); } private async void HandleValidSubmit() diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 7268f8d..03de625 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -1,4 +1,5 @@ using Blazored.LocalStorage; +using BlazorTp1.Factories; using BlazorTp1.Models; using Microsoft.AspNetCore.Components; @@ -30,17 +31,7 @@ public class DataLocalService : IDataService model.Id = currentData.Max(s => s.Id) + 1; // Add the item to the current data - currentData.Add(new Item - { - Id = model.Id, - DisplayName = model.DisplayName, - Name = model.Name, - RepairWith = model.RepairWith, - EnchantCategories = model.EnchantCategories, - MaxDurability = model.MaxDurability, - StackSize = model.StackSize, - CreatedDate = DateTime.Now - }); + currentData.Add(ItemFactory.Create(model)); // Save the image var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); @@ -51,8 +42,6 @@ public class DataLocalService : IDataService imagePathInfo.Create(); } - - // Determine the image name var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); @@ -144,13 +133,7 @@ public class DataLocalService : IDataService await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); // Modify the content of the item - item.DisplayName = model.DisplayName; - item.Name = model.Name; - item.RepairWith = model.RepairWith; - item.EnchantCategories = model.EnchantCategories; - item.MaxDurability = model.MaxDurability; - item.StackSize = model.StackSize; - item.UpdatedDate = DateTime.Now; + ItemFactory.Update(item, model); // Save the data await _localStorage.SetItemAsync("data", currentData); From 2049302c564a4cb441972e11103a8ac3920eebca Mon Sep 17 00:00:00 2001 From: runtenick Date: Sun, 13 Nov 2022 08:45:28 +0100 Subject: [PATCH 07/18] Modal added --- BlazorTp1/App.razor | 26 ++++++++++++++------------ BlazorTp1/BlazorTp1.csproj | 1 + BlazorTp1/Pages/List.razor | 1 + BlazorTp1/Pages/List.razor.cs | 4 ++++ BlazorTp1/Pages/_Layout.cshtml | 3 +++ BlazorTp1/Program.cs | 3 +++ BlazorTp1/Services/DataLocalService.cs | 24 ++++++++++++++++++++++++ BlazorTp1/Services/IDataService.cs | 2 ++ BlazorTp1/_Imports.razor | 2 ++ 9 files changed, 54 insertions(+), 12 deletions(-) diff --git a/BlazorTp1/App.razor b/BlazorTp1/App.razor index 6fd3ed1..36bc2e6 100644 --- a/BlazorTp1/App.razor +++ b/BlazorTp1/App.razor @@ -1,12 +1,14 @@ - - - - - - - Not found - -

Sorry, there's nothing at this address.

-
-
-
+ + + + + + + + Not found + +

Sorry, there's nothing at this address.

+
+
+
+
diff --git a/BlazorTp1/BlazorTp1.csproj b/BlazorTp1/BlazorTp1.csproj index 3105fce..ce09efd 100644 --- a/BlazorTp1/BlazorTp1.csproj +++ b/BlazorTp1/BlazorTp1.csproj @@ -13,6 +13,7 @@ + diff --git a/BlazorTp1/Pages/List.razor b/BlazorTp1/Pages/List.razor index 9080e62..1bd5c64 100644 --- a/BlazorTp1/Pages/List.razor +++ b/BlazorTp1/Pages/List.razor @@ -46,6 +46,7 @@ Editer + diff --git a/BlazorTp1/Pages/List.razor.cs b/BlazorTp1/Pages/List.razor.cs index fbab704..1295274 100644 --- a/BlazorTp1/Pages/List.razor.cs +++ b/BlazorTp1/Pages/List.razor.cs @@ -30,5 +30,9 @@ namespace BlazorTp1.Pages totalItem = await DataService.Count(); } } + private void OnDelete(int id) + { + + } } } diff --git a/BlazorTp1/Pages/_Layout.cshtml b/BlazorTp1/Pages/_Layout.cshtml index 641ca97..a30151a 100644 --- a/BlazorTp1/Pages/_Layout.cshtml +++ b/BlazorTp1/Pages/_Layout.cshtml @@ -34,5 +34,8 @@ + + + diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index a4adb86..993a31c 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -3,6 +3,7 @@ using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using BlazorTp1.Data; +using Blazored.Modal; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; @@ -24,6 +25,8 @@ builder.Services.AddBlazoredLocalStorage(); builder.Services.AddScoped(); +builder.Services.AddBlazoredModal(); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 03de625..367f128 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -138,4 +138,28 @@ public class DataLocalService : IDataService // 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); + } } \ No newline at end of file diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs index c489a15..c621463 100644 --- a/BlazorTp1/Services/IDataService.cs +++ b/BlazorTp1/Services/IDataService.cs @@ -12,4 +12,6 @@ public interface IDataService Task GetById(int id); Task Update(int id, ItemModel item); + + Task Delete(int id); } diff --git a/BlazorTp1/_Imports.razor b/BlazorTp1/_Imports.razor index 009cdfc..fb7258e 100644 --- a/BlazorTp1/_Imports.razor +++ b/BlazorTp1/_Imports.razor @@ -9,3 +9,5 @@ @using BlazorTp1 @using BlazorTp1.Shared @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services \ No newline at end of file From de97aa4dd86da920ffde725a5e8c2a5d112f63ba Mon Sep 17 00:00:00 2001 From: runtenick Date: Sun, 13 Nov 2022 08:54:51 +0100 Subject: [PATCH 08/18] Delete confirmation pop-up made --- BlazorTp1/Modals/DeleteConfirmation.razor | 12 +++++++ BlazorTp1/Modals/DeleteConfirmation.razor.cs | 37 ++++++++++++++++++++ BlazorTp1/Pages/{Edit.cs => Edit.razor.cs} | 0 BlazorTp1/Pages/List.razor.cs | 25 ++++++++++++- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 BlazorTp1/Modals/DeleteConfirmation.razor create mode 100644 BlazorTp1/Modals/DeleteConfirmation.razor.cs rename BlazorTp1/Pages/{Edit.cs => Edit.razor.cs} (100%) diff --git a/BlazorTp1/Modals/DeleteConfirmation.razor b/BlazorTp1/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..6435118 --- /dev/null +++ b/BlazorTp1/Modals/DeleteConfirmation.razor @@ -0,0 +1,12 @@ +

DeleteConfirmation

+ +
+ +

+ Are you sure you want to delete @item.DisplayName ? +

+ + + + +
diff --git a/BlazorTp1/Modals/DeleteConfirmation.razor.cs b/BlazorTp1/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..1b4c6e7 --- /dev/null +++ b/BlazorTp1/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,37 @@ +using Blazored.Modal.Services; +using Blazored.Modal; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Modals +{ + public partial class DeleteConfirmation + { + [CascadingParameter] + public BlazoredModalInstance ModalInstance { get; set; } + + [Inject] + public IDataService DataService { get; set; } + + [Parameter] + public int Id { get; set; } + + private Item item = new Item(); + + protected override async Task OnInitializedAsync() + { + // Get the item + item = await DataService.GetById(Id); + } + + void ConfirmDelete() + { + ModalInstance.CloseAsync(ModalResult.Ok(true)); + } + + void Cancel() + { + ModalInstance.CancelAsync(); + } + } +} diff --git a/BlazorTp1/Pages/Edit.cs b/BlazorTp1/Pages/Edit.razor.cs similarity index 100% rename from BlazorTp1/Pages/Edit.cs rename to BlazorTp1/Pages/Edit.razor.cs diff --git a/BlazorTp1/Pages/List.razor.cs b/BlazorTp1/Pages/List.razor.cs index 1295274..2cc442a 100644 --- a/BlazorTp1/Pages/List.razor.cs +++ b/BlazorTp1/Pages/List.razor.cs @@ -2,6 +2,9 @@ using BlazorTp1.Models; using Blazorise.DataGrid; using Blazored.LocalStorage; +using Blazored.Modal.Services; +using Blazored.Modal; +using BlazorTp1.Modals; namespace BlazorTp1.Pages { @@ -17,6 +20,12 @@ namespace BlazorTp1.Pages [Inject] public IWebHostEnvironment WebHostEnvironment { get; set; } + [Inject] + public NavigationManager NavigationManager { get; set; } + + [CascadingParameter] + public IModalService Modal { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) @@ -30,9 +39,23 @@ namespace BlazorTp1.Pages totalItem = await DataService.Count(); } } - private void OnDelete(int id) + private async void OnDelete(int id) { + var parameters = new ModalParameters(); + parameters.Add(nameof(Item.Id), id); + + var modal = Modal.Show("Delete Confirmation", parameters); + var result = await modal.Result; + + if (result.Cancelled) + { + return; + } + + await DataService.Delete(id); + // Reload the page + NavigationManager.NavigateTo("list", true); } } } From 17d0394bb7676ad7d70acdf11f5b7dc577b289b2 Mon Sep 17 00:00:00 2001 From: runtenick Date: Sun, 13 Nov 2022 09:12:18 +0100 Subject: [PATCH 09/18] Localization and Globalization done --- BlazorTp1/BlazorTp1.csproj | 1 + BlazorTp1/Controllers/CultureController.cs | 31 ++++++ BlazorTp1/Pages/Index.razor | 5 + BlazorTp1/Pages/List.razor | 2 +- BlazorTp1/Pages/List.razor.cs | 4 + BlazorTp1/Program.cs | 35 ++++++ BlazorTp1/Resources/Pages.List.fr-FR.resx | 123 +++++++++++++++++++++ BlazorTp1/Resources/Pages.List.resx | 123 +++++++++++++++++++++ BlazorTp1/Shared/CultureSelector.razor | 43 +++++++ BlazorTp1/Shared/MainLayout.razor | 4 + 10 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 BlazorTp1/Controllers/CultureController.cs create mode 100644 BlazorTp1/Resources/Pages.List.fr-FR.resx create mode 100644 BlazorTp1/Resources/Pages.List.resx create mode 100644 BlazorTp1/Shared/CultureSelector.razor diff --git a/BlazorTp1/BlazorTp1.csproj b/BlazorTp1/BlazorTp1.csproj index ce09efd..7d44c3c 100644 --- a/BlazorTp1/BlazorTp1.csproj +++ b/BlazorTp1/BlazorTp1.csproj @@ -17,6 +17,7 @@ +
diff --git a/BlazorTp1/Controllers/CultureController.cs b/BlazorTp1/Controllers/CultureController.cs new file mode 100644 index 0000000..328c28e --- /dev/null +++ b/BlazorTp1/Controllers/CultureController.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + +/// +/// The culture controller. +/// +[Route("[controller]/[action]")] +public class CultureController : Controller +{ + /// + /// Sets the culture. + /// + /// The culture. + /// The redirect URI. + /// + /// The action result. + /// + public IActionResult SetCulture(string culture, string redirectUri) + { + if (culture != null) + { + // Define a cookie with the selected culture + this.HttpContext.Response.Cookies.Append( + CookieRequestCultureProvider.DefaultCookieName, + CookieRequestCultureProvider.MakeCookieValue( + new RequestCulture(culture))); + } + + return this.LocalRedirect(redirectUri); + } +} \ No newline at end of file diff --git a/BlazorTp1/Pages/Index.razor b/BlazorTp1/Pages/Index.razor index 6085c4a..9fa6e36 100644 --- a/BlazorTp1/Pages/Index.razor +++ b/BlazorTp1/Pages/Index.razor @@ -1,4 +1,5 @@ @page "/" +@using System.Globalization Index @@ -6,4 +7,8 @@ Welcome to your new app. +

+ CurrentCulture: @CultureInfo.CurrentCulture +

+ diff --git a/BlazorTp1/Pages/List.razor b/BlazorTp1/Pages/List.razor index 1bd5c64..c4fef1e 100644 --- a/BlazorTp1/Pages/List.razor +++ b/BlazorTp1/Pages/List.razor @@ -1,7 +1,7 @@ @page "/list" @using BlazorTp1.Models -

List

+

@Localizer["Title"]

diff --git a/BlazorTp1/Pages/List.razor.cs b/BlazorTp1/Pages/List.razor.cs index 2cc442a..f20a1fb 100644 --- a/BlazorTp1/Pages/List.razor.cs +++ b/BlazorTp1/Pages/List.razor.cs @@ -5,6 +5,7 @@ using Blazored.LocalStorage; using Blazored.Modal.Services; using Blazored.Modal; using BlazorTp1.Modals; +using Microsoft.Extensions.Localization; namespace BlazorTp1.Pages { @@ -26,6 +27,9 @@ namespace BlazorTp1.Pages [CascadingParameter] public IModalService Modal { get; set; } + [Inject] + public IStringLocalizer Localizer { get; set; } + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index 993a31c..e3cd109 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -6,6 +6,9 @@ using BlazorTp1.Data; using Blazored.Modal; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Localization; +using System.Globalization; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -27,6 +30,23 @@ builder.Services.AddScoped(); builder.Services.AddBlazoredModal(); +// Add the controller of the app +builder.Services.AddControllers(); + +// Add the localization to the app and specify the resources path +builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); + +// Configure the localtization +builder.Services.Configure(options => +{ + // Set the default culture of the web site + options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US")); + + // Declare the supported culture + options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; + options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; +}); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -43,6 +63,21 @@ app.UseStaticFiles(); app.UseRouting(); +// Get the current localization options +var options = ((IApplicationBuilder)app).ApplicationServices.GetService>(); + +if (options?.Value != null) +{ + // use the default localization + app.UseRequestLocalization(options.Value); +} + +// Add the controller to the endpoint +app.UseEndpoints(endpoints => +{ + endpoints.MapControllers(); +}); + app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); diff --git a/BlazorTp1/Resources/Pages.List.fr-FR.resx b/BlazorTp1/Resources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..941e1c7 --- /dev/null +++ b/BlazorTp1/Resources/Pages.List.fr-FR.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Liste d'éléments + + \ No newline at end of file diff --git a/BlazorTp1/Resources/Pages.List.resx b/BlazorTp1/Resources/Pages.List.resx new file mode 100644 index 0000000..ae67689 --- /dev/null +++ b/BlazorTp1/Resources/Pages.List.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Items List + + \ No newline at end of file diff --git a/BlazorTp1/Shared/CultureSelector.razor b/BlazorTp1/Shared/CultureSelector.razor new file mode 100644 index 0000000..0f46a4e --- /dev/null +++ b/BlazorTp1/Shared/CultureSelector.razor @@ -0,0 +1,43 @@ +@using System.Globalization +@inject NavigationManager NavigationManager + +

+ +

+ +@code +{ + private CultureInfo[] supportedCultures = new[] + { + new CultureInfo("en-US"), + new CultureInfo("fr-FR") + }; + + private CultureInfo Culture + { + get => CultureInfo.CurrentCulture; + set + { + if (CultureInfo.CurrentUICulture == value) + { + return; + } + + var culture = value.Name.ToLower(CultureInfo.InvariantCulture); + + var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped); + var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}"; + + // Redirect the user to the culture controller to set the cookie + this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true); + } + } +} diff --git a/BlazorTp1/Shared/MainLayout.razor b/BlazorTp1/Shared/MainLayout.razor index 46ade29..e497265 100644 --- a/BlazorTp1/Shared/MainLayout.razor +++ b/BlazorTp1/Shared/MainLayout.razor @@ -12,6 +12,10 @@ About
+
+ +
+
@Body
From 4ed1eb0d494175b3936fdca3f3be62b818f6a67b Mon Sep 17 00:00:00 2001 From: runtenick Date: Sun, 13 Nov 2022 11:07:38 +0100 Subject: [PATCH 10/18] Utilisation du composant, erreur: java interops calls --- BlazorTp1/Components/Crafting.razor | 53 +++++++++++++ BlazorTp1/Components/Crafting.razor.cs | 86 ++++++++++++++++++++++ BlazorTp1/Components/Crafting.razor.css | 19 +++++ BlazorTp1/Components/Crafting.razor.js | 16 ++++ BlazorTp1/Components/CraftingAction.cs | 11 +++ BlazorTp1/Components/CraftingItem.razor | 16 ++++ BlazorTp1/Components/CraftingItem.razor.cs | 64 ++++++++++++++++ BlazorTp1/Components/CraftingRecipe.cs | 10 +++ BlazorTp1/Pages/Index.razor | 6 +- BlazorTp1/Pages/Index.razor.cs | 24 ++++++ BlazorTp1/Pages/_Layout.cshtml | 2 + BlazorTp1/Services/DataLocalService.cs | 20 +++++ BlazorTp1/Services/IDataService.cs | 5 +- BlazorTp1/wwwroot/index.html | 12 +++ 14 files changed, 342 insertions(+), 2 deletions(-) create mode 100644 BlazorTp1/Components/Crafting.razor create mode 100644 BlazorTp1/Components/Crafting.razor.cs create mode 100644 BlazorTp1/Components/Crafting.razor.css create mode 100644 BlazorTp1/Components/Crafting.razor.js create mode 100644 BlazorTp1/Components/CraftingAction.cs create mode 100644 BlazorTp1/Components/CraftingItem.razor create mode 100644 BlazorTp1/Components/CraftingItem.razor.cs create mode 100644 BlazorTp1/Components/CraftingRecipe.cs create mode 100644 BlazorTp1/Pages/Index.razor.cs create mode 100644 BlazorTp1/wwwroot/index.html diff --git a/BlazorTp1/Components/Crafting.razor b/BlazorTp1/Components/Crafting.razor new file mode 100644 index 0000000..b6c8ec7 --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor @@ -0,0 +1,53 @@ +

Crafting

+ + +
+
+
+ +
Available items:
+
+
+ + @foreach (var item in Items) + { + + } +
+
+ +
+ +
+
Recipe
+ +
+ +
+ + + + + + + + + +
+
+ +
Result
+
+ +
+
+ +
+
Actions
+
+ +
+
+
+
+
diff --git a/BlazorTp1/Components/Crafting.razor.cs b/BlazorTp1/Components/Crafting.razor.cs new file mode 100644 index 0000000..8f7d934 --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor.cs @@ -0,0 +1,86 @@ +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace BlazorTp1.Components +{ + public partial class Crafting + { + private Item _recipeResult; + + public Crafting() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + this.RecipeItems = new List { null, null, null, null, null, null, null, null, null }; + } + + public ObservableCollection Actions { get; set; } + public Item CurrentDragItem { get; set; } + + [Parameter] + public List Items { get; set; } + + public List RecipeItems { get; set; } + + //se der ruim tirar esse attributo + [CascadingParameter] + public Crafting Parent { get; set; } + + public Item RecipeResult + { + get => this._recipeResult; + set + { + if (this._recipeResult == value) + { + return; + } + + this._recipeResult = value; + this.StateHasChanged(); + } + } + + [Parameter] + public List Recipes { get; set; } + + + /// + /// Gets or sets the java script runtime. + /// + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } + + public void CheckRecipe() + { + RecipeResult = null; + + // Get the current model + var currentModel = string.Join("|", this.RecipeItems.Select(s => s != null ? s.Name : string.Empty)); + + this.Actions.Add(new CraftingAction { Action = $"Items : {currentModel}" }); + + foreach (var craftingRecipe in Recipes) + { + // Get the recipe model + var recipeModel = string.Join("|", craftingRecipe.Have.SelectMany(s => s)); + + this.Actions.Add(new CraftingAction { Action = $"Recipe model : {recipeModel}" }); + + if (currentModel == recipeModel) + { + RecipeResult = craftingRecipe.Give; + } + } + } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + } + } +} + diff --git a/BlazorTp1/Components/Crafting.razor.css b/BlazorTp1/Components/Crafting.razor.css new file mode 100644 index 0000000..2a388f2 --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor.css @@ -0,0 +1,19 @@ +.css-grid { + grid-template-columns: repeat(4,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 286px; +} + +.css-recipe { + grid-template-columns: repeat(3,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 212px; +} + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} diff --git a/BlazorTp1/Components/Crafting.razor.js b/BlazorTp1/Components/Crafting.razor.js new file mode 100644 index 0000000..8fdb58e --- /dev/null +++ b/BlazorTp1/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/BlazorTp1/Components/CraftingAction.cs b/BlazorTp1/Components/CraftingAction.cs new file mode 100644 index 0000000..f682f85 --- /dev/null +++ b/BlazorTp1/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using BlazorTp1.Models; + +namespace BlazorTp1.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/BlazorTp1/Components/CraftingItem.razor b/BlazorTp1/Components/CraftingItem.razor new file mode 100644 index 0000000..b9c9fd3 --- /dev/null +++ b/BlazorTp1/Components/CraftingItem.razor @@ -0,0 +1,16 @@ +

CraftingItem

+ +
+ + @if (Item != null) + { + @Item.DisplayName + } +
diff --git a/BlazorTp1/Components/CraftingItem.razor.cs b/BlazorTp1/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..e4d225b --- /dev/null +++ b/BlazorTp1/Components/CraftingItem.razor.cs @@ -0,0 +1,64 @@ +using Blazorise; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Components +{ + public partial class CraftingItem + { + [Parameter] + public int Index { get; set; } + + [Parameter] + public Item Item { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + + [CascadingParameter] + public Crafting Parent { get; set; } + + internal void OnDragEnter() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + } + + internal void OnDragLeave() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + } + + internal void OnDrop() + { + if (NoDrop) + { + return; + } + + this.Item = Parent.CurrentDragItem; + Parent.RecipeItems[this.Index] = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); + + // Check recipe + Parent.CheckRecipe(); + } + + private void OnDragStart() + { + Parent.CurrentDragItem = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + } +} diff --git a/BlazorTp1/Components/CraftingRecipe.cs b/BlazorTp1/Components/CraftingRecipe.cs new file mode 100644 index 0000000..2da1ed1 --- /dev/null +++ b/BlazorTp1/Components/CraftingRecipe.cs @@ -0,0 +1,10 @@ +using BlazorTp1.Models; + +namespace BlazorTp1.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/BlazorTp1/Pages/Index.razor b/BlazorTp1/Pages/Index.razor index 9fa6e36..c4e48ca 100644 --- a/BlazorTp1/Pages/Index.razor +++ b/BlazorTp1/Pages/Index.razor @@ -3,7 +3,7 @@ Index -

Hello, world!

+

Tp Blazor Nicolas Franco

Welcome to your new app. @@ -11,4 +11,8 @@ Welcome to your new app. CurrentCulture: @CultureInfo.CurrentCulture

+
+ +
+ diff --git a/BlazorTp1/Pages/Index.razor.cs b/BlazorTp1/Pages/Index.razor.cs new file mode 100644 index 0000000..1a5c3f8 --- /dev/null +++ b/BlazorTp1/Pages/Index.razor.cs @@ -0,0 +1,24 @@ +using BlazorTp1.Components; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Pages +{ + public partial class Index + { + [Inject] + public IDataService DataService { get; set; } + + public List Items { get; set; } = new List(); + + private List Recipes { get; set; } = new List(); + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + } + } +} diff --git a/BlazorTp1/Pages/_Layout.cshtml b/BlazorTp1/Pages/_Layout.cshtml index a30151a..ea9b1bb 100644 --- a/BlazorTp1/Pages/_Layout.cshtml +++ b/BlazorTp1/Pages/_Layout.cshtml @@ -37,5 +37,7 @@ + + diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 367f128..13f109d 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -1,4 +1,5 @@ using Blazored.LocalStorage; +using BlazorTp1.Components; using BlazorTp1.Factories; using BlazorTp1.Models; using Microsoft.AspNetCore.Components; @@ -162,4 +163,23 @@ public class DataLocalService : IDataService // 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); + } } \ No newline at end of file diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs index c621463..a03b7ad 100644 --- a/BlazorTp1/Services/IDataService.cs +++ b/BlazorTp1/Services/IDataService.cs @@ -1,4 +1,5 @@ -using BlazorTp1.Models; +using BlazorTp1.Components; +using BlazorTp1.Models; using System; public interface IDataService @@ -14,4 +15,6 @@ public interface IDataService Task Update(int id, ItemModel item); Task Delete(int id); + + Task> GetRecipes(); } diff --git a/BlazorTp1/wwwroot/index.html b/BlazorTp1/wwwroot/index.html new file mode 100644 index 0000000..8cb9f7a --- /dev/null +++ b/BlazorTp1/wwwroot/index.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file From 93e3f989d88565a05427cb2fabf55b19457251dc Mon Sep 17 00:00:00 2001 From: runtenick Date: Thu, 17 Nov 2022 21:29:38 +0100 Subject: [PATCH 11/18] error fix --- BlazorTp1/Components/Card.razor | 6 ++++++ BlazorTp1/Pages/Index.razor | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 BlazorTp1/Components/Card.razor diff --git a/BlazorTp1/Components/Card.razor b/BlazorTp1/Components/Card.razor new file mode 100644 index 0000000..abd5aab --- /dev/null +++ b/BlazorTp1/Components/Card.razor @@ -0,0 +1,6 @@ +using BlazorTp1.Models; +

Card

+ +@code { + +} diff --git a/BlazorTp1/Pages/Index.razor b/BlazorTp1/Pages/Index.razor index c4e48ca..2e2907d 100644 --- a/BlazorTp1/Pages/Index.razor +++ b/BlazorTp1/Pages/Index.razor @@ -15,4 +15,24 @@ Welcome to your new app. + + +
+ My Templated Component +
+
+ +
+
Welcome To Template Component
+
+
+ + + +
+ + + From eb317e8dc6afcd59c60758326d287d1ddee7c7eb Mon Sep 17 00:00:00 2001 From: runtenick Date: Thu, 17 Nov 2022 21:49:02 +0100 Subject: [PATCH 12/18] error onRenderAsync fixed --- BlazorTp1/Components/Card.razor | 1 - 1 file changed, 1 deletion(-) diff --git a/BlazorTp1/Components/Card.razor b/BlazorTp1/Components/Card.razor index abd5aab..d2fe1aa 100644 --- a/BlazorTp1/Components/Card.razor +++ b/BlazorTp1/Components/Card.razor @@ -1,4 +1,3 @@ -using BlazorTp1.Models;

Card

@code { From 9e0d0dfa4be4bad9649b30b1ddd18ac2a1aa4755 Mon Sep 17 00:00:00 2001 From: runtenick Date: Thu, 17 Nov 2022 22:11:59 +0100 Subject: [PATCH 13/18] component razor remade --- BlazorTp1/Components/Card.razor | 11 ++++++----- BlazorTp1/Components/Card.razor.cs | 21 +++++++++++++++++++++ BlazorTp1/Components/Crafting.razor | 1 - BlazorTp1/Components/Crafting.razor.cs | 5 ----- BlazorTp1/Components/CraftingItem.razor.css | 6 ++++++ BlazorTp1/Components/ShowItems.razor | 12 ++++++++++++ BlazorTp1/Components/ShowItems.razor.cs | 13 +++++++++++++ BlazorTp1/Pages/Index.razor | 19 +------------------ BlazorTp1/Pages/Index.razor.cs | 13 ++++++++++--- 9 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 BlazorTp1/Components/Card.razor.cs create mode 100644 BlazorTp1/Components/CraftingItem.razor.css create mode 100644 BlazorTp1/Components/ShowItems.razor create mode 100644 BlazorTp1/Components/ShowItems.razor.cs diff --git a/BlazorTp1/Components/Card.razor b/BlazorTp1/Components/Card.razor index d2fe1aa..00d680f 100644 --- a/BlazorTp1/Components/Card.razor +++ b/BlazorTp1/Components/Card.razor @@ -1,5 +1,6 @@ -

Card

- -@code { - -} +@typeparam TItem +
+ @CardHeader(Item) + @CardBody(Item) + @CardFooter +
diff --git a/BlazorTp1/Components/Card.razor.cs b/BlazorTp1/Components/Card.razor.cs new file mode 100644 index 0000000..03dc91e --- /dev/null +++ b/BlazorTp1/Components/Card.razor.cs @@ -0,0 +1,21 @@ +using Blazorise; +using BlazorTp1.Models; +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Components +{ + public partial class Card + { + [Parameter] + public RenderFragment CardBody { get; set; } + + [Parameter] + public RenderFragment CardFooter { get; set; } + + [Parameter] + public RenderFragment CardHeader { get; set; } + + [Parameter] + public TItem Item { get; set; } + } +} diff --git a/BlazorTp1/Components/Crafting.razor b/BlazorTp1/Components/Crafting.razor index b6c8ec7..0aacb92 100644 --- a/BlazorTp1/Components/Crafting.razor +++ b/BlazorTp1/Components/Crafting.razor @@ -41,7 +41,6 @@ -
Actions
diff --git a/BlazorTp1/Components/Crafting.razor.cs b/BlazorTp1/Components/Crafting.razor.cs index 8f7d934..4282903 100644 --- a/BlazorTp1/Components/Crafting.razor.cs +++ b/BlazorTp1/Components/Crafting.razor.cs @@ -25,10 +25,6 @@ namespace BlazorTp1.Components public List RecipeItems { get; set; } - //se der ruim tirar esse attributo - [CascadingParameter] - public Crafting Parent { get; set; } - public Item RecipeResult { get => this._recipeResult; @@ -47,7 +43,6 @@ namespace BlazorTp1.Components [Parameter] public List Recipes { get; set; } - /// /// Gets or sets the java script runtime. /// diff --git a/BlazorTp1/Components/CraftingItem.razor.css b/BlazorTp1/Components/CraftingItem.razor.css new file mode 100644 index 0000000..b2d4521 --- /dev/null +++ b/BlazorTp1/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} diff --git a/BlazorTp1/Components/ShowItems.razor b/BlazorTp1/Components/ShowItems.razor new file mode 100644 index 0000000..7739061 --- /dev/null +++ b/BlazorTp1/Components/ShowItems.razor @@ -0,0 +1,12 @@ +@typeparam TItem + +
+ @if ((Items?.Count ?? 0) != 0) + { + @foreach (var item in Items) + { + @ShowTemplate(item) + ; + } + } +
\ No newline at end of file diff --git a/BlazorTp1/Components/ShowItems.razor.cs b/BlazorTp1/Components/ShowItems.razor.cs new file mode 100644 index 0000000..a452fcc --- /dev/null +++ b/BlazorTp1/Components/ShowItems.razor.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Components; + +namespace BlazorTp1.Components +{ + public partial class ShowItems + { + [Parameter] + public List Items { get; set; } + + [Parameter] + public RenderFragment ShowTemplate { get; set; } + } +} diff --git a/BlazorTp1/Pages/Index.razor b/BlazorTp1/Pages/Index.razor index 2e2907d..b0e6e78 100644 --- a/BlazorTp1/Pages/Index.razor +++ b/BlazorTp1/Pages/Index.razor @@ -1,5 +1,6 @@ @page "/" @using System.Globalization +@using BlazorTp1.Components Index @@ -15,24 +16,6 @@ Welcome to your new app.
- - -
- My Templated Component -
-
- -
-
Welcome To Template Component
-
-
- - - -
- diff --git a/BlazorTp1/Pages/Index.razor.cs b/BlazorTp1/Pages/Index.razor.cs index 1a5c3f8..36832ff 100644 --- a/BlazorTp1/Pages/Index.razor.cs +++ b/BlazorTp1/Pages/Index.razor.cs @@ -7,18 +7,25 @@ namespace BlazorTp1.Pages public partial class Index { [Inject] - public IDataService DataService { get; set; } + public IDataService? DataService { get; set; } public List Items { get; set; } = new List(); private List Recipes { get; set; } = new List(); - protected override async Task OnInitializedAsync() + protected override async Task OnAfterRenderAsync(bool firstRender) { - await base.OnInitializedAsync(); + base.OnAfterRenderAsync(firstRender); + + if (!firstRender) + { + return; + } Items = await DataService.List(0, await DataService.Count()); Recipes = await DataService.GetRecipes(); + + StateHasChanged(); } } } From a7662f699e4d122762fa5d98197384d3fd69f834 Mon Sep 17 00:00:00 2001 From: Nicolas FRANCO Date: Fri, 18 Nov 2022 11:10:21 +0100 Subject: [PATCH 14/18] modal error --- BlazorTp1/Pages/_Layout.cshtml | 72 +++++++++++++------------- BlazorTp1/Program.cs | 10 ++-- BlazorTp1/Services/DataLocalService.cs | 10 ++++ 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/BlazorTp1/Pages/_Layout.cshtml b/BlazorTp1/Pages/_Layout.cshtml index ea9b1bb..10372ee 100644 --- a/BlazorTp1/Pages/_Layout.cshtml +++ b/BlazorTp1/Pages/_Layout.cshtml @@ -4,40 +4,40 @@ - - - - - - - - - - - @RenderBody() - -
- - An error has occurred. This application may no longer respond until reloaded. - - - An unhandled exception has occurred. See browser dev tools for details. - - Reload - 🗙 -
- - - - - - - - - - - - - - + + + + + + + + + + + + @RenderBody() + +
+ + An error has occurred. This application may no longer respond until reloaded. + + + An unhandled exception has occurred. See browser dev tools for details. + + Reload + 🗙 +
+ + + + + + + + + + + + + diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index e3cd109..97cb8f6 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -1,14 +1,12 @@ using Blazored.LocalStorage; +using Blazored.Modal; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using BlazorTp1.Data; -using Blazored.Modal; -using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Localization; -using System.Globalization; using Microsoft.Extensions.Options; +using System.Globalization; var builder = WebApplication.CreateBuilder(args); @@ -19,6 +17,8 @@ builder.Services.AddSingleton(); builder.Services.AddHttpClient(); +builder.Services.AddBlazoredModal(); + builder.Services .AddBlazorise() .AddBootstrapProviders() @@ -28,8 +28,6 @@ builder.Services.AddBlazoredLocalStorage(); builder.Services.AddScoped(); -builder.Services.AddBlazoredModal(); - // Add the controller of the app builder.Services.AddControllers(); diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 13f109d..9154eb7 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -55,6 +55,16 @@ public class DataLocalService : IDataService public async Task Count() { + // 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")).Length; } From 34e26e982c018f0a8df033aeab6d8acfefe4eb01 Mon Sep 17 00:00:00 2001 From: Nicolas FRANCO Date: Fri, 18 Nov 2022 11:48:04 +0100 Subject: [PATCH 15/18] Api: make HTTP requests done --- BlazorTp1/BlazorTp1.csproj | 4 -- BlazorTp1/Factories/ItemFactory.cs | 7 +++- BlazorTp1/Models/Item.cs | 1 + BlazorTp1/Models/ItemModel.cs | 1 + BlazorTp1/Pages/Edit.razor | 6 +++ BlazorTp1/Pages/Edit.razor.cs | 5 --- BlazorTp1/Pages/List.razor | 4 +- BlazorTp1/Pages/_Layout.cshtml | 8 ++-- BlazorTp1/Program.cs | 3 ++ BlazorTp1/Services/DataApiService.cs | 58 ++++++++++++++++++++++++++ BlazorTp1/Services/DataLocalService.cs | 50 ---------------------- 11 files changed, 79 insertions(+), 68 deletions(-) create mode 100644 BlazorTp1/Services/DataApiService.cs diff --git a/BlazorTp1/BlazorTp1.csproj b/BlazorTp1/BlazorTp1.csproj index 7d44c3c..2b73896 100644 --- a/BlazorTp1/BlazorTp1.csproj +++ b/BlazorTp1/BlazorTp1.csproj @@ -20,8 +20,4 @@ - - - - diff --git a/BlazorTp1/Factories/ItemFactory.cs b/BlazorTp1/Factories/ItemFactory.cs index 2a7e9fe..8a87eb4 100644 --- a/BlazorTp1/Factories/ItemFactory.cs +++ b/BlazorTp1/Factories/ItemFactory.cs @@ -15,7 +15,8 @@ namespace BlazorTp1.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 BlazorTp1.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 BlazorTp1.Factories item.MaxDurability = model.MaxDurability; item.StackSize = model.StackSize; item.UpdatedDate = DateTime.Now; + item.ImageBase64 = Convert.ToBase64String(model.ImageContent); } } } diff --git a/BlazorTp1/Models/Item.cs b/BlazorTp1/Models/Item.cs index e697f5a..2ca8525 100644 --- a/BlazorTp1/Models/Item.cs +++ b/BlazorTp1/Models/Item.cs @@ -11,5 +11,6 @@ public List RepairWith { get; set; } public DateTime CreatedDate { get; set; } public DateTime? UpdatedDate { get; set; } + public string ImageBase64 { get; set; } } } diff --git a/BlazorTp1/Models/ItemModel.cs b/BlazorTp1/Models/ItemModel.cs index cf93333..7790cc2 100644 --- a/BlazorTp1/Models/ItemModel.cs +++ b/BlazorTp1/Models/ItemModel.cs @@ -33,5 +33,6 @@ namespace BlazorTp1.Models [Required(ErrorMessage = "The image of the item is mandatory!")] public byte[] ImageContent { get; set; } + public string ImageBase64 { get; set; } } } diff --git a/BlazorTp1/Pages/Edit.razor b/BlazorTp1/Pages/Edit.razor index c18f772..28c1f8f 100644 --- a/BlazorTp1/Pages/Edit.razor +++ b/BlazorTp1/Pages/Edit.razor @@ -77,6 +77,12 @@

+

+ +

diff --git a/BlazorTp1/Pages/Edit.razor.cs b/BlazorTp1/Pages/Edit.razor.cs index cdf97ab..1267de9 100644 --- a/BlazorTp1/Pages/Edit.razor.cs +++ b/BlazorTp1/Pages/Edit.razor.cs @@ -44,11 +44,6 @@ namespace BlazorTp1.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/BlazorTp1/Pages/List.razor b/BlazorTp1/Pages/List.razor index c4fef1e..bde242a 100644 --- a/BlazorTp1/Pages/List.razor +++ b/BlazorTp1/Pages/List.razor @@ -19,9 +19,9 @@ - @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) + @if (!string.IsNullOrWhiteSpace(context.ImageBase64)) { - @context.DisplayName + @context.DisplayName } else { diff --git a/BlazorTp1/Pages/_Layout.cshtml b/BlazorTp1/Pages/_Layout.cshtml index 10372ee..a2962dc 100644 --- a/BlazorTp1/Pages/_Layout.cshtml +++ b/BlazorTp1/Pages/_Layout.cshtml @@ -29,15 +29,13 @@
+ + - - - - - + diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index 97cb8f6..a78b722 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -4,6 +4,7 @@ using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; using BlazorTp1.Data; +using BlazorTp1.Services; using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.Options; using System.Globalization; @@ -28,6 +29,8 @@ builder.Services.AddBlazoredLocalStorage(); builder.Services.AddScoped(); +builder.Services.AddScoped(); + // Add the controller of the app builder.Services.AddControllers(); diff --git a/BlazorTp1/Services/DataApiService.cs b/BlazorTp1/Services/DataApiService.cs new file mode 100644 index 0000000..661478e --- /dev/null +++ b/BlazorTp1/Services/DataApiService.cs @@ -0,0 +1,58 @@ +using BlazorTp1.Factories; +using BlazorTp1.Components; +using BlazorTp1.Models; +namespace BlazorTp1.Services +{ + public class DataApiService : IDataService + { + private readonly HttpClient _http; + + public DataApiService( + HttpClient http) + { + _http = http; + } + + public async Task Add(ItemModel model) + { + // Get the item + var item = ItemFactory.Create(model); + + // Save the data + await _http.PostAsJsonAsync("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/", item); + } + + public async Task Count() + { + return await _http.GetFromJsonAsync("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/count"); + } + + public async Task> 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 GetById(int id) + { + return await _http.GetFromJsonAsync($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/{id}"); + } + + public async Task Update(int id, ItemModel model) + { + // Get the item + var item = ItemFactory.Create(model); + + await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item); + } + + public async Task Delete(int id) + { + await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}"); + } + + 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/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 9154eb7..1dca040 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -34,21 +34,6 @@ public class DataLocalService : IDataService // 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); } @@ -117,32 +102,6 @@ public class DataLocalService : IDataService 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); @@ -161,15 +120,6 @@ public class DataLocalService : IDataService // 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); } From cdf81fa9d346ea1aa467ddc3b2aa75c01f4a3f6e Mon Sep 17 00:00:00 2001 From: Marc CHEVALDONNE Date: Fri, 18 Nov 2022 19:02:20 +0100 Subject: [PATCH 16/18] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'BlazorTp1/Co?= =?UTF-8?q?mponents/Crafting.razor'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlazorTp1/Components/Crafting.razor | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BlazorTp1/Components/Crafting.razor b/BlazorTp1/Components/Crafting.razor index 0aacb92..97b8a76 100644 --- a/BlazorTp1/Components/Crafting.razor +++ b/BlazorTp1/Components/Crafting.razor @@ -1,6 +1,4 @@ -

Crafting

- - +
From 97aa0b54857d4d30f5d1241a6ec762a1982894fe Mon Sep 17 00:00:00 2001 From: Marc CHEVALDONNE Date: Fri, 18 Nov 2022 19:03:47 +0100 Subject: [PATCH 17/18] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'BlazorTp1/Co?= =?UTF-8?q?mponents/CraftingItem.razor'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BlazorTp1/Components/CraftingItem.razor | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BlazorTp1/Components/CraftingItem.razor b/BlazorTp1/Components/CraftingItem.razor index b9c9fd3..3e488b6 100644 --- a/BlazorTp1/Components/CraftingItem.razor +++ b/BlazorTp1/Components/CraftingItem.razor @@ -1,6 +1,4 @@ -

CraftingItem

- -
Date: Tue, 22 Nov 2022 08:47:25 +0100 Subject: [PATCH 18/18] appsettings.json added --- BlazorTp1/Program.cs | 2 ++ BlazorTp1/wwwroot/appsettings.json | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 BlazorTp1/wwwroot/appsettings.json diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs index a78b722..a52bc75 100644 --- a/BlazorTp1/Program.cs +++ b/BlazorTp1/Program.cs @@ -37,6 +37,8 @@ builder.Services.AddControllers(); // Add the localization to the app and specify the resources path builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); +builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); + // Configure the localtization builder.Services.Configure(options => { diff --git a/BlazorTp1/wwwroot/appsettings.json b/BlazorTp1/wwwroot/appsettings.json new file mode 100644 index 0000000..ef6372d --- /dev/null +++ b/BlazorTp1/wwwroot/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Trace", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} \ No newline at end of file