From 750785032ba5f7e4796b7fb9812fe0d008b78f33 Mon Sep 17 00:00:00 2001 From: Louis GUICHARD-MONTGUERS Date: Wed, 15 Jan 2025 13:56:03 +0100 Subject: [PATCH] Quote stub json --- .../WF-WebAdmin/Service/IQuoteServiceJson.cs | 17 ++++ .../WF-WebAdmin/Service/QuoteServiceStub.cs | 84 ++++++++++--------- 2 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 WF-WebAdmin/WF-WebAdmin/Service/IQuoteServiceJson.cs diff --git a/WF-WebAdmin/WF-WebAdmin/Service/IQuoteServiceJson.cs b/WF-WebAdmin/WF-WebAdmin/Service/IQuoteServiceJson.cs new file mode 100644 index 0000000..0a93f33 --- /dev/null +++ b/WF-WebAdmin/WF-WebAdmin/Service/IQuoteServiceJson.cs @@ -0,0 +1,17 @@ +using WF_WebAdmin.Model; + +namespace WF_WebAdmin.Service +{ + public interface IQuoteServiceJson + { + public Task> GetQuoteJson(); + + public Task SaveQuoteJson(List users); + + public Task AddQuoteJson(User user); + + public Task DeleteQuoteJson(int id); + + public Task UpdateQuoteJson(User user); + } +} diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs index 6619214..0ec9970 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs @@ -1,64 +1,66 @@ -using Microsoft.AspNetCore.Components; +using System.Text.Json; using WF_WebAdmin.Model; -using static System.Net.WebRequestMethods; -namespace WF_WebAdmin.Service -{ - public class QuoteServiceStub : IQuoteService - { - [Inject] public HttpClient Http { get; set; } - - [Inject] public NavigationManager NavigationManager { get; set; } +namespace WF_WebAdmin.Service; + public class QuoteServiceStub : IQuoteServiceJson + { + private readonly string _jsonFilePath; - public Task addQuote(Quote quote) - { - throw new NotImplementedException(); - } - - public Task removeQuote(Quote quote) - { - throw new NotImplementedException(); - } - - public Task validQuote(Quote quote) + public QuoteServiceStub(string filePath) { - throw new NotImplementedException(); + _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", filePath); } - public Task updateQuote(Quote quote) + public async Task> GetQuoteJson() { - throw new NotImplementedException(); - } + if (!File.Exists(_jsonFilePath)) + { + Console.Out.WriteLine($"{_jsonFilePath} not found"); + return new List(); + } - public Task> getAllQuote() - { - throw new NotImplementedException(); + var json = await File.ReadAllTextAsync(_jsonFilePath); + return JsonSerializer.Deserialize>(json) ?? new List(); } - public Task> getSomeQuote(int nb, int page) + public async Task SaveQuoteJson(List users) { - throw new NotImplementedException(); + var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true }); + await File.WriteAllTextAsync(_jsonFilePath, json); } - public Task> getOnequote(int id) + public async Task AddQuoteJson(User user) { - throw new NotImplementedException(); + var data = await GetQuoteJson(); + user.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; + data.Add(user); + await SaveQuoteJson(data); } - public Task> reserchQuote(string reserch, List argument) + public async Task DeleteQuoteJson(int id) { - throw new NotImplementedException(); + var data = await GetQuoteJson(); + var person = data.FirstOrDefault(p => p.Id == id); + if (person != null) + { + data.Remove(person); + await SaveQuoteJson(data); + } } - public Task> getAllQuoteInvalid() + public async Task UpdateQuoteJson(User user) { - throw new NotImplementedException(); + var data = await GetQuoteJson(); + 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 SaveQuoteJson(data); + } } - public Task> getSomeQuoteInvalid(int nb, int page) - { - throw new NotImplementedException(); - } - } -} +} + \ No newline at end of file