From d84d419ee43cb70e765a9eb864ab72291d0af0d1 Mon Sep 17 00:00:00 2001 From: Kevin MONDEJAR Date: Tue, 21 Jan 2025 09:53:36 +0100 Subject: [PATCH] J ai ratraper les connerie de tommy pour pas changer --- .../WF-WebAdmin/Pages/ModifQuote.razor | 4 + .../WF-WebAdmin/Pages/ModifQuote.razor.cs | 61 +++++++++- WF-WebAdmin/WF-WebAdmin/Program.cs | 3 +- .../WF-WebAdmin/Service/IQuoteService.cs | 4 +- .../WF-WebAdmin/Service/QuoteServiceStub.cs | 108 ++++++++++++------ .../WF-WebAdmin/wwwroot/fake-dataQuote.json | 40 +++++++ 6 files changed, 180 insertions(+), 40 deletions(-) diff --git a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor index d562f7a..2817608 100644 --- a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor +++ b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor @@ -21,4 +21,8 @@ + +
+ @pagination +
} \ No newline at end of file diff --git a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs index 5ba6ad3..a9e8524 100644 --- a/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs +++ b/WF-WebAdmin/WF-WebAdmin/Pages/ModifQuote.razor.cs @@ -1,5 +1,7 @@ using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Mvc.RazorPages; using WF_WebAdmin.Model; +using WF_WebAdmin.Service; namespace WF_WebAdmin.Pages { @@ -9,17 +11,64 @@ namespace WF_WebAdmin.Pages private int MaxValue = 5; - [Inject] - public HttpClient Http { get; set; } + private int pages = 1; + + private int maxPage; + + private RenderFragment pagination; [Inject] - public NavigationManager NavigationManager { get; set; } + public IQuoteService QuoteService { get; set; } protected override async Task OnInitializedAsync() { - string path = $"{NavigationManager.BaseUri}fake-dataModifQuote.json"; - Console.WriteLine($"filePath {path}"); - quotes = await Http.GetFromJsonAsync(path); + maxPage = await QuoteService.getNbQuote() / MaxValue; + if(maxPage * MaxValue != await QuoteService.getNbQuote()) + maxPage += 1; + List quotesList = await QuoteService.getSomeQuote(MaxValue, pages); + quotes = quotesList.ToArray(); + buildPagination(); + } + + protected void buildPagination() + { + if (pages == 1) + { + pagination = __builder => + { + __builder.AddMarkupContent(0, @" + + +

...

+ + "); + }; + } + + } + + protected async Task pageSuivante() + { + pages += 1; + List quotesList = await QuoteService.getSomeQuote(MaxValue, pages); + quotes = quotesList.ToArray(); + } + + protected async Task pagePrécédente() + { + if (pages > 1) + { + pages -= 1; + List quotesList = await QuoteService.getSomeQuote(MaxValue, pages); + quotes = quotesList.ToArray(); + } + } + + protected async Task pageNumero(int page) + { + this.pages = page; + List quotesList = await QuoteService.getSomeQuote(MaxValue, this.pages); + quotes = quotesList.ToArray(); } } } diff --git a/WF-WebAdmin/WF-WebAdmin/Program.cs b/WF-WebAdmin/WF-WebAdmin/Program.cs index fa0d376..5af7669 100644 --- a/WF-WebAdmin/WF-WebAdmin/Program.cs +++ b/WF-WebAdmin/WF-WebAdmin/Program.cs @@ -4,6 +4,7 @@ using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using WF_WebAdmin.Data; +using WF_WebAdmin.Service; var builder = WebApplication.CreateBuilder(args); @@ -11,7 +12,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); - +builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services diff --git a/WF-WebAdmin/WF-WebAdmin/Service/IQuoteService.cs b/WF-WebAdmin/WF-WebAdmin/Service/IQuoteService.cs index a76b4b0..356edd8 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/IQuoteService.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/IQuoteService.cs @@ -16,12 +16,14 @@ namespace WF_WebAdmin.Service public Task> getSomeQuote(int nb, int page); - public Task> getOnequote(int id); + public Task getOnequote(int id); public Task> reserchQuote(string reserch, List argument); public Task> getAllQuoteInvalid(); public Task> getSomeQuoteInvalid(int nb, int page); + + public Task getNbQuote(); } } diff --git a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs index 0ec9970..6644168 100644 --- a/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs +++ b/WF-WebAdmin/WF-WebAdmin/Service/QuoteServiceStub.cs @@ -3,64 +3,108 @@ using WF_WebAdmin.Model; namespace WF_WebAdmin.Service; - public class QuoteServiceStub : IQuoteServiceJson + public class QuoteServiceStub : IQuoteService { - private readonly string _jsonFilePath; + private readonly string _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", "fake-dataQuote.json"); - public QuoteServiceStub(string filePath) + public async Task saveQuoteJson(List quotes) { - _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", filePath); + var json = JsonSerializer.Serialize(quotes, new JsonSerializerOptions { WriteIndented = true }); + await File.WriteAllTextAsync(_jsonFilePath, json); } - public async Task> GetQuoteJson() + public async Task addQuote(Quote quote) { - if (!File.Exists(_jsonFilePath)) + var data = await getAllQuote(); + quote.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; + data.Add(quote); + await saveQuoteJson(data); + } + + public async Task removeQuote(Quote quote) + { + var data = await getAllQuote(); + var q = data.FirstOrDefault(p => p.Id == quote.Id); + if (q != null) { - Console.Out.WriteLine($"{_jsonFilePath} not found"); - return new List(); + data.Remove(q); + await saveQuoteJson(data); } + } - var json = await File.ReadAllTextAsync(_jsonFilePath); - return JsonSerializer.Deserialize>(json) ?? new List(); + public async Task validQuote(Quote quote) + { + throw new NotImplementedException(); } - public async Task SaveQuoteJson(List users) + public async Task updateQuote(Quote quote) { - var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true }); - await File.WriteAllTextAsync(_jsonFilePath, json); + var data = await getAllQuote(); + var q = data.FirstOrDefault(p => p.Id == quote.Id); + if (q != null) + { + q.Content = quote.Content; + q.Charac = quote.Charac; + q.ImgPath = quote.ImgPath; + q.TitleSrc = quote.TitleSrc; + q.DateSrc = quote.DateSrc; + q.Langue = quote.Langue; + await saveQuoteJson(data); + } } - public async Task AddQuoteJson(User user) + public async Task> getAllQuote() { - var data = await GetQuoteJson(); - user.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; - data.Add(user); - await SaveQuoteJson(data); + 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 DeleteQuoteJson(int id) + public async Task> getSomeQuote(int nb, int page) { - var data = await GetQuoteJson(); - var person = data.FirstOrDefault(p => p.Id == id); - if (person != null) + var quotes = await getAllQuote(); + if((page - 1) * nb + nb > quotes.Count()) { - data.Remove(person); - await SaveQuoteJson(data); + return quotes.GetRange(quotes.Count()-nb, nb); } + return quotes.GetRange((page - 1) * nb, nb); } - public async Task UpdateQuoteJson(User user) + public async Task getOnequote(int id) { - var data = await GetQuoteJson(); - var person = data.FirstOrDefault(p => p.Id == user.Id); - if (person != null) + var data = await getAllQuote(); + var q = data.FirstOrDefault(p => p.Id == id); + if (q != null) { - person.Name = user.Name; - person.Email = user.Email; - person.Image = user.Image; - await SaveQuoteJson(data); + return q; } + return new Quote(); + } + + public async Task> reserchQuote(string reserch, List argument) + { + throw new NotImplementedException(); + } + + public async Task> getAllQuoteInvalid() + { + throw new NotImplementedException(); } + public async Task> getSomeQuoteInvalid(int nb, int page) + { + throw new NotImplementedException(); + } + + public async Task getNbQuote() + { + var data = await getAllQuote(); + return data.Count; + } } \ No newline at end of file diff --git a/WF-WebAdmin/WF-WebAdmin/wwwroot/fake-dataQuote.json b/WF-WebAdmin/WF-WebAdmin/wwwroot/fake-dataQuote.json index f38cd34..2b96570 100644 --- a/WF-WebAdmin/WF-WebAdmin/wwwroot/fake-dataQuote.json +++ b/WF-WebAdmin/WF-WebAdmin/wwwroot/fake-dataQuote.json @@ -18,5 +18,45 @@ "DateSrc": "2002-02-02", "Like": 0, "Langue": "fr" + }, + { + "Id": 3, + "Content": "coucou", + "Charac": "moi", + "ImgPath": "img", + "TitleSrc": "G4", + "DateSrc": "2001-01-01", + "Like": 20, + "Langue": "fr" + }, + { + "Id": 4, + "Content": "boujour", + "Charac": "toi", + "ImgPath": "img", + "TitleSrc": "G4", + "DateSrc": "2002-02-02", + "Like": 0, + "Langue": "fr" + }, + { + "Id": 5, + "Content": "coucou", + "Charac": "moi", + "ImgPath": "img", + "TitleSrc": "G4", + "DateSrc": "2001-01-01", + "Like": 20, + "Langue": "fr" + }, + { + "Id": 6, + "Content": "boujour", + "Charac": "toi", + "ImgPath": "img", + "TitleSrc": "G4", + "DateSrc": "2002-02-02", + "Like": 0, + "Langue": "fr" } ] \ No newline at end of file