diff --git a/Blazor/Blazor/Models/AdministratorsModel.cs b/Blazor/Blazor/Models/AdministratorsModel.cs index 023cce4..5a03654 100644 --- a/Blazor/Blazor/Models/AdministratorsModel.cs +++ b/Blazor/Blazor/Models/AdministratorsModel.cs @@ -6,23 +6,8 @@ namespace Blazor.Models; public class AdministratorsModel : PasswordHasher { - public int Id { get; private set; } - public string Username { get; private set; } - public string HashedPassword { get; set; } + public int Id { get; set; } + public string Username { get; set; } + public string hashedPassword { get; set; } - private byte[] salt = RandomNumberGenerator.GetBytes(128 / 8); // for password hash - - AdministratorsModel(int id, string username, string password) - { - this.Id = id; - this.Username = username; - //hash password - this.HashedPassword = Convert.ToBase64String(KeyDerivation.Pbkdf2( - password: password!, - salt: salt, - prf: KeyDerivationPrf.HMACSHA256, - iterationCount: 100000, - numBytesRequested: 256 / 8) - ); - } } diff --git a/Blazor/Blazor/Pages/AddAdministrators.razor b/Blazor/Blazor/Pages/AddAdministrators.razor new file mode 100644 index 0000000..944bb4d --- /dev/null +++ b/Blazor/Blazor/Pages/AddAdministrators.razor @@ -0,0 +1,23 @@ +@page "/addAdministrators" +@using Blazor.Models + +

AddAdministrators

+ + + + + +

+ + +

+ + + +
\ No newline at end of file diff --git a/Blazor/Blazor/Pages/AddAdministrators.razor.cs b/Blazor/Blazor/Pages/AddAdministrators.razor.cs new file mode 100644 index 0000000..222c73d --- /dev/null +++ b/Blazor/Blazor/Pages/AddAdministrators.razor.cs @@ -0,0 +1,27 @@ +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components.Forms; +using Microsoft.AspNetCore.Components; +using Blazor.Models; +using Blazor.Services; + +namespace Blazor.Pages +{ + public partial class AddAdministrators + { + private AdministratorsModel administratorsModel = new(); + + [Inject] + public IDataService DataService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + + private async void HandleValidSubmit() + { + await DataService.Add(administratorsModel); + + NavigationManager.NavigateTo("administrators"); + } + } +} diff --git a/Blazor/Blazor/Pages/Administrators.razor b/Blazor/Blazor/Pages/Administrators.razor index fdb0252..c4b1c77 100644 --- a/Blazor/Blazor/Pages/Administrators.razor +++ b/Blazor/Blazor/Pages/Administrators.razor @@ -1,6 +1,7 @@ @page "/administrators" @using Blazorise.DataGrid @using Blazor.ViewClasses +

Administrators

("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); } + + public async Task GetAdminById(int id) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the chapter int the list + var admin = currentData.FirstOrDefault(w => w.Id == id); + + // Check if chapter exist + if (admin == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + return admin; + } + + public async Task Update(int id, AdministratorsModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the admin int the list + var admin = currentData.FirstOrDefault(w => w.Id == id); + + // Check if admin exist + if (admin == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + // Modify the content of the adminnistrator + admin.Username = model.Username; + admin.hashedPassword = model.hashedPassword; + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + + public async Task Add(AdministratorsModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Simulate the Id + model.Id = currentData.Max(s => s.Id) + 1; + + // Add the chapter to the current data + currentData.Add(new Administrator + { + Id = model.Id, + Username = model.Username, + hashedPassword = model.hashedPassword + }); + + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + public async Task CountAdmin() + { + // Load data from the local storage + var currentData = await _localStorage.GetItemAsync("data"); + + // Check if data exist in the local storage + if (currentData == null) + { + // this code add in the local storage the fake data + var originalData = await _http.GetFromJsonAsync($"{_navigationManager.BaseUri}fake-administrator.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Length; + } + + public async Task> ListAdmin(int currentPage, int pageSize) + { + // Load data from the local storage + var currentData = await _localStorage.GetItemAsync("data"); + + // Check if data exist in the local storage + if (currentData == null) + { + // this code add in the local storage the fake data + var originalData = await _http.GetFromJsonAsync($"{_navigationManager.BaseUri}fake-administrator.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); + } } } diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs index 00ea5ce..cb9ae80 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/Services/IDataService.cs @@ -15,6 +15,15 @@ namespace Blazor.Services Task Update(int id, ChapterModel model); + Task Add(AdministratorsModel model); + + Task Update(int id, AdministratorsModel model); + + Task GetAdminById(int id); + + Task CountAdmin(); + Task> ListAdmin(int currentPage, int pageSize); + Task Delete(int id); } } diff --git a/Blazor/Blazor/Shared/NavMenu.razor b/Blazor/Blazor/Shared/NavMenu.razor index 6c502bb..7e7e6f5 100644 --- a/Blazor/Blazor/Shared/NavMenu.razor +++ b/Blazor/Blazor/Shared/NavMenu.razor @@ -24,6 +24,11 @@ Chapters +