From 83c67004b9bdee018ee24c787cfbe143e6c19a72 Mon Sep 17 00:00:00 2001 From: Theo RENAUD Date: Wed, 16 Nov 2022 11:49:40 +0100 Subject: [PATCH] fini jusque fin de globalisation --- BlazorApp1/App.razor | 4 +- BlazorApp1/BlazorApp1.csproj | 2 + BlazorApp1/Controllers/CultureController.cs | 35 ++++ BlazorApp1/Factories/ItemFactory.cs | 48 +++++ BlazorApp1/Modals/DeleteConfirmation.razor | 10 ++ BlazorApp1/Modals/DeleteConfirmation.razor.cs | 37 ++++ BlazorApp1/Pages/Add.razor.cs | 59 ++----- BlazorApp1/Pages/Counter.razor | 2 +- BlazorApp1/Pages/Edit.razor | 83 +++++++++ BlazorApp1/Pages/Edit.razor.cs | 109 ++++++++++++ BlazorApp1/Pages/Index.razor | 6 +- BlazorApp1/Pages/List.razor | 9 +- BlazorApp1/Pages/_Layout.cshtml | 3 +- BlazorApp1/Pages/list.razor.cs | 54 +++--- BlazorApp1/Program.cs | 39 ++++- BlazorApp1/Ressources/Pages.List.fr-FR.resx | 123 +++++++++++++ BlazorApp1/Ressources/Pages.List.resx | 123 +++++++++++++ BlazorApp1/Services/DataLocalService.cs | 164 ++++++++++++++++++ BlazorApp1/Services/IDataServicee.cs | 14 ++ BlazorApp1/Shared/CultureSelector.razor | 43 +++++ BlazorApp1/Shared/MainLayout.razor | 3 + BlazorApp1/_Imports.razor | 4 +- BlazorApp1/wwwroot/images/ma binocle.png | Bin 0 -> 4737 bytes 23 files changed, 891 insertions(+), 83 deletions(-) create mode 100644 BlazorApp1/Controllers/CultureController.cs create mode 100644 BlazorApp1/Factories/ItemFactory.cs create mode 100644 BlazorApp1/Modals/DeleteConfirmation.razor create mode 100644 BlazorApp1/Modals/DeleteConfirmation.razor.cs create mode 100644 BlazorApp1/Pages/Edit.razor create mode 100644 BlazorApp1/Pages/Edit.razor.cs create mode 100644 BlazorApp1/Ressources/Pages.List.fr-FR.resx create mode 100644 BlazorApp1/Ressources/Pages.List.resx create mode 100644 BlazorApp1/Services/DataLocalService.cs create mode 100644 BlazorApp1/Services/IDataServicee.cs create mode 100644 BlazorApp1/Shared/CultureSelector.razor create mode 100644 BlazorApp1/wwwroot/images/ma binocle.png diff --git a/BlazorApp1/App.razor b/BlazorApp1/App.razor index 6fd3ed1..5a38683 100644 --- a/BlazorApp1/App.razor +++ b/BlazorApp1/App.razor @@ -1,4 +1,5 @@ - + + @@ -10,3 +11,4 @@ + \ No newline at end of file diff --git a/BlazorApp1/BlazorApp1.csproj b/BlazorApp1/BlazorApp1.csproj index c0eacf6..9354de1 100644 --- a/BlazorApp1/BlazorApp1.csproj +++ b/BlazorApp1/BlazorApp1.csproj @@ -8,9 +8,11 @@ + + diff --git a/BlazorApp1/Controllers/CultureController.cs b/BlazorApp1/Controllers/CultureController.cs new file mode 100644 index 0000000..0b60190 --- /dev/null +++ b/BlazorApp1/Controllers/CultureController.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + +namespace BlazorApp1.Controllers +{ + + /// + /// 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/BlazorApp1/Factories/ItemFactory.cs b/BlazorApp1/Factories/ItemFactory.cs new file mode 100644 index 0000000..d9cb4e1 --- /dev/null +++ b/BlazorApp1/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using BlazorApp1.Models; + +namespace BlazorApp1.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/BlazorApp1/Modals/DeleteConfirmation.razor b/BlazorApp1/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..a1b43d6 --- /dev/null +++ b/BlazorApp1/Modals/DeleteConfirmation.razor @@ -0,0 +1,10 @@ +
+ +

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

+ + + + +
diff --git a/BlazorApp1/Modals/DeleteConfirmation.razor.cs b/BlazorApp1/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..a3e9a7c --- /dev/null +++ b/BlazorApp1/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,37 @@ +using BlazorApp1.Models; +using Blazored.Modal; +using Blazored.Modal.Services; +using Microsoft.AspNetCore.Components; + +namespace BlazorApp1.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/BlazorApp1/Pages/Add.razor.cs b/BlazorApp1/Pages/Add.razor.cs index 68c51af..6ba34ad 100644 --- a/BlazorApp1/Pages/Add.razor.cs +++ b/BlazorApp1/Pages/Add.razor.cs @@ -7,25 +7,11 @@ namespace BlazorApp1.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,21 @@ namespace BlazorApp1.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"); + /// + /// 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" }; - // Check if the folder "images" exist - if (!imagePathInfo.Exists) - { - imagePathInfo.Create(); - } + [Inject] + public IDataService DataService { get; set; } - // Determine the image name - var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png"); + [Inject] + public NavigationManager NavigationManager { get; set; } - // Write the file content - await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent); + private async void HandleValidSubmit() + { + await DataService.Add(itemModel); - // Save the data - await LocalStorage.SetItemAsync("data", currentData); NavigationManager.NavigateTo("list"); } diff --git a/BlazorApp1/Pages/Counter.razor b/BlazorApp1/Pages/Counter.razor index ef23cb3..54d3a80 100644 --- a/BlazorApp1/Pages/Counter.razor +++ b/BlazorApp1/Pages/Counter.razor @@ -13,6 +13,6 @@ private void IncrementCount() { - currentCount++; + currentCount=currentCount+8000; } } diff --git a/BlazorApp1/Pages/Edit.razor b/BlazorApp1/Pages/Edit.razor new file mode 100644 index 0000000..c00eb38 --- /dev/null +++ b/BlazorApp1/Pages/Edit.razor @@ -0,0 +1,83 @@ +@page "/edit/{Id:int}" +@using BlazorApp1.Models + +

Edit

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/BlazorApp1/Pages/Edit.razor.cs b/BlazorApp1/Pages/Edit.razor.cs new file mode 100644 index 0000000..a393532 --- /dev/null +++ b/BlazorApp1/Pages/Edit.razor.cs @@ -0,0 +1,109 @@ +using BlazorApp1.Factories; +using BlazorApp1.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; + +namespace BlazorApp1.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/BlazorApp1/Pages/Index.razor b/BlazorApp1/Pages/Index.razor index 6085c4a..381542a 100644 --- a/BlazorApp1/Pages/Index.razor +++ b/BlazorApp1/Pages/Index.razor @@ -1,9 +1,11 @@ @page "/" +@using System.Globalization Index

Hello, world!

- Welcome to your new app. - +

+ CurrentCulture: @CultureInfo.CurrentCulture +

diff --git a/BlazorApp1/Pages/List.razor b/BlazorApp1/Pages/List.razor index 5ec4d95..bd9974b 100644 --- a/BlazorApp1/Pages/List.razor +++ b/BlazorApp1/Pages/List.razor @@ -1,10 +1,11 @@ @page "/list" @using BlazorApp1.Models +

@Localizer["Title"]

List

- + Ajouter
@@ -43,4 +44,10 @@ + + + Editer + + + \ No newline at end of file diff --git a/BlazorApp1/Pages/_Layout.cshtml b/BlazorApp1/Pages/_Layout.cshtml index 108ef35..fa86d76 100644 --- a/BlazorApp1/Pages/_Layout.cshtml +++ b/BlazorApp1/Pages/_Layout.cshtml @@ -12,6 +12,7 @@ + @RenderBody() @@ -28,7 +29,7 @@ - + diff --git a/BlazorApp1/Pages/list.razor.cs b/BlazorApp1/Pages/list.razor.cs index 9d9a230..d928e07 100644 --- a/BlazorApp1/Pages/list.razor.cs +++ b/BlazorApp1/Pages/list.razor.cs @@ -1,63 +1,65 @@ -using BlazorApp1.Models; +using BlazorApp1.Modals; +using BlazorApp1.Models; using Blazored.LocalStorage; +using Blazored.Modal; +using Blazored.Modal.Services; using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; namespace BlazorApp1.Pages { public partial class List { + [Inject] + public IStringLocalizer Localizer { get; set; } private List items; private int totalItem; [Inject] - public HttpClient Http { get; set; } + public NavigationManager NavigationManager { get; set; } - [Inject] - public ILocalStorageService LocalStorage { get; set; } + [CascadingParameter] + public IModalService Modal { get; set; } [Inject] - public IWebHostEnvironment WebHostEnvironment { get; set; } + public IDataService DataService { get; set; } [Inject] - public NavigationManager NavigationManager { get; set; } + public IWebHostEnvironment WebHostEnvironment { get; set; } - protected override async Task OnAfterRenderAsync(bool firstRender) + private async Task OnReadData(DataGridReadDataEventArgs e) { - // Do not treat this action if is not the first render - if (!firstRender) + if (e.CancellationToken.IsCancellationRequested) { return; } - var currentData = await LocalStorage.GetItemAsync("data"); - - // Check if data exist in the local storage - if (currentData == null) + if (!e.CancellationToken.IsCancellationRequested) { - // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) - var originalData = Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json").Result; - await LocalStorage.SetItemAsync("data", originalData); + items = await DataService.List(e.Page, e.PageSize); + totalItem = await DataService.Count(); } } - private async Task OnReadData(DataGridReadDataEventArgs e) + private async void OnDelete(int id) { - if (e.CancellationToken.IsCancellationRequested) + 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; } - // 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 LocalStorage.GetItemAsync("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + await DataService.Delete(id); - if (!e.CancellationToken.IsCancellationRequested) - { - totalItem = (await LocalStorage.GetItemAsync>("data")).Count; - items = new List(response); // an actual data for the current page - } + // Reload the page + NavigationManager.NavigateTo("list", true); } } } diff --git a/BlazorApp1/Program.cs b/BlazorApp1/Program.cs index 7f683e4..7f5c9d2 100644 --- a/BlazorApp1/Program.cs +++ b/BlazorApp1/Program.cs @@ -1,10 +1,12 @@ using BlazorApp1.Data; using Blazored.LocalStorage; +using Blazored.Modal; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; -using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Localization; +using Microsoft.Extensions.Options; +using System.Globalization; var builder = WebApplication.CreateBuilder(args); @@ -18,6 +20,25 @@ builder.Services .AddBootstrapProviders() .AddFontAwesomeIcons(); builder.Services.AddBlazoredLocalStorage(); +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(); @@ -35,6 +56,20 @@ app.UseStaticFiles(); app.UseRouting(); +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/BlazorApp1/Ressources/Pages.List.fr-FR.resx b/BlazorApp1/Ressources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..65261b3 --- /dev/null +++ b/BlazorApp1/Ressources/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 + + \ No newline at end of file diff --git a/BlazorApp1/Ressources/Pages.List.resx b/BlazorApp1/Ressources/Pages.List.resx new file mode 100644 index 0000000..9d42237 --- /dev/null +++ b/BlazorApp1/Ressources/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 + + + List + + \ No newline at end of file diff --git a/BlazorApp1/Services/DataLocalService.cs b/BlazorApp1/Services/DataLocalService.cs new file mode 100644 index 0000000..01833b0 --- /dev/null +++ b/BlazorApp1/Services/DataLocalService.cs @@ -0,0 +1,164 @@ +using BlazorApp1.Factories; +using BlazorApp1.Models; +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; + +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(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); + } + + 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(); + } + + +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); + + ItemFactory.Update(item, model); + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + + + } + public async Task Delete(int id) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("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/BlazorApp1/Services/IDataServicee.cs b/BlazorApp1/Services/IDataServicee.cs new file mode 100644 index 0000000..4b94277 --- /dev/null +++ b/BlazorApp1/Services/IDataServicee.cs @@ -0,0 +1,14 @@ +using BlazorApp1.Models; + +public interface IDataService +{ + Task Add(ItemModel model); + + Task Count(); + + Task> List(int currentPage, int pageSize); + Task GetById(int id); + + Task Update(int id, ItemModel model); + Task Delete(int id); +} \ No newline at end of file diff --git a/BlazorApp1/Shared/CultureSelector.razor b/BlazorApp1/Shared/CultureSelector.razor new file mode 100644 index 0000000..653175d --- /dev/null +++ b/BlazorApp1/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); + } + } +} \ No newline at end of file diff --git a/BlazorApp1/Shared/MainLayout.razor b/BlazorApp1/Shared/MainLayout.razor index 62f8c7a..7d9071c 100644 --- a/BlazorApp1/Shared/MainLayout.razor +++ b/BlazorApp1/Shared/MainLayout.razor @@ -10,6 +10,9 @@
About +
+ +
diff --git a/BlazorApp1/_Imports.razor b/BlazorApp1/_Imports.razor index 6ccf59e..1719665 100644 --- a/BlazorApp1/_Imports.razor +++ b/BlazorApp1/_Imports.razor @@ -8,4 +8,6 @@ @using Microsoft.JSInterop @using BlazorApp1 @using BlazorApp1.Shared -@using Blazorise.DataGrid \ No newline at end of file +@using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services \ No newline at end of file diff --git a/BlazorApp1/wwwroot/images/ma binocle.png b/BlazorApp1/wwwroot/images/ma binocle.png new file mode 100644 index 0000000000000000000000000000000000000000..7402cee08f77704c222beac8242cc875c4e773f1 GIT binary patch literal 4737 zcmV-{5`OK8P))2XrhyVZ6Zi2Q)NC>0a z-o^LUJL{fxBxIAFJ!eNzYSgGvqehJyHEPtTQKQE107Ciy<<9{%?>ocA>+*RvdwzZ$ zEIQME^Z!C!Yq}WCKYakOZF}r8jBT6!b@#PicKWxTUr`{&f9P&8+EP+n&(TyKTGtx` z1Yggc_V*Ki3eDmE-3H&n0^0t8VBB-T+bpMljK22bi_TkP6c8zerfI6Wu6A8`A9Zd$ z>nuR@e@bgO0qBb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