From 8352e4fe28bd537a1bb98d8a40663d5b0f7ae3b6 Mon Sep 17 00:00:00 2001 From: tomivt Date: Tue, 14 Jan 2025 17:33:46 +0100 Subject: [PATCH] Add UserServiceJson --- .../WF-WebAdmin/Pages/DeleteUser.razor.cs | 20 +++--- .../WF-WebAdmin/Pages/ModifQuote.razor.cs | 4 +- .../WF-WebAdmin/Service/IUserService.cs | 2 +- .../WF-WebAdmin/Service/IUserServiceJson.cs | 17 +++++ .../WF-WebAdmin/Service/QuoteServiceStub.cs | 58 +++++++++++++++-- .../WF-WebAdmin/Service/UserServiceStub.cs | 64 +++++++++++++++++++ 6 files changed, 147 insertions(+), 18 deletions(-) create mode 100644 WF-WebAdmin/WF-WebAdmin/Service/IUserServiceJson.cs create mode 100644 WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs diff --git a/WF-WebAdmin/WF-WebAdmin/Pages/DeleteUser.razor.cs b/WF-WebAdmin/WF-WebAdmin/Pages/DeleteUser.razor.cs index 0743f27..3daab72 100644 --- a/WF-WebAdmin/WF-WebAdmin/Pages/DeleteUser.razor.cs +++ b/WF-WebAdmin/WF-WebAdmin/Pages/DeleteUser.razor.cs @@ -1,30 +1,30 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Configuration.UserSecrets; using WF_WebAdmin.Model; +using WF_WebAdmin.Service; namespace WF_WebAdmin.Pages { public partial class DeleteUser { - private List users; - - private bool showPopup = false; private User userToDelete = null; + [Inject] public HttpClient Http { get; set; } [Inject] public NavigationManager NavigationManager { get; set; } + + private List users; + private UserServiceStub userService; protected override async Task OnInitializedAsync() { - users = await Http.GetFromJsonAsync>($"{NavigationManager.BaseUri}fake-dataUser.json"); + userService = new UserServiceStub($"fake-dataUser.json"); + users = await userService.GetUsersJson(); } - - - private void ShowConfirmation(User user) { // Afficher la modale et mémoriser l'utilisateur à supprimer @@ -32,8 +32,6 @@ namespace WF_WebAdmin.Pages showPopup = true; } - - private async Task RemoveUser() { if (userToDelete != null) @@ -47,6 +45,6 @@ namespace WF_WebAdmin.Pages { showPopup = false; } - } - + } + } diff --git a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs index 6bb7933..5ba6ad3 100644 --- a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs +++ b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs @@ -17,7 +17,9 @@ namespace WF_WebAdmin.Pages protected override async Task OnInitializedAsync() { - quotes = await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-dataModifQuote.json"); + string path = $"{NavigationManager.BaseUri}fake-dataModifQuote.json"; + Console.WriteLine($"filePath {path}"); + quotes = await Http.GetFromJsonAsync(path); } } } diff --git a/WF-WebAdmin/WF-WebAdmin/Service/IUserService.cs b/WF-WebAdmin/WF-WebAdmin/Service/IUserService.cs index 1742e10..3a505f4 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/IUserService.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/IUserService.cs @@ -4,7 +4,7 @@ namespace WF_WebAdmin.Service { public interface IUserService { - public void removeUser(User user); + public void removeUserJs(User user); public void updateRole(User user); diff --git a/WF-WebAdmin/WF-WebAdmin/Service/IUserServiceJson.cs b/WF-WebAdmin/WF-WebAdmin/Service/IUserServiceJson.cs new file mode 100644 index 0000000..f6c8383 --- /dev/null +++ b/WF-WebAdmin/WF-WebAdmin/Service/IUserServiceJson.cs @@ -0,0 +1,17 @@ +using WF_WebAdmin.Model; + +namespace WF_WebAdmin.Service +{ + public interface IUserServiceJson + { + public Task> GetUsersJson(); + + public Task SaveUsersJson(List users); + + public Task AddUserJson(User user); + + public Task DeleteUserJson(int id); + + public Task UpdateUserJson(User user); + } +} \ No newline at end of file diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs index d6be33b..6619214 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs @@ -6,11 +6,59 @@ namespace WF_WebAdmin.Service { public class QuoteServiceStub : IQuoteService { - [Inject] - public HttpClient Http { get; set; } + [Inject] public HttpClient Http { get; set; } - [Inject] - public NavigationManager NavigationManager { get; set; } + [Inject] public NavigationManager NavigationManager { get; set; } - + + public Task addQuote(Quote quote) + { + throw new NotImplementedException(); + } + + public Task removeQuote(Quote quote) + { + throw new NotImplementedException(); + } + + public Task validQuote(Quote quote) + { + throw new NotImplementedException(); + } + + public Task updateQuote(Quote quote) + { + throw new NotImplementedException(); + } + + public Task> getAllQuote() + { + throw new NotImplementedException(); + } + + public Task> getSomeQuote(int nb, int page) + { + throw new NotImplementedException(); + } + + public Task> getOnequote(int id) + { + throw new NotImplementedException(); + } + + public Task> reserchQuote(string reserch, List argument) + { + throw new NotImplementedException(); + } + + public Task> getAllQuoteInvalid() + { + throw new NotImplementedException(); + } + + public Task> getSomeQuoteInvalid(int nb, int page) + { + throw new NotImplementedException(); + } + } } diff --git a/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs new file mode 100644 index 0000000..bc34fbd --- /dev/null +++ b/WF-WebAdmin/WF-WebAdmin/Service/UserServiceStub.cs @@ -0,0 +1,64 @@ +using System.Text.Json; +using WF_WebAdmin.Model; + +namespace WF_WebAdmin.Service; + +public class UserServiceStub : IUserServiceJson +{ + private readonly string _jsonFilePath; + + public UserServiceStub(string filePath) + { + _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", filePath); + } + + public async Task> GetUsersJson() + { + if (!File.Exists(_jsonFilePath)) + { + Console.Out.WriteLine($"{_jsonFilePath} not found"); + return new List(); + } + + var json = await File.ReadAllTextAsync(_jsonFilePath); + return JsonSerializer.Deserialize>(json) ?? new List(); + } + + public async Task SaveUsersJson(List users) + { + var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true }); + await File.WriteAllTextAsync(_jsonFilePath, json); + } + + public async Task AddUserJson(User user) + { + var data = await GetUsersJson(); + user.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; + data.Add(user); + await SaveUsersJson(data); + } + + public async Task DeleteUserJson(int id) + { + var data = await GetUsersJson(); + var person = data.FirstOrDefault(p => p.Id == id); + if (person != null) + { + data.Remove(person); + await SaveUsersJson(data); + } + } + + public async Task UpdateUserJson(User user) + { + var data = await GetUsersJson(); + var person = data.FirstOrDefault(p => p.Id == user.Id); + if (person != null) + { + person.Name = user.Name; + person.Email = user.Email; + person.Image = user.Image; + await SaveUsersJson(data); + } + } +} \ No newline at end of file