Quote stub json

pull/23/head
Louis GUICHARD-MONTGUERS 3 months ago
parent dc90d68d79
commit 750785032b

@ -0,0 +1,17 @@
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service
{
public interface IQuoteServiceJson
{
public Task<List<User>> GetQuoteJson();
public Task SaveQuoteJson(List<User> users);
public Task AddQuoteJson(User user);
public Task DeleteQuoteJson(int id);
public Task UpdateQuoteJson(User user);
}
}

@ -1,64 +1,66 @@
using Microsoft.AspNetCore.Components; using System.Text.Json;
using WF_WebAdmin.Model; using WF_WebAdmin.Model;
using static System.Net.WebRequestMethods;
namespace WF_WebAdmin.Service namespace WF_WebAdmin.Service;
{
public class QuoteServiceStub : IQuoteService
{
[Inject] public HttpClient Http { get; set; }
[Inject] public NavigationManager NavigationManager { get; set; }
public class QuoteServiceStub : IQuoteServiceJson
{
private readonly string _jsonFilePath;
public Task addQuote(Quote quote) public QuoteServiceStub(string filePath)
{
throw new NotImplementedException();
}
public Task removeQuote(Quote quote)
{
throw new NotImplementedException();
}
public Task validQuote(Quote quote)
{ {
throw new NotImplementedException(); _jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", filePath);
} }
public Task updateQuote(Quote quote) public async Task<List<User>> GetQuoteJson()
{ {
throw new NotImplementedException(); if (!File.Exists(_jsonFilePath))
} {
Console.Out.WriteLine($"{_jsonFilePath} not found");
return new List<User>();
}
public Task<List<Quote>> getAllQuote() var json = await File.ReadAllTextAsync(_jsonFilePath);
{ return JsonSerializer.Deserialize<List<User>>(json) ?? new List<User>();
throw new NotImplementedException();
} }
public Task<List<Quote>> getSomeQuote(int nb, int page) public async Task SaveQuoteJson(List<User> users)
{ {
throw new NotImplementedException(); var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(_jsonFilePath, json);
} }
public Task<List<Quote>> 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<List<Quote>> reserchQuote(string reserch, List<string> 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<List<Quote>> 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<List<Quote>> getSomeQuoteInvalid(int nb, int page)
{
throw new NotImplementedException();
}
}
} }
Loading…
Cancel
Save