|
|
@ -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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|