Merge : Oui

pull/38/head
Jade VAN BRABANDT 1 year ago
commit 90327265b9

@ -7,31 +7,39 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Content Remove="Pages\_Layout.cshtml" /> <Compile Remove="Pages\Player\**" />
<Content Remove="Pages\Player\**" />
<EmbeddedResource Remove="Pages\Player\**" />
<None Remove="Pages\Player\**" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Remove="Pages\_Layout.cshtml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.4.0" /> <PackageReference Include="Blazored.LocalStorage" Version="4.4.0" />
<PackageReference Include="Blazored.Modal" Version="7.1.0" /> <PackageReference Include="Blazored.Modal" Version="7.1.0" />
<PackageReference Include="Blazorise.Bootstrap" Version="1.4.0" /> <PackageReference Include="Blazorise.Bootstrap" Version="1.4.0" />
<PackageReference Include="Blazorise.DataGrid" Version="1.4.0" /> <PackageReference Include="Blazorise.DataGrid" Version="1.4.0" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.4.0" /> <PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.4.0" />
<PackageReference Include="ChoETL.JSON.NETStandard" Version="1.2.1.64" /> <PackageReference Include="ChoETL.JSON.NETStandard" Version="1.2.1.64" />
<PackageReference Include="ChoETL.NETStandard" Version="1.2.1.64" /> <PackageReference Include="ChoETL.NETStandard" Version="1.2.1.64" />
<PackageReference Include="System.Net.Http" Version="4.3.4" /> <PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" /> <PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup> <PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<UpToDateCheckInput Remove="Pages\_Layout.cshtml" /> <ItemGroup>
</ItemGroup> <UpToDateCheckInput Remove="Pages\_Layout.cshtml" />
</ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="Pages\_Layout.cshtml" /> <ItemGroup>
</ItemGroup> <_ContentIncludedByDefault Remove="Pages\_Layout.cshtml" />
</ItemGroup>
<ItemGroup>
<None Include="Pages\_Layout.cshtml" /> <ItemGroup>
<None Include="Pages\_Layout.cshtml" />
</ItemGroup> </ItemGroup>

@ -1,13 +1,30 @@
using Microsoft.AspNetCore.Cryptography.KeyDerivation; using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
namespace Blazor.Models;
namespace Blazor.Models;
public class AdministratorModel : PasswordHasher<string>
{ public class AdministratorModel
public int Id { get; set; } {
public string Username { get; set; } public int Id { get; set; }
public string HashedPassword { get; set; } public string Username { get; set; }
public string HashedPassword { get; set; }
}
public void HashPassword(string password)
{
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.UTF8.GetBytes(password);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
HashedPassword = sb.ToString();
}
}
}

@ -1,7 +1,7 @@
@page "/addAdministrators" @page "/addAdministrator"
@using Blazor.Models @using Blazor.Models
<h3>AddAdministrators</h3> <h3>AddAdministrator</h3>
<EditForm Model="@administratorModel" OnValidSubmit="@HandleValidSubmit"> <EditForm Model="@administratorModel" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator /> <DataAnnotationsValidator />

@ -16,11 +16,40 @@ namespace Blazor.Pages.Admins
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[Inject]
public ILogger<AddAdministrator> Logger { get; set; }
private async void HandleValidSubmit() private async void HandleValidSubmit()
{ {
administratorModel.HashPassword(administratorModel.HashedPassword);
await DataService.Add(administratorModel); await DataService.Add(administratorModel);
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("username", administratorModel.Username));
formData.Add(new KeyValuePair<string, string>("password", administratorModel.HashedPassword));
var formContent = new FormUrlEncodedContent(formData);
string apiUri = "https://trusting-panini.87-106-126-109.plesk.page/api/add/administrators";
using (var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(apiUri, formContent);
if (response.IsSuccessStatusCode)
{
var responseBody = await response.Content.ReadAsStringAsync();
}
else
{
var errorResponse = await response.Content.ReadAsStringAsync();
}
}
Logger.LogInformation("Admin '{administratorsModelName}' added", administratorModel.Username);
NavigationManager.NavigateTo("administrators"); NavigationManager.NavigateTo("administrators");
} }
} }

