From 2845856f514fc9d297085fd40efb42539dcc1c5c Mon Sep 17 00:00:00 2001 From: "patrick.brugiere" Date: Sat, 23 Dec 2023 23:02:45 +0100 Subject: [PATCH] debut de travail sur la modification des user --- .../adminBlazor/Services/DataLocalService.cs | 84 +++++++++++++++++++ .../adminBlazor/Services/IDataService.cs | 19 +++++ .../adminBlazor/Shared/NavBar.razor | 1 - 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 Project/adminBlazor/adminBlazor/Services/DataLocalService.cs create mode 100644 Project/adminBlazor/adminBlazor/Services/IDataService.cs diff --git a/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs b/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs new file mode 100644 index 0000000..340a7a0 --- /dev/null +++ b/Project/adminBlazor/adminBlazor/Services/DataLocalService.cs @@ -0,0 +1,84 @@ +using adminBlazor.Models; +using Blazored.LocalStorage; +using Blazorise.Extensions; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Hosting; + +namespace adminBlazor.Services +{ + public class DataLocalService + { + [Inject] + public required ILocalStorageService LocalStorage { get; set; } + + [Inject] + public required IWebHostEnvironment WebHostEnvironment { get; set; } + public async Task GetById(int id) + { + // Get the current data + var currentData = await LocalStorage.GetItemAsync>("data"); + + var user = currentData.FirstOrDefault(w => w.Id == id); + + if (user == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + return user; + } + + public async Task Update(int id, User model) + { + // Get the current data + var currentData = await LocalStorage.GetItemAsync>("data"); + + var user = currentData.FirstOrDefault(w => w.Id == id); + + if (user == 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 (user.Name != model.Name) + { + var oldFileName = new FileInfo($"{imagePathInfo}/{user.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.Image); + + // Modify the content of the item + user.Nickname = model.Nickname; + user.Name = model.Name; + user.Surname = model.Surname; + user.Roles = model.Roles; + user.Group = model.Group; + user.Email = model.Email; + user.ExtraTime = model.ExtraTime; + user.Password = model.Password; + user.Image = model.Image; + + // Save the data + await LocalStorage.SetItemAsync("data", currentData); + } + } +} diff --git a/Project/adminBlazor/adminBlazor/Services/IDataService.cs b/Project/adminBlazor/adminBlazor/Services/IDataService.cs new file mode 100644 index 0000000..bab3500 --- /dev/null +++ b/Project/adminBlazor/adminBlazor/Services/IDataService.cs @@ -0,0 +1,19 @@ +using adminBlazor.Models; +using Microsoft.AspNetCore.Hosting; +using Blazored.LocalStorage; +namespace adminBlazor.Services +{ + public interface IDataService + { + Task Add(User model); + + Task Count(); + + Task> List(int currentPage, int pageSize); + + Task GetById(int id); + + Task Update(int id, User model); + } + +} diff --git a/Project/adminBlazor/adminBlazor/Shared/NavBar.razor b/Project/adminBlazor/adminBlazor/Shared/NavBar.razor index af14f6f..9fd2288 100644 --- a/Project/adminBlazor/adminBlazor/Shared/NavBar.razor +++ b/Project/adminBlazor/adminBlazor/Shared/NavBar.razor @@ -14,7 +14,6 @@ - @code { private bool isNavMenuVisible = false;