fin du travail sur la modif

blazor
Patrick BRUGIERE 1 year ago
parent 87f5360728
commit 7389af802d

@ -1,5 +1,6 @@
using adminBlazor.Models; using adminBlazor.Models;
using Blazorise; using Blazorise;
using Blazorise.Extensions;
using System.Data; using System.Data;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Xml.Linq; using System.Xml.Linq;
@ -8,7 +9,7 @@ namespace adminBlazor.Factories
{ {
public static class UserFactory public static class UserFactory
{ {
public static UserModel ToModel(UserModel user, byte[] imageContent) public static UserModel ToModel(User user/* byte[] imageContent*/)
{ {
return new UserModel return new UserModel
{ {
@ -41,19 +42,37 @@ namespace adminBlazor.Factories
Roles = user.Roles Roles = user.Roles
}; };
} }
public static void Update(User item, UserModel user)
public static void Update(UserModel item, UserModel user)
{ {
item.Id = user.Id;
item.Name = user.Name;
item.Surname = user.Surname; if (!string.IsNullOrEmpty(user.Name))
item.Nickname = user.Nickname; item.Name = user.Name;
item.ExtraTime = user.ExtraTime;
item.Image = user.Image; if (!string.IsNullOrEmpty(user.Surname))
item.Group = user.Group; item.Surname = user.Surname;
item.Password = user.Password;
item.Email = user.Email; if (!string.IsNullOrEmpty(user.Nickname))
item.Roles = user.Roles; item.Nickname = user.Nickname;
if (user.ExtraTime!=null)
item.ExtraTime = user.ExtraTime;
if (!string.IsNullOrEmpty(user.Image))
item.Image = user.Image;
if (user.Group!=null)
item.Group = user.Group;
if (!string.IsNullOrEmpty(user.Password))
item.Password = user.Password;
if (!string.IsNullOrEmpty(user.Email))
item.Email = user.Email;
if (user.Roles != null)
item.Roles = user.Roles;
} }
} }
} }

@ -12,10 +12,7 @@ namespace adminBlazor.Models
public string Nickname { get; set; } public string Nickname { get; set; }
public string Image { get; set; } public string Image { get; set; }
public bool ExtraTime { get; set; } public bool ExtraTime { get; set; }
[Range(0, 100)]
public int Group { get; set; } public int Group { get; set; }
public List<String> Roles { get; set; } public List<String> Roles { get; set; }
} }
} }

@ -1,4 +1,5 @@
using adminBlazor.Models; using adminBlazor.Factories;
using adminBlazor.Models;
using adminBlazor.Services; using adminBlazor.Services;
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -32,14 +33,17 @@ namespace adminBlazor.Pages
/// <summary> /// <summary>
/// The current item model /// The current item model
/// </summary> /// </summary>
private Models.UserModel user = new Models.UserModel() private Models.User user = new Models.User()
{ {
Roles = new List<string>() Roles = new List<string>()
}; };
private async void HandleValidSubmit() private async void HandleValidSubmit()
{ {
await DataService.Update(Id, user); UserModel item = UserFactory.ToModel(user);
await DataService.Update(Id,item);
NavigationManager.NavigateTo("list"); NavigationManager.NavigateTo("list");
} }
@ -56,7 +60,7 @@ namespace adminBlazor.Pages
} }
// Set the model with the item // Set the model with the item
user = new UserModel user = new User
{ {
Id = user.Id, Id = user.Id,
Name = user.Name, Name = user.Name,

@ -30,7 +30,7 @@ namespace adminBlazor.Services
_localStorage = localStorage; // Assure-toi que LocalStorage est initialisé correctement ici _localStorage = localStorage; // Assure-toi que LocalStorage est initialisé correctement ici
} }
public Task Add(UserModel model) public Task Add(User model)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -40,11 +40,11 @@ namespace adminBlazor.Services
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<UserModel> GetById(int id) public async Task<User> GetById(int id)
{ {
//var currentData = await LocalStorage.GetItemAsync<User[]>("user.json"); //var currentData = await LocalStorage.GetItemAsync<User[]>("user.json");
var currentData = await _localStorage.GetItemAsync<List<UserModel>>("data"); var currentData = await _localStorage.GetItemAsync<List<User>>("data");
var user = currentData.FirstOrDefault(w => w.Id == id); var user = currentData.FirstOrDefault(w => w.Id == id);
@ -56,7 +56,7 @@ namespace adminBlazor.Services
return user; return user;
} }
public Task<List<UserModel>> List(int currentPage, int pageSize) public Task<List<User>> List(int currentPage, int pageSize)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -64,7 +64,7 @@ namespace adminBlazor.Services
public async Task Update(int id, UserModel model) public async Task Update(int id, UserModel model)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<UserModel>>("data"); var currentData = await _localStorage.GetItemAsync<List<User>>("data");
var user = currentData.FirstOrDefault(w => w.Id == id); var user = currentData.FirstOrDefault(w => w.Id == id);
@ -74,7 +74,8 @@ namespace adminBlazor.Services
} }
// Save the image // Save the image
// var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images"); //
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
// Check if the folder "images" exist // Check if the folder "images" exist
// if (!imagePathInfo.Exists) // if (!imagePathInfo.Exists)
@ -94,22 +95,14 @@ namespace adminBlazor.Services
} }
// Determine the image name // Determine the image name
//var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
// Write the file content // Write the file content
// await File.WriteAllBytesAsync(fileName.FullName, model.Image); // await File.WriteAllBytesAsync(fileName.FullName, model.Image);
UserFactory.Update(user, model); UserFactory.Update(user, model);
// Modify the content of the item // Modify the content of the item
user.Nickname = model.Nickname;
user.Name = model.Name;
user.Surname = model.Surname;
user.Roles = model.Roles;
user.Group = model.Group;
user.Email = model.Email;
user.ExtraTime = model.ExtraTime;
user.Password = model.Password;
user.Image = model.Image;
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);

@ -7,13 +7,13 @@ namespace adminBlazor.Services
public interface IDataService public interface IDataService
{ {
Task Add(UserModel model); Task Add(User model);
Task<int> Count(); Task<int> Count();
Task<List<UserModel>> List(int currentPage, int pageSize); Task<List<User>> List(int currentPage, int pageSize);
Task<UserModel> GetById(int id); Task<User> GetById(int id);
Task Update(int id, UserModel model); Task Update(int id, UserModel model);
} }

Loading…
Cancel
Save