@ -9,6 +9,7 @@ using Blazor.Modals;
using Blazor.Services; using Blazor.Services;
using Blazored.Modal; using Blazored.Modal;
using Blazored.Modal; using Blazored.Modal;
using Blazor.Pages.Chapters;
namespace Blazor.Pages.Admins; namespace Blazor.Pages.Admins;
@ -43,13 +44,13 @@ public partial class Administrators
} }
// When you use a real API, we use this follow code // When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Item[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); //var response = await Http.GetFromJsonAsync<ChaptersModel[]>( $"https://trusting-panini.87-106-126-109.plesk.page/api/administrators?page={e.Page}&pageSize={e.PageSize}" );
var response = (await Http.GetFromJsonAsync<Administrator[]>($"{NavigationManager.BaseUri}fake-administrator.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); var response = Http.GetFromJsonAsync<Administrator[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/administrators").Result;
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
totalItem = (await Http.GetFromJsonAsync<List<Administrator>>($"{NavigationManager.BaseUri}fake-administrator.json")).Count;
administrators = new List<Administrator>(response); // an actual data for the current page administrators = new List<Administrator>(response); // an actual data for the current page
totalItem = administrators.Count;
} }
} }
@ -67,7 +68,7 @@ public partial class Administrators
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method) // this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
var originalData = Http.GetFromJsonAsync<AdministratorModel[]>($"trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; var originalData = Http.GetFromJsonAsync<AdministratorModel[]>($"https://trusting-panini.87-106-126-109.plesk.page/api/administrators").Result;
await LocalStorage.SetItemAsync("data", originalData); await LocalStorage.SetItemAsync("data", originalData);
} }
} }

@ -1,4 +1,5 @@
using Blazor.Models; using Blazor.Models;
using Blazor.Pages.Chapters;
using Blazor.Services; using Blazor.Services;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -21,9 +22,15 @@ namespace Blazor.Pages.Admins
[Inject] [Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; } public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public ILogger<EditAdministrator> Logger { get; set; }
private string OldAdminName { get; set; }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
var administrator = await DataService.GetAdminById(Id); var administrator = await DataService.GetAdminById(Id);
OldAdminName = administrator.Username;
// Set the model with the admin // Set the model with the admin
administratorModel = new AdministratorModel administratorModel = new AdministratorModel
@ -37,6 +44,9 @@ namespace Blazor.Pages.Admins
{ {
await DataService.Update(Id, administratorModel); await DataService.Update(Id, administratorModel);
Logger.LogInformation("Admin '{OldAdminModelName}' edited in '{NewAdminModelName}'",OldAdminName,administratorsModel.Username);
NavigationManager.NavigateTo("administrators"); NavigationManager.NavigateTo("administrators");
} }
} }

@ -14,6 +14,10 @@ public partial class AddChapter
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[Inject]
public ILogger<AddChapter> Logger { get; set; }
private async void HandleValidSubmit() private async void HandleValidSubmit()
{ {
@ -40,6 +44,9 @@ public partial class AddChapter
var errorResponse = await response.Content.ReadAsStringAsync(); var errorResponse = await response.Content.ReadAsStringAsync();
} }
} }
Logger.LogInformation("Chapter '{chapterModelName}' added",chapterModel.Name);
NavigationManager.NavigateTo("chapters"); NavigationManager.NavigateTo("chapters");
} }

