J ai ratraper les connerie de tommy pour pas changer

QuoteServiceLocal
Kevin MONDEJAR 3 months ago
parent b3f3133e1a
commit d84d419ee4

@ -21,4 +21,8 @@
<DataGridColumn TItem="Quote" Field="@nameof(Quote.Langue)" Caption="Langue" /> <DataGridColumn TItem="Quote" Field="@nameof(Quote.Langue)" Caption="Langue" />
<DataGridColumn TItem="Quote" Field="@nameof(Quote.DateSrc)" Caption="Date" DisplayFormat="{0:d}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" /> <DataGridColumn TItem="Quote" Field="@nameof(Quote.DateSrc)" Caption="Date" DisplayFormat="{0:d}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" />
</DataGrid> </DataGrid>
<div>
@pagination
</div>
} }

@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WF_WebAdmin.Model; using WF_WebAdmin.Model;
using WF_WebAdmin.Service;
namespace WF_WebAdmin.Pages namespace WF_WebAdmin.Pages
{ {
@ -9,17 +11,64 @@ namespace WF_WebAdmin.Pages
private int MaxValue = 5; private int MaxValue = 5;
[Inject] private int pages = 1;
public HttpClient Http { get; set; }
private int maxPage;
private RenderFragment pagination;
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public IQuoteService QuoteService { get; set; }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
string path = $"{NavigationManager.BaseUri}fake-dataModifQuote.json"; maxPage = await QuoteService.getNbQuote() / MaxValue;
Console.WriteLine($"filePath {path}"); if(maxPage * MaxValue != await QuoteService.getNbQuote())
quotes = await Http.GetFromJsonAsync<Quote[]>(path); maxPage += 1;
List<Quote> quotesList = await QuoteService.getSomeQuote(MaxValue, pages);
quotes = quotesList.ToArray();
buildPagination();
}
protected void buildPagination()
{
if (pages == 1)
{
pagination = __builder =>
{
__builder.AddMarkupContent(0, @"<button>1</button>
<button onClick='pageNumero(pages+1)'>@pages+1</button>
<button onClick='pageNumero(pages+2)'>@pages+2</button>
<p>...</p>
<button onClick='pageNumero(maxPage)'>@maxPage</button>
<button onClick='pageSuivante()'>></button>");
};
}
}
protected async Task pageSuivante()
{
pages += 1;
List<Quote> quotesList = await QuoteService.getSomeQuote(MaxValue, pages);
quotes = quotesList.ToArray();
}
protected async Task pagePrécédente()
{
if (pages > 1)
{
pages -= 1;
List<Quote> quotesList = await QuoteService.getSomeQuote(MaxValue, pages);
quotes = quotesList.ToArray();
}
}
protected async Task pageNumero(int page)
{
this.pages = page;
List<Quote> quotesList = await QuoteService.getSomeQuote(MaxValue, this.pages);
quotes = quotesList.ToArray();
} }
} }
} }

@ -4,6 +4,7 @@ using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using WF_WebAdmin.Data; using WF_WebAdmin.Data;
using WF_WebAdmin.Service;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -11,7 +12,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(); builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>(); builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped<IQuoteService,QuoteServiceStub>();
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();
builder.Services builder.Services

@ -16,12 +16,14 @@ namespace WF_WebAdmin.Service
public Task<List<Quote>> getSomeQuote(int nb, int page); public Task<List<Quote>> getSomeQuote(int nb, int page);
public Task<List<Quote>> getOnequote(int id); public Task<Quote> getOnequote(int id);
public Task<List<Quote>> reserchQuote(string reserch, List<string> argument); public Task<List<Quote>> reserchQuote(string reserch, List<string> argument);
public Task<List<Quote>> getAllQuoteInvalid(); public Task<List<Quote>> getAllQuoteInvalid();
public Task<List<Quote>> getSomeQuoteInvalid(int nb, int page); public Task<List<Quote>> getSomeQuoteInvalid(int nb, int page);
public Task<int> getNbQuote();
} }
} }

@ -3,64 +3,108 @@ using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service; 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<Quote> 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<List<User>> 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"); data.Remove(q);
return new List<User>(); await saveQuoteJson(data);
} }
}
var json = await File.ReadAllTextAsync(_jsonFilePath); public async Task validQuote(Quote quote)
return JsonSerializer.Deserialize<List<User>>(json) ?? new List<User>(); {
throw new NotImplementedException();
} }
public async Task SaveQuoteJson(List<User> users) public async Task updateQuote(Quote quote)
{ {
var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true }); var data = await getAllQuote();
await File.WriteAllTextAsync(_jsonFilePath, json); 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<List<Quote>> getAllQuote()
{ {
var data = await GetQuoteJson(); if (!File.Exists(_jsonFilePath))
user.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1; {
data.Add(user); Console.Out.WriteLine($"{_jsonFilePath} not found");
await SaveQuoteJson(data); return new List<Quote>();
}
var json = await File.ReadAllTextAsync(_jsonFilePath);
return JsonSerializer.Deserialize<List<Quote>>(json) ?? new List<Quote>();
} }
public async Task DeleteQuoteJson(int id) public async Task<List<Quote>> getSomeQuote(int nb, int page)
{ {
var data = await GetQuoteJson(); var quotes = await getAllQuote();
var person = data.FirstOrDefault(p => p.Id == id); if((page - 1) * nb + nb > quotes.Count())
if (person != null)
{ {
data.Remove(person); return quotes.GetRange(quotes.Count()-nb, nb);
await SaveQuoteJson(data);
} }
return quotes.GetRange((page - 1) * nb, nb);
} }
public async Task UpdateQuoteJson(User user) public async Task<Quote> getOnequote(int id)
{ {
var data = await GetQuoteJson(); var data = await getAllQuote();
var person = data.FirstOrDefault(p => p.Id == user.Id); var q = data.FirstOrDefault(p => p.Id == id);
if (person != null) if (q != null)
{ {
person.Name = user.Name; return q;
person.Email = user.Email;
person.Image = user.Image;
await SaveQuoteJson(data);
} }
return new Quote();
}
public async Task<List<Quote>> reserchQuote(string reserch, List<string> argument)
{
throw new NotImplementedException();
}
public async Task<List<Quote>> getAllQuoteInvalid()
{
throw new NotImplementedException();
} }
public async Task<List<Quote>> getSomeQuoteInvalid(int nb, int page)
{
throw new NotImplementedException();
}
public async Task<int> getNbQuote()
{
var data = await getAllQuote();
return data.Count;
}
} }

@ -18,5 +18,45 @@
"DateSrc": "2002-02-02", "DateSrc": "2002-02-02",
"Like": 0, "Like": 0,
"Langue": "fr" "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"
} }
] ]
Loading…
Cancel
Save