diff --git a/.idea/.idea.TP Blazor/.idea/indexLayout.xml b/.idea/.idea.TP Blazor/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.TP Blazor/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.TP Blazor/.idea/vcs.xml b/.idea/.idea.TP Blazor/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/.idea.TP Blazor/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..522c4a2 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/TP Blazor/TP Blazor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/TP Blazor/TP Blazor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/TP Blazor/TP Blazor.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/TP Blazor.sln b/TP Blazor.sln index f2dd4d1..0d42231 100644 --- a/TP Blazor.sln +++ b/TP Blazor.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33205.214 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TP Blazor", "TP Blazor\TP Blazor.csproj", "{DC7DF5A3-75B4-4044-B267-88F4E7705712}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TP Blazor", "TP Blazor\TP Blazor.csproj", "{DC7DF5A3-75B4-4044-B267-88F4E7705712}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/TP Blazor/App.razor b/TP Blazor/App.razor index 6fd3ed1..09ed54d 100644 --- a/TP Blazor/App.razor +++ b/TP Blazor/App.razor @@ -1,12 +1,14 @@ - - - - - - - Not found - -

Sorry, there's nothing at this address.

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

Sorry, there's nothing at this address.

+
+
+
+
\ No newline at end of file diff --git a/TP Blazor/Components/Card.razor b/TP Blazor/Components/Card.razor new file mode 100644 index 0000000..e15f762 --- /dev/null +++ b/TP Blazor/Components/Card.razor @@ -0,0 +1,10 @@ +@using TP_Blazor.Models + +

Card

+ +@typeparam TItem +
+ @CardHeader(Item) + @CardBody(Item) + @CardFooter +
diff --git a/TP Blazor/Components/Card.razor.cs b/TP Blazor/Components/Card.razor.cs new file mode 100644 index 0000000..cd9a3a5 --- /dev/null +++ b/TP Blazor/Components/Card.razor.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Components; + +namespace TP_Blazor.Components +{ + public partial class Card + { + [Parameter] + public RenderFragment CardBody { get; set; } + + [Parameter] + public RenderFragment CardHeader { get; set; } + + [Parameter] + public RenderFragment CardFooter { get; set; } + } +} diff --git a/TP Blazor/Controllers/CultureController.cs b/TP Blazor/Controllers/CultureController.cs new file mode 100644 index 0000000..60e6389 --- /dev/null +++ b/TP Blazor/Controllers/CultureController.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; + +namespace TP_Blazor.Controllers; + +[Route("[controller]/[action]")] +public class CultureController:Controller +{ + public IActionResult SetCulture(string culture, string returnUrl) + { + if(culture != null) + { + this.HttpContext.Response.Cookies.Append( + CookieRequestCultureProvider.DefaultCookieName, + CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture))); + } + return this.LocalRedirect(returnUrl); + } +} \ No newline at end of file diff --git a/TP Blazor/Factories/ItemFactory.cs b/TP Blazor/Factories/ItemFactory.cs new file mode 100644 index 0000000..c716645 --- /dev/null +++ b/TP Blazor/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using TP_Blazor.Models; + +namespace TP_Blazor.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; + } + +} \ No newline at end of file diff --git a/TP Blazor/Modals/DeleteConfirmation.razor b/TP Blazor/Modals/DeleteConfirmation.razor new file mode 100644 index 0000000..561147f --- /dev/null +++ b/TP Blazor/Modals/DeleteConfirmation.razor @@ -0,0 +1,10 @@ +
+ +

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

+ + + + +
\ No newline at end of file diff --git a/TP Blazor/Modals/DeleteConfirmation.razor.cs b/TP Blazor/Modals/DeleteConfirmation.razor.cs new file mode 100644 index 0000000..b46bc18 --- /dev/null +++ b/TP Blazor/Modals/DeleteConfirmation.razor.cs @@ -0,0 +1,37 @@ +using Blazored.Modal; +using Blazored.Modal.Services; +using Microsoft.AspNetCore.Components; +using TP_Blazor.Models; +using TP_Blazor.Services; + +namespace TP_Blazor.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(); + } +} \ No newline at end of file diff --git a/TP Blazor/Models/ItemModel.cs b/TP Blazor/Models/ItemModel.cs new file mode 100644 index 0000000..ef96511 --- /dev/null +++ b/TP Blazor/Models/ItemModel.cs @@ -0,0 +1,38 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace TP_Blazor.Models; + +public class ItemModel +{ + public int Id { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "Le nom affiché ne doit pas dépasser 50 caractères.")] + public string DisplayName { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "Le nom ne doit pas dépasser 50 caractères.")] + [RegularExpression(@"^[a-z''-'\s]{1,40}$", ErrorMessage = "Seulement les caractères en minuscule sont acceptées.")] + public string Name { get; set; } + + [Required] + [Range(1, 64)] + public int StackSize { get; set; } + + [Required] + [Range(1, 125)] + public int MaxDurability { get; set; } + + public List EnchantCategories { get; set; } + + public List RepairWith { get; set; } + + [Required] + [Range(typeof(bool), "true", "true", ErrorMessage = "Vous devez accepter les conditions.")] + public bool AcceptCondition { get; set; } + + [Required(ErrorMessage = "L'image de l'item est obligatoire !")] + public byte[] ImageContent { get; set; } +} + diff --git a/TP Blazor/Pages/Add.razor b/TP Blazor/Pages/Add.razor new file mode 100644 index 0000000..d696ca7 --- /dev/null +++ b/TP Blazor/Pages/Add.razor @@ -0,0 +1,72 @@ +@page "/add" + +

Add

+ + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

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

+

+ Repair with: +

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

+

+ +

+

+ +

+ + + +
+ + + diff --git a/TP Blazor/Pages/Add.razor.cs b/TP Blazor/Pages/Add.razor.cs new file mode 100644 index 0000000..8f7cf02 --- /dev/null +++ b/TP Blazor/Pages/Add.razor.cs @@ -0,0 +1,116 @@ +using System; +using System.Security.Cryptography; +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using TP_Blazor.Models; +using TP_Blazor.Services; + +namespace TP_Blazor.Pages; + +public partial class Add +{ + [Inject] + ILocalStorageService LocalStorage { get; set; } + + [Inject] + IWebHostEnvironment WebHostEnvironment { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" }; + + [Inject] + private IDataService DataService{ get; set; } + + + + + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + private async void OnHandleValidSubmit() + { + /*var currentData = await LocalStorage.GetItemAsync>("data"); + itemModel.Id = currentData.Max(s => s.Id) + 1; + 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 + }); + + var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images"); + + if (!imagePathInfo.Exists) + { + imagePathInfo.Create(); + } + + var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png"); + await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent); + + await LocalStorage.SetItemAsync("data", currentData); + NavigationManager.NavigateTo("list");*/ + await DataService.Add(itemModel); + NavigationManager.NavigateTo("list"); + } + + private async Task LoadImage(InputFileChangeEventArgs e) + { + + 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/TP Blazor/Pages/Edit.razor b/TP Blazor/Pages/Edit.razor new file mode 100644 index 0000000..9aecaa2 --- /dev/null +++ b/TP Blazor/Pages/Edit.razor @@ -0,0 +1,81 @@ +@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/TP Blazor/Pages/Edit.razor.cs b/TP Blazor/Pages/Edit.razor.cs new file mode 100644 index 0000000..99ee15c --- /dev/null +++ b/TP Blazor/Pages/Edit.razor.cs @@ -0,0 +1,106 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using TP_Blazor.Factories; +using TP_Blazor.Models; +using TP_Blazor.Services; + +namespace TP_Blazor.Pages; + +public partial class Edit +{ + [Parameter] + public int Id { get; set; } + + 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); + } + } +} \ No newline at end of file diff --git a/TP Blazor/Pages/Index.razor b/TP Blazor/Pages/Index.razor index 6085c4a..d545980 100644 --- a/TP Blazor/Pages/Index.razor +++ b/TP Blazor/Pages/Index.razor @@ -1,9 +1,28 @@ @page "/" +@using System.Globalization +@using TP_Blazor.Components Index

Hello, world!

+

+ CurrentCulture: @CultureInfo.CurrentCulture +

-Welcome to your new app. - - + + +
+ My templated Component Header +
+
+ +
+

Template Component Body

+
+
+ + + +
diff --git a/TP Blazor/Pages/List.razor b/TP Blazor/Pages/List.razor index 94fc595..1656ee0 100644 --- a/TP Blazor/Pages/List.razor +++ b/TP Blazor/Pages/List.razor @@ -1,8 +1,12 @@ @page "/list" @using TP_Blazor.Models -

Liste

+

@Localizer["Title"]

-@*@if (items!=null) { +@*@if (items!=null) + { +
+ + @@ -10,7 +14,9 @@ - + + + @foreach (var item in items) { @@ -23,8 +29,24 @@ } -
Id Display Name Stack SizeEnchant Categories Repair With Created Date
@item.CreatedDate.ToShortDateString()
}*@ + + + + }*@ + +
+ + @Localizer["btnTitle"] + +
+ + + + + @context.DisplayName + + @@ -39,4 +61,10 @@ + + + Editer + + + \ No newline at end of file diff --git a/TP Blazor/Pages/List.razor.cs b/TP Blazor/Pages/List.razor.cs index 58790ce..fc9879b 100644 --- a/TP Blazor/Pages/List.razor.cs +++ b/TP Blazor/Pages/List.razor.cs @@ -2,6 +2,13 @@ using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; using TP_Blazor.Models; +using Blazored; +using Blazored.LocalStorage; +using Blazored.Modal; +using Blazored.Modal.Services; +using TP_Blazor.Modals; +using TP_Blazor.Services; +using Microsoft.Extensions.Localization; namespace TP_Blazor.Pages; @@ -15,31 +22,77 @@ public partial class List private int totalItem; [Inject] - public HttpClient Http { get; set; } + public IStringLocalizer Localizer { get; set; } + [Inject] + public HttpClient HttpClient { get; set; } + + [Inject] + public IDataService DataService { get; set; } + + [CascadingParameter] + public IModalService Modal { get; set; } [Inject] public NavigationManager NavigationManager { get; set; } + [Inject] + IWebHostEnvironment WebHostEnvironment { get; set; } + + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + + private async Task OnReadData(DataGridReadDataEventArgs e) { if (e.CancellationToken.IsCancellationRequested) { return; } - var response = (await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); + //var response = (await HttpClient.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 + //totalItem = (await HttpClient.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(); } } - protected override void OnAfterRender(bool firstRender) + protected override async Task OnAfterRenderAsync(bool firstRender) { - base.OnAfterRender(firstRender); + if (!firstRender) + { + return; + } + + var currentData = await LocalStorage.GetItemAsync("data"); + + if (currentData==null) + { + var originalData = HttpClient.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-data.json").Result; + await LocalStorage.SetItemAsync("data", originalData); + } } + + 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/TP Blazor/Pages/_Host.cshtml b/TP Blazor/Pages/_Host.cshtml index b6b8f8c..cbad39c 100644 --- a/TP Blazor/Pages/_Host.cshtml +++ b/TP Blazor/Pages/_Host.cshtml @@ -16,6 +16,7 @@ + @@ -34,6 +35,7 @@ 🗙 - + + diff --git a/TP Blazor/Program.cs b/TP Blazor/Program.cs index 371def7..de0cb1a 100644 --- a/TP Blazor/Program.cs +++ b/TP Blazor/Program.cs @@ -1,3 +1,4 @@ +using System.Globalization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using TP_Blazor.Data; @@ -6,6 +7,10 @@ using Blazorise.Icons.FontAwesome; using Microsoft.Extensions.DependencyInjection; using Blazorise.Bootstrap; using Blazored.LocalStorage; +using TP_Blazor.Services; +using Blazored.Modal; +using Microsoft.AspNetCore.Localization; +using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); @@ -16,8 +21,18 @@ builder.Services.AddSingleton(); builder.Services.AddHttpClient(); builder.Services.AddBlazorise() .AddBootstrapProviders() - .AddBlazoredLocalStorage() .AddFontAwesomeIcons(); +builder.Services.AddBlazoredLocalStorage(); +builder.Services.AddBlazoredModal(); +builder.Services.AddControllers(); +builder.Services.AddLocalization(opt=>{opt.ResourcesPath= "Resources"; }); +builder.Services.Configure(option => +{ + option.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US")); + option.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; + option.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; +}); +builder.Services.AddScoped(); var app = builder.Build(); @@ -32,6 +47,18 @@ app.UseStaticFiles(); app.UseRouting(); +var options =((IApplicationBuilder)app).ApplicationServices.GetService>(); + +if (options?.Value != null) +{ + app.UseRequestLocalization(options.Value); +} + +app.UseEndpoints(endpoints => +{ + endpoints.MapControllers(); +}); + app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); diff --git a/TP Blazor/Resources/Pages.List.Designer.cs b/TP Blazor/Resources/Pages.List.Designer.cs new file mode 100644 index 0000000..33bac08 --- /dev/null +++ b/TP Blazor/Resources/Pages.List.Designer.cs @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// Ce code a été généré par un outil. +// Version du runtime :4.0.30319.42000 +// +// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si +// le code est régénéré. +// +//------------------------------------------------------------------------------ + +namespace TP_Blazor.Resources { + using System; + + + /// + /// Une classe de ressource fortement typée destinée, entre autres, à la consultation des chaînes localisées. + /// + // Cette classe a été générée automatiquement par la classe StronglyTypedResourceBuilder + // à l'aide d'un outil, tel que ResGen ou Visual Studio. + // Pour ajouter ou supprimer un membre, modifiez votre fichier .ResX, puis réexécutez ResGen + // avec l'option /str ou régénérez votre projet VS. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class Pages_List { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Pages_List() { + } + + /// + /// Retourne l'instance ResourceManager mise en cache utilisée par cette classe. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TP_Blazor.Resources.Pages.List", typeof(Pages_List).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Remplace la propriété CurrentUICulture du thread actuel pour toutes + /// les recherches de ressources à l'aide de cette classe de ressource fortement typée. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Recherche une chaîne localisée semblable à My Title. + /// + public static string btnTitle { + get { + return ResourceManager.GetString("btnTitle", resourceCulture); + } + } + } +} diff --git a/TP Blazor/Resources/Pages.List.resx b/TP Blazor/Resources/Pages.List.resx new file mode 100644 index 0000000..08603f9 --- /dev/null +++ b/TP Blazor/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 + + + My Title + + \ No newline at end of file diff --git a/TP Blazor/Ressources/Pages.List.fr-FR.Designer.cs b/TP Blazor/Ressources/Pages.List.fr-FR.Designer.cs new file mode 100644 index 0000000..103772e --- /dev/null +++ b/TP Blazor/Ressources/Pages.List.fr-FR.Designer.cs @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace TP_Blazor.Ressources { + using System; + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [System.Diagnostics.DebuggerNonUserCodeAttribute()] + [System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Pages_List_fr_FR { + + private static System.Resources.ResourceManager resourceMan; + + private static System.Globalization.CultureInfo resourceCulture; + + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Pages_List_fr_FR() { + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Resources.ResourceManager ResourceManager { + get { + if (object.Equals(null, resourceMan)) { + System.Resources.ResourceManager temp = new System.Resources.ResourceManager("TP_Blazor.Ressources.Pages_List_fr_FR", typeof(Pages_List_fr_FR).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] + internal static System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/TP Blazor/Ressources/Pages.List.fr-FR.resx b/TP Blazor/Ressources/Pages.List.fr-FR.resx new file mode 100644 index 0000000..f9387df --- /dev/null +++ b/TP Blazor/Ressources/Pages.List.fr-FR.resx @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + à propos + + + Ajouter + + + Image + + + Nom + + + Taille + + + Acceuil + + + List + + + Compteur + + + Liste d'utilisateur + + \ No newline at end of file diff --git a/TP Blazor/Services/DataLocalService.cs b/TP Blazor/Services/DataLocalService.cs new file mode 100644 index 0000000..f8ebdf0 --- /dev/null +++ b/TP Blazor/Services/DataLocalService.cs @@ -0,0 +1,134 @@ +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using TP_Blazor.Factories; +using TP_Blazor.Models; + +namespace TP_Blazor.Services; + +public class DataLocalService:IDataService +{ + private readonly HttpClient _httpClient; + private readonly ILocalStorageService _localStorageService; + private readonly NavigationManager _navigationManager; + private readonly IWebHostEnvironment _webHostEnvironment; + + public DataLocalService(HttpClient httpClient, ILocalStorageService localStorageService, NavigationManager navigationManager, IWebHostEnvironment webHostEnvironment) + { + _httpClient = httpClient; + _localStorageService = localStorageService; + _navigationManager = navigationManager; + _webHostEnvironment = webHostEnvironment; + } + + public async Task Add(ItemModel item) + { + var currentItems = await _localStorageService.GetItemAsync>("data"); + item.Id = currentItems.Max(s=>s.Id)+1; + // currentItems.Add(new Item + // { + // Id = item.Id, + // Name = item.Name, + // DisplayName = item.DisplayName, + // RepairWith = item.RepairWith, + // EnchantCategories = item.EnchantCategories, + // MaxDurability = item.MaxDurability, + // StackSize = item.StackSize, + // CreatedDate = DateTime.Now + // }); + currentItems.Add(ItemFactory.Create(item)); + + var imagePathsInfo = new DirectoryInfo(Path.Combine($"{_webHostEnvironment.ContentRootPath}/images")); + if (!imagePathsInfo.Exists) + { + imagePathsInfo.Create(); + } + + var fileName = new FileInfo($"{imagePathsInfo}/{item.Name}.png"); + await File.WriteAllBytesAsync(fileName.FullName, item.ImageContent); + await _localStorageService.SetItemAsync("data", currentItems); + } + + public async Task Count() + { + var currentItems = _localStorageService.GetItemAsync("data"); + if (currentItems== null) + { + var originalItems = _httpClient.GetFromJsonAsync($"f{_navigationManager.BaseUri}ake-data.json"); + await _localStorageService.SetItemAsync("data", originalItems); + } + return (await _localStorageService.GetItemAsync("data")).Length; + } + + public async Task> List(int page, int pageSize) + { + var currentItems = _localStorageService.GetItemAsync("data"); + if (currentItems == null) + { + var originalItems = _httpClient.GetFromJsonAsync($"f{_navigationManager.BaseUri}ake-data.json"); + _localStorageService.SetItemAsync("data", originalItems); + } + return (await _localStorageService.GetItemAsync("data")).Skip((page-1)*pageSize).Take(pageSize).ToList(); + } + + public async Task GetById(int id) + { + var currentItems =await _localStorageService.GetItemAsync>("data"); + var item = currentItems.FirstOrDefault(s => s.Id == id); + if (item == null) + { + throw new Exception($"Item with id {id} not found"); + } + return item; + } + + public async Task Update(int id, ItemModel item) + { + var currentItems = await _localStorageService.GetItemAsync>("data"); + var itemToUpdate = currentItems.FirstOrDefault(s => s.Id == id); + + if (itemToUpdate == null) + { + throw new Exception($"Item with id {id} not found"); + } + var imagePathsInfo = new DirectoryInfo($"{_webHostEnvironment.ContentRootPath}/images"); + if (!imagePathsInfo.Exists) + { + imagePathsInfo.Create(); + } + + if (itemToUpdate.Name != item.Name) + { + var oldFileName = new FileInfo($"{imagePathsInfo}/{itemToUpdate.Name}.png"); + if (oldFileName.Exists) + { + oldFileName.Delete(); + } + } + + var fileName = new FileInfo($"{imagePathsInfo}/{item.Name}.png"); + await File.WriteAllBytesAsync(fileName.FullName, item.ImageContent); + // itemToUpdate.Name = item.Name; + // itemToUpdate.DisplayName = item.DisplayName; + // itemToUpdate.RepairWith = item.RepairWith; + // itemToUpdate.EnchantCategories = item.EnchantCategories; + // itemToUpdate.MaxDurability = item.MaxDurability; + // itemToUpdate.StackSize = item.StackSize; + // itemToUpdate.UpdatedDate = DateTime.Now; + ItemFactory.Update(itemToUpdate, item); + await _localStorageService.SetItemAsync("data", currentItems); + } + + public async Task Delete(int id) + { + var currentItems =await _localStorageService.GetItemAsync>("data"); + var itemToDelete = currentItems.FirstOrDefault(s => s.Id == id); + currentItems.Remove(itemToDelete); + var imagePathsInfo = new DirectoryInfo($"{_webHostEnvironment.ContentRootPath}/images"); + var fileName = new FileInfo($"{imagePathsInfo}/{itemToDelete.Name}.png"); + if (fileName.Exists) + { + File.Delete(fileName.FullName); + } + await _localStorageService.SetItemAsync("data", currentItems); + } +} \ No newline at end of file diff --git a/TP Blazor/Services/IDataService.cs b/TP Blazor/Services/IDataService.cs new file mode 100644 index 0000000..c7266f1 --- /dev/null +++ b/TP Blazor/Services/IDataService.cs @@ -0,0 +1,13 @@ +using TP_Blazor.Models; + +namespace TP_Blazor.Services; + +public interface IDataService +{ + public Task Add(ItemModel item); + Task Count(); + Task> List(int page, int pageSize); + Task GetById(int id); + Task Update(int id,ItemModel item); + Task Delete(int id); +} \ No newline at end of file diff --git a/TP Blazor/Shared/CultureSelector.razor b/TP Blazor/Shared/CultureSelector.razor new file mode 100644 index 0000000..90cdeb7 --- /dev/null +++ b/TP Blazor/Shared/CultureSelector.razor @@ -0,0 +1,14 @@ +@using System.Globalization +@inject NavigationManager NavigationManager + +

+ +

\ No newline at end of file diff --git a/TP Blazor/Shared/CultureSelector.razor.cs b/TP Blazor/Shared/CultureSelector.razor.cs new file mode 100644 index 0000000..d491150 --- /dev/null +++ b/TP Blazor/Shared/CultureSelector.razor.cs @@ -0,0 +1,32 @@ +using System.Globalization; + +namespace TP_Blazor.Shared; + +public partial class CultureSelector +{ + 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/TP Blazor/Shared/MainLayout.razor b/TP Blazor/Shared/MainLayout.razor index 8025b70..dc54838 100644 --- a/TP Blazor/Shared/MainLayout.razor +++ b/TP Blazor/Shared/MainLayout.razor @@ -10,6 +10,9 @@
About +
+ +
diff --git a/TP Blazor/TP Blazor.csproj b/TP Blazor/TP Blazor.csproj index 4859a02..cef7a08 100644 --- a/TP Blazor/TP Blazor.csproj +++ b/TP Blazor/TP Blazor.csproj @@ -1,7 +1,7 @@ - + - net7.0 + net6.0 enable enable TP_Blazor @@ -14,14 +14,31 @@ + + + + + + + True + True + Pages.List.resx + + + + + PublicResXFileCodeGenerator + Pages.List.Designer.cs + + diff --git a/TP Blazor/_Imports.razor b/TP Blazor/_Imports.razor index a029dcc..8bd9121 100644 --- a/TP Blazor/_Imports.razor +++ b/TP Blazor/_Imports.razor @@ -9,3 +9,5 @@ @using TP_Blazor @using TP_Blazor.Shared @using Blazorise.DataGrid +@using Blazored.Modal +@using Blazored.Modal.Services diff --git a/TP Blazor/images/default.png b/TP Blazor/images/default.png new file mode 100644 index 0000000..480d066 Binary files /dev/null and b/TP Blazor/images/default.png differ diff --git a/TP Blazor/images/ertgetrfz.png b/TP Blazor/images/ertgetrfz.png new file mode 100644 index 0000000..70abab6 Binary files /dev/null and b/TP Blazor/images/ertgetrfz.png differ diff --git a/TP Blazor/images/scenty.png b/TP Blazor/images/scenty.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/TP Blazor/images/scenty.png differ diff --git a/TP Blazor/images/turnaboutez.png b/TP Blazor/images/turnaboutez.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/TP Blazor/images/turnaboutez.png differ diff --git a/TP Blazor/wwwroot/images/default.png b/TP Blazor/wwwroot/images/default.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/TP Blazor/wwwroot/images/default.png differ