@ -1,40 +1,47 @@
using Blazor.Models; using Blazor.Models;
using Blazor.Services; using Blazor.Services;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Blazor.Pages.Chapters; namespace Blazor.Pages.Chapters;
public partial class EditChapter public partial class EditChapter
{ {
[Parameter] [Parameter]
public int Id { get; set; } public int Id { get; set; }
private ChapterModel chapterModel = new(); private ChapterModel chapterModel = new();
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
[Inject] [Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; } public IWebHostEnvironment WebHostEnvironment { get; set; }
protected override async Task OnInitializedAsync() [Inject]
{ public ILogger<EditChapter> Logger { get; set; }
var chapter = await DataService.GetById(Id);
private string OldChapterName { get; set; }
// Set the model with the chapter
chapterModel = new ChapterModel protected override async Task OnInitializedAsync()
{ {
Id = chapter.Id, var chapter = await DataService.GetById(Id);
Name = chapter.Name OldChapterName = chapter.Name;
};
} // Set the model with the chapter
chapterModel = new ChapterModel
private async void HandleValidSubmit() {
{ Id = chapter.Id,
await DataService.Update(Id, chapterModel); Name = chapter.Name
};
}
private async void HandleValidSubmit()
{
await DataService.Update(Id, chapterModel);
var formData = new List<KeyValuePair<string, string>>(); var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("name", chapterModel.Name)); formData.Add(new KeyValuePair<string, string>("name", chapterModel.Name));
@ -47,6 +54,7 @@ public partial class EditChapter
{ {
var response = await httpClient.PostAsync(apiUri, formContent); var response = await httpClient.PostAsync(apiUri, formContent);
if (response.IsSuccessStatusCode) if (response.IsSuccessStatusCode)
{ {
var responseBody = await response.Content.ReadAsStringAsync(); var responseBody = await response.Content.ReadAsStringAsync();
@ -55,7 +63,9 @@ public partial class EditChapter
{ {
var errorResponse = await response.Content.ReadAsStringAsync(); var errorResponse = await response.Content.ReadAsStringAsync();
} }
} }
NavigationManager.NavigateTo("chapters");
} Logger.LogInformation("Chapter '{OldChapterModelName}' edited in '{NewChapterModelName}'", OldChapterName, chapterModel.Name);
} NavigationManager.NavigateTo("chapters");
}
}

@ -5,7 +5,7 @@
<h3>Chapters</h3> <h3>Chapters</h3>
<div> <div>
<NavLink class="btn btn-primary" href="addChapter" Match="NavLinkMatch.All"> <NavLink class="btn btn-primary" href="addQuestion" Match="NavLinkMatch.All">
<i class="fa fa-plus"></i> Ajouter <i class="fa fa-plus"></i> Ajouter
</NavLink> </NavLink>
</div> </div>

@ -7,6 +7,7 @@ using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using Blazored.Modal; using Blazored.Modal;
using Microsoft.Extensions.Logging;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -36,6 +37,8 @@ builder.Services.AddBlazoredModal();
builder.Services.AddScoped<IDataService, DataLocalService>(); builder.Services.AddScoped<IDataService, DataLocalService>();
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

@ -1,321 +1,321 @@
using Blazor.Models; using Blazor.Models;
using Blazor.ViewClasses; using Blazor.ViewClasses;
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using static System.Net.WebRequestMethods; using static System.Net.WebRequestMethods;
namespace Blazor.Services namespace Blazor.Services
{ {
public class DataLocalService : IDataService public class DataLocalService : IDataService
{ {
private readonly HttpClient _http; private readonly HttpClient _http;
private readonly ILocalStorageService _localStorage; private readonly ILocalStorageService _localStorage;
private readonly NavigationManager _navigationManager; private readonly NavigationManager _navigationManager;
private readonly IWebHostEnvironment _webHostEnvironment; private readonly IWebHostEnvironment _webHostEnvironment;
public DataLocalService( public DataLocalService(
ILocalStorageService localStorage, ILocalStorageService localStorage,
HttpClient http, HttpClient http,
IWebHostEnvironment webHostEnvironment, IWebHostEnvironment webHostEnvironment,
NavigationManager navigationManager) NavigationManager navigationManager)
{ {
_localStorage = localStorage; _localStorage = localStorage;
_http = http; _http = http;
_webHostEnvironment = webHostEnvironment; _webHostEnvironment = webHostEnvironment;
_navigationManager = navigationManager; _navigationManager = navigationManager;
} }
public async Task<Chapter> GetById(int id) public async Task<Chapter> GetById(int id)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data"); var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data");
// Get the chapter int the list // Get the chapter int the list
var chapter = currentData.FirstOrDefault(w => w.Id == id); var chapter = currentData.FirstOrDefault(w => w.Id == id);
// Check if chapter exist // Check if chapter exist
if (chapter == null) if (chapter == null)
{ {
throw new Exception($"Unable to found the item with ID: {id}"); throw new Exception($"Unable to found the item with ID: {id}");
} }
return chapter; return chapter;
} }
public async Task Update(int id, ChapterModel model) public async Task Update(int id, ChapterModel model)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data"); var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data");
// Get the chapter int the list // Get the chapter int the list
var chapter = currentData.FirstOrDefault(w => w.Id == id); var chapter = currentData.FirstOrDefault(w => w.Id == id);
// Check if chapter exist // Check if chapter exist
if (chapter == null) if (chapter == null)
{ {
throw new Exception($"Unable to found the item with ID: {id}"); throw new Exception($"Unable to found the item with ID: {id}");
} }
// Modify the content of the chapter // Modify the content of the chapter
chapter.Name = model.Name; chapter.Name = model.Name;
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task Add(ChapterModel model) public async Task Add(ChapterModel model)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data"); var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data");
// Simulate the Id // Simulate the Id
model.Id = currentData.Max(s => s.Id) + 1; model.Id = currentData.Max(s => s.Id) + 1;
// Add the chapter to the current data // Add the chapter to the current data
currentData.Add(new Chapter currentData.Add(new Chapter
{ {
Id = model.Id, Id = model.Id,
Name = model.Name Name = model.Name
}); });
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task Delete(int id) public async Task Delete(int id)
{ {
// Get the current data // Get the current data
//var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data"); //var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data");
var currentData = _http.GetFromJsonAsync<List<Chapter>>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; var currentData = _http.GetFromJsonAsync<List<Chapter>>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
// Get the chapter int the list // Get the chapter int the list
var chapter = currentData.FirstOrDefault(w => w.Id == id); var chapter = currentData.FirstOrDefault(w => w.Id == id);
// Delete chapter in // Delete chapter in
currentData.Remove(chapter); currentData.Remove(chapter);
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task<int> Count() public async Task<int> Count()
{ {
// Load data from the local storage // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Chapter[]>("data"); var currentData = await _localStorage.GetItemAsync<Chapter[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Chapter[]>($"{_navigationManager.BaseUri}fake-data.json"); var originalData = await _http.GetFromJsonAsync<Chapter[]>($"{_navigationManager.BaseUri}fake-data.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Chapter[]>("data")).Length; return (await _localStorage.GetItemAsync<Chapter[]>("data")).Length;
} }
public async Task<List<Chapter>> List(int currentPage, int pageSize) public async Task<List<Chapter>> List(int currentPage, int pageSize)
{ {
// Load data from the local storage // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Chapter[]>("data"); var currentData = await _localStorage.GetItemAsync<Chapter[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Chapter[]>($"{_navigationManager.BaseUri}fake-data.json"); var originalData = await _http.GetFromJsonAsync<Chapter[]>($"{_navigationManager.BaseUri}fake-data.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Chapter[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); return (await _localStorage.GetItemAsync<Chapter[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
} }
public async Task<Administrator> GetAdminById(int id) public async Task<Administrator> GetAdminById(int id)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data"); var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
// Get the admin int the list // Get the admin int the list
var admin = currentData.FirstOrDefault(w => w.Id == id); var admin = currentData.FirstOrDefault(w => w.Id == id);
// Check if admin exist // Check if admin exist
if (admin == null) if (admin == null)
{ {
throw new Exception($"Unable to found the item with ID: {id}"); throw new Exception($"Unable to found the item with ID: {id}");
} }
return admin; return admin;
} }
public async Task Update(int id, AdministratorModel model) public async Task Update(int id, AdministratorModel model)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data"); var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
// Get the admin int the list // Get the admin int the list
var admin = currentData.FirstOrDefault(w => w.Id == id); var admin = currentData.FirstOrDefault(w => w.Id == id);
// Check if admin exist // Check if admin exist
if (admin == null) if (admin == null)
{ {
throw new Exception($"Unable to found the item with ID: {id}"); throw new Exception($"Unable to found the item with ID: {id}");
} }
// Modify the content of the adminnistrator // Modify the content of the adminnistrator
admin.Username = model.Username; admin.Username = model.Username;
admin.HashedPassword = model.HashedPassword; admin.HashedPassword = model.HashedPassword;
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task Add(AdministratorModel model) public async Task Add(AdministratorModel model)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data"); var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
// Simulate the Id // Simulate the Id
model.Id = currentData.Max(s => s.Id) + 1; model.Id = currentData.Max(s => s.Id) + 1;
// Add the admin to the current data // Add the admin to the current data
currentData.Add(new Administrator currentData.Add(new Administrator
{ {
Id = model.Id, Id = model.Id,
Username = model.Username, Username = model.Username,
HashedPassword = model.HashedPassword HashedPassword = model.HashedPassword
}); });
// Save the data // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);
} }
public async Task<int> CountAdmin() public async Task<int> CountAdmin()
{ {
// Load data from the local storage // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Administrator[]>("data"); var currentData = await _localStorage.GetItemAsync<Administrator[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Administrator[]>($"{_navigationManager.BaseUri}fake-administrator.json"); var originalData = await _http.GetFromJsonAsync<Administrator[]>($"{_navigationManager.BaseUri}fake-administrator.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Administrator[]>("data")).Length; return (await _localStorage.GetItemAsync<Administrator[]>("data")).Length;
} }
public async Task<List<Administrator>> ListAdmin(int currentPage, int pageSize) public async Task<List<Administrator>> ListAdmin(int currentPage, int pageSize)
{ {
// Load data from the local storage // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Administrator[]>("data"); var currentData = await _localStorage.GetItemAsync<Administrator[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Administrator[]>($"{_navigationManager.BaseUri}fake-administrator.json"); var originalData = await _http.GetFromJsonAsync<Administrator[]>($"{_navigationManager.BaseUri}fake-administrator.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Administrator[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); return (await _localStorage.GetItemAsync<Administrator[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
} }
public async Task<Question> GetQuestionById(int id) public async Task<Question> GetQuestionById(int id)
{ {
// Get the current data // Get the current data
var currentData = await _localStorage.GetItemAsync<List<Question>>("data"); var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// Get the question int the list // Get the question int the list
var question = currentData.FirstOrDefault(w => w.Id == id); var question = currentData.FirstOrDefault(w => w.Id == id);
// Check if question exist // Check if question exist
if (question == null) if (question == null)
{ {
throw new Exception($"Unable to found the item with ID: {id}"); throw new Exception($"Unable to found the item with ID: {id}");
} }
return question; return question;
} }
//public async Task Update(int id, QuestionsModel model) //public async Task Update(int id, QuestionsModel model)
//{ //{
// // Get the current data // // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data"); // var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// // Get the admin int the list // // Get the admin int the list
// var question = currentData.FirstOrDefault(w => w.Id == id); // var question = currentData.FirstOrDefault(w => w.Id == id);
// // Check if admin exist // // Check if admin exist
// if (question == null) // if (question == null)
// { // {
// throw new Exception($"Unable to found the item with ID: {id}"); // throw new Exception($"Unable to found the item with ID: {id}");
// } // }
// // Modify the content of the adminnistrator // // Modify the content of the adminnistrator
// question.Username = model.Username; // question.Username = model.Username;
// question.HashedPassword = model.HashedPassword; // question.HashedPassword = model.HashedPassword;
// // Save the data // // Save the data
// await _localStorage.SetItemAsync("data", currentData); // await _localStorage.SetItemAsync("data", currentData);
//} //}
//public async Task Add(QuestionsModel model) //public async Task Add(QuestionsModel model)
//{ //{
// // Get the current data // // Get the current data
// var currentData = await _localStorage.GetItemAsync<List<Question>>("data"); // var currentData = await _localStorage.GetItemAsync<List<Question>>("data");
// // Simulate the Id // // Simulate the Id
// model.Id = currentData.Max(s => s.Id) + 1; // model.Id = currentData.Max(s => s.Id) + 1;
// // Add the admin to the current data // // Add the admin to the current data
// currentData.Add(new Question // currentData.Add(new Question
// { // {
// Id = model.Id, // Id = model.Id,
// Username = model.Username, // Username = model.Username,
// HashedPassword = model.HashedPassword // HashedPassword = model.HashedPassword
// }); // });
// // Save the data // // Save the data
// await _localStorage.SetItemAsync("data", currentData); // await _localStorage.SetItemAsync("data", currentData);
//} //}
public async Task<int> CountQuestion() public async Task<int> CountQuestion()
{ {
// Load data from the local storage // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Question[]>("data"); var currentData = await _localStorage.GetItemAsync<Question[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Question[]>($"{_navigationManager.BaseUri}fake-question.json"); var originalData = await _http.GetFromJsonAsync<Question[]>($"{_navigationManager.BaseUri}fake-question.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Question[]>("data")).Length; return (await _localStorage.GetItemAsync<Question[]>("data")).Length;
} }
public async Task<List<Question>> ListQuestion(int currentPage, int pageSize) public async Task<List<Question>> ListQuestion(int currentPage, int pageSize)
{ {
// Load data from the local storage // Load data from the local storage
var currentData = await _localStorage.GetItemAsync<Question[]>("data"); var currentData = await _localStorage.GetItemAsync<Question[]>("data");
// Check if data exist in the local storage // Check if data exist in the local storage
if (currentData == null) if (currentData == null)
{ {
// this code add in the local storage the fake data // this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<Question[]>($"{_navigationManager.BaseUri}fake-question.json"); var originalData = await _http.GetFromJsonAsync<Question[]>($"{_navigationManager.BaseUri}fake-question.json");
await _localStorage.SetItemAsync("data", originalData); await _localStorage.SetItemAsync("data", originalData);
} }
return (await _localStorage.GetItemAsync<Question[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); return (await _localStorage.GetItemAsync<Question[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
} }

@ -2,8 +2,8 @@
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft.AspNetCore": "Warning" "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
} }
}, }
"AllowedHosts": "*" }
}
Loading…
Cancel
Save