From f13daf29f52b87445d1689462468d89fccf4e67c Mon Sep 17 00:00:00 2001
From: Lucie Bedouret
Date: Tue, 15 Nov 2022 10:00:50 +0100
Subject: [PATCH] CONTINUE : continued the tutorial to Translate the site using
resources (not completed)
---
myBlazorApp/myBlazorApp/App.razor | 19 +--
.../Controllers/CultureController.cs | 32 +++++
.../myBlazorApp/Factories/ItemFactory.cs | 50 ++++++++
.../Modals/DeleteConfirmation.razor | 11 ++
.../Modals/DeleteConfirmation.razor.cs | 40 ++++++
myBlazorApp/myBlazorApp/Pages/Edit.razor | 82 +++++++++++++
myBlazorApp/myBlazorApp/Pages/Edit.razor.cs | 116 ++++++++++++++++++
myBlazorApp/myBlazorApp/Pages/Index.razor | 8 +-
myBlazorApp/myBlazorApp/Pages/List.razor | 6 +
myBlazorApp/myBlazorApp/Pages/List.razor.cs | 28 +++++
myBlazorApp/myBlazorApp/Pages/_Layout.cshtml | 2 +
myBlazorApp/myBlazorApp/Program.cs | 44 ++++++-
.../Resources/Pages.List.Designer.cs | 60 +++++++++
.../myBlazorApp/Resources/Pages.List.resx | 15 +++
.../myBlazorApp/Services/DataLocalService.cs | 105 ++++++++++++++--
.../myBlazorApp/Services/IDataService.cs | 3 +
.../myBlazorApp/Shared/CultureSelector.razor | 44 +++++++
.../myBlazorApp/Shared/MainLayout.razor | 7 +-
myBlazorApp/myBlazorApp/_Imports.razor | 4 +
myBlazorApp/myBlazorApp/myBlazorApp.csproj | 23 ++++
.../wwwroot/images/{chat.png => default.png} | Bin
.../wwwroot/images/harry potter.png | Bin 24192 -> 0 bytes
.../wwwroot/images/hermione granger.png | Bin 3033 -> 0 bytes
.../wwwroot/images/luna lovegood.png | Bin 7338 -> 0 bytes
.../myBlazorApp/wwwroot/images/polar bear.png | Bin 0 -> 105302 bytes
.../wwwroot/images/ron weasley.png | Bin 12046 -> 0 bytes
26 files changed, 672 insertions(+), 27 deletions(-)
create mode 100644 myBlazorApp/myBlazorApp/Controllers/CultureController.cs
create mode 100644 myBlazorApp/myBlazorApp/Factories/ItemFactory.cs
create mode 100644 myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor
create mode 100644 myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor.cs
create mode 100644 myBlazorApp/myBlazorApp/Pages/Edit.razor
create mode 100644 myBlazorApp/myBlazorApp/Pages/Edit.razor.cs
create mode 100644 myBlazorApp/myBlazorApp/Resources/Pages.List.Designer.cs
create mode 100644 myBlazorApp/myBlazorApp/Resources/Pages.List.resx
create mode 100644 myBlazorApp/myBlazorApp/Shared/CultureSelector.razor
rename myBlazorApp/myBlazorApp/wwwroot/images/{chat.png => default.png} (100%)
delete mode 100644 myBlazorApp/myBlazorApp/wwwroot/images/harry potter.png
delete mode 100644 myBlazorApp/myBlazorApp/wwwroot/images/hermione granger.png
delete mode 100644 myBlazorApp/myBlazorApp/wwwroot/images/luna lovegood.png
create mode 100644 myBlazorApp/myBlazorApp/wwwroot/images/polar bear.png
delete mode 100644 myBlazorApp/myBlazorApp/wwwroot/images/ron weasley.png
diff --git a/myBlazorApp/myBlazorApp/App.razor b/myBlazorApp/myBlazorApp/App.razor
index d06cf24..d8441fd 100644
--- a/myBlazorApp/myBlazorApp/App.razor
+++ b/myBlazorApp/myBlazorApp/App.razor
@@ -1,10 +1,11 @@
-
-
-
-
-
- Not found
+
+
+
+
+
+
+ Not found
Sorry, there's nothing at this address.
-
-
-
+
+
+
diff --git a/myBlazorApp/myBlazorApp/Controllers/CultureController.cs b/myBlazorApp/myBlazorApp/Controllers/CultureController.cs
new file mode 100644
index 0000000..d5f71fc
--- /dev/null
+++ b/myBlazorApp/myBlazorApp/Controllers/CultureController.cs
@@ -0,0 +1,32 @@
+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);
+ }
+}
+
diff --git a/myBlazorApp/myBlazorApp/Factories/ItemFactory.cs b/myBlazorApp/myBlazorApp/Factories/ItemFactory.cs
new file mode 100644
index 0000000..11db794
--- /dev/null
+++ b/myBlazorApp/myBlazorApp/Factories/ItemFactory.cs
@@ -0,0 +1,50 @@
+using System;
+using myBlazorApp.Models;
+
+namespace myBlazorApp.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/myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor b/myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor
new file mode 100644
index 0000000..ca93451
--- /dev/null
+++ b/myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor
@@ -0,0 +1,11 @@
+
+
diff --git a/myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor.cs b/myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor.cs
new file mode 100644
index 0000000..8a4f948
--- /dev/null
+++ b/myBlazorApp/myBlazorApp/Modals/DeleteConfirmation.razor.cs
@@ -0,0 +1,40 @@
+using System;
+using Blazored.Modal;
+using Blazored.Modal.Services;
+using Microsoft.AspNetCore.Components;
+using myBlazorApp.Models;
+using myBlazorApp.Services;
+
+namespace myBlazorApp.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/myBlazorApp/myBlazorApp/Pages/Edit.razor b/myBlazorApp/myBlazorApp/Pages/Edit.razor
new file mode 100644
index 0000000..74d3978
--- /dev/null
+++ b/myBlazorApp/myBlazorApp/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)
+ {
+
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/myBlazorApp/myBlazorApp/Pages/Edit.razor.cs b/myBlazorApp/myBlazorApp/Pages/Edit.razor.cs
new file mode 100644
index 0000000..26d94a5
--- /dev/null
+++ b/myBlazorApp/myBlazorApp/Pages/Edit.razor.cs
@@ -0,0 +1,116 @@
+using System;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Forms;
+using myBlazorApp.Factories;
+using myBlazorApp.Models;
+using myBlazorApp.Services;
+
+namespace myBlazorApp.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 = ItemFactory.ToModel(item, 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/myBlazorApp/myBlazorApp/Pages/Index.razor b/myBlazorApp/myBlazorApp/Pages/Index.razor
index cd39618..aa25222 100644
--- a/myBlazorApp/myBlazorApp/Pages/Index.razor
+++ b/myBlazorApp/myBlazorApp/Pages/Index.razor
@@ -1,4 +1,6 @@
-@page "/"
+@using System.Globalization
+
+@page "/"
Index
@@ -8,3 +10,7 @@ Welcome to your new app.
+
+
+ CurrentCulture: @CultureInfo.CurrentCulture
+
\ No newline at end of file
diff --git a/myBlazorApp/myBlazorApp/Pages/List.razor b/myBlazorApp/myBlazorApp/Pages/List.razor
index a68a3ea..21823bc 100644
--- a/myBlazorApp/myBlazorApp/Pages/List.razor
+++ b/myBlazorApp/myBlazorApp/Pages/List.razor
@@ -44,4 +44,10 @@
+
+
+ Editer
+
+
+
\ No newline at end of file
diff --git a/myBlazorApp/myBlazorApp/Pages/List.razor.cs b/myBlazorApp/myBlazorApp/Pages/List.razor.cs
index 5ba93d7..79530a7 100644
--- a/myBlazorApp/myBlazorApp/Pages/List.razor.cs
+++ b/myBlazorApp/myBlazorApp/Pages/List.razor.cs
@@ -1,7 +1,10 @@
using System;
using Blazored.LocalStorage;
+using Blazored.Modal;
+using Blazored.Modal.Services;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
+using myBlazorApp.Modals;
using myBlazorApp.Models;
using myBlazorApp.Services;
@@ -19,6 +22,12 @@ namespace myBlazorApp.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)
@@ -32,6 +41,25 @@ namespace myBlazorApp.Pages
totalItem = await DataService.Count();
}
}
+
+ 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);
+ }
}
}
diff --git a/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml b/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml
index bb515fd..7e0555a 100644
--- a/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml
+++ b/myBlazorApp/myBlazorApp/Pages/_Layout.cshtml
@@ -33,6 +33,8 @@
+
+