Add UserServiceJson

pull/23/head
tomivt 3 months ago
parent e176e97bb2
commit 8352e4fe28

@ -1,30 +1,30 @@
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Configuration.UserSecrets;
using WF_WebAdmin.Model;
using WF_WebAdmin.Service;
namespace WF_WebAdmin.Pages
{
public partial class DeleteUser
{
private List<User> users;
private bool showPopup = false;
private User userToDelete = null;
[Inject]
public HttpClient Http { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
private List<User> users;
private UserServiceStub userService;
protected override async Task OnInitializedAsync()
{
users = await Http.GetFromJsonAsync<List<User>>($"{NavigationManager.BaseUri}fake-dataUser.json");
userService = new UserServiceStub($"fake-dataUser.json");
users = await userService.GetUsersJson();
}
private void ShowConfirmation(User user)
{
// Afficher la modale et mémoriser l'utilisateur à supprimer
@ -32,8 +32,6 @@ namespace WF_WebAdmin.Pages
showPopup = true;
}
private async Task RemoveUser()
{
if (userToDelete != null)

@ -17,7 +17,9 @@ namespace WF_WebAdmin.Pages
protected override async Task OnInitializedAsync()
{
quotes = await Http.GetFromJsonAsync<Quote[]>($"{NavigationManager.BaseUri}fake-dataModifQuote.json");
string path = $"{NavigationManager.BaseUri}fake-dataModifQuote.json";
Console.WriteLine($"filePath {path}");
quotes = await Http.GetFromJsonAsync<Quote[]>(path);
}
}
}

@ -4,7 +4,7 @@ namespace WF_WebAdmin.Service
{
public interface IUserService
{
public void removeUser(User user);
public void removeUserJs(User user);
public void updateRole(User user);

@ -0,0 +1,17 @@
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service
{
public interface IUserServiceJson
{
public Task<List<User>> GetUsersJson();
public Task SaveUsersJson(List<User> users);
public Task AddUserJson(User user);
public Task DeleteUserJson(int id);
public Task UpdateUserJson(User user);
}
}

@ -6,11 +6,59 @@ namespace WF_WebAdmin.Service
{
public class QuoteServiceStub : IQuoteService
{
[Inject]
public HttpClient Http { get; set; }
[Inject] public HttpClient Http { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject] public NavigationManager NavigationManager { get; set; }
public Task addQuote(Quote quote)
{
throw new NotImplementedException();
}
public Task removeQuote(Quote quote)
{
throw new NotImplementedException();
}
public Task validQuote(Quote quote)
{
throw new NotImplementedException();
}
public Task updateQuote(Quote quote)
{
throw new NotImplementedException();
}
public Task<List<Quote>> getAllQuote()
{
throw new NotImplementedException();
}
public Task<List<Quote>> getSomeQuote(int nb, int page)
{
throw new NotImplementedException();
}
public Task<List<Quote>> getOnequote(int id)
{
throw new NotImplementedException();
}
public Task<List<Quote>> reserchQuote(string reserch, List<string> argument)
{
throw new NotImplementedException();
}
public Task<List<Quote>> getAllQuoteInvalid()
{
throw new NotImplementedException();
}
public Task<List<Quote>> getSomeQuoteInvalid(int nb, int page)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,64 @@
using System.Text.Json;
using WF_WebAdmin.Model;
namespace WF_WebAdmin.Service;
public class UserServiceStub : IUserServiceJson
{
private readonly string _jsonFilePath;
public UserServiceStub(string filePath)
{
_jsonFilePath = Path.Combine(Environment.CurrentDirectory, "wwwroot", filePath);
}
public async Task<List<User>> GetUsersJson()
{
if (!File.Exists(_jsonFilePath))
{
Console.Out.WriteLine($"{_jsonFilePath} not found");
return new List<User>();
}
var json = await File.ReadAllTextAsync(_jsonFilePath);
return JsonSerializer.Deserialize<List<User>>(json) ?? new List<User>();
}
public async Task SaveUsersJson(List<User> users)
{
var json = JsonSerializer.Serialize(users, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(_jsonFilePath, json);
}
public async Task AddUserJson(User user)
{
var data = await GetUsersJson();
user.Id = data.Count > 0 ? data.Max(p => p.Id) + 1 : 1;
data.Add(user);
await SaveUsersJson(data);
}
public async Task DeleteUserJson(int id)
{
var data = await GetUsersJson();
var person = data.FirstOrDefault(p => p.Id == id);
if (person != null)
{
data.Remove(person);
await SaveUsersJson(data);
}
}
public async Task UpdateUserJson(User user)
{
var data = await GetUsersJson();
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 SaveUsersJson(data);
}
}
}
Loading…
Cancel
Save