diff --git a/Blazor/Blazor/Pages/Admins/EditAdministrator.razor.cs b/Blazor/Blazor/Pages/Admins/EditAdministrator.razor.cs index ed2578f..370744f 100644 --- a/Blazor/Blazor/Pages/Admins/EditAdministrator.razor.cs +++ b/Blazor/Blazor/Pages/Admins/EditAdministrator.razor.cs @@ -45,7 +45,7 @@ namespace Blazor.Pages.Admins await DataService.Update(Id, administratorModel); - Logger.LogInformation("Admin '{OldAdminModelName}' edited in '{NewAdminModelName}'",OldAdminName,administratorsModel.Username); + Logger.LogInformation("Admin '{OldAdminModelName}' edited in '{NewAdminModelName}'",OldAdminName,administratorModel.Username); NavigationManager.NavigateTo("administrators"); } diff --git a/Blazor/Blazor/Pages/Chapters/EditChapter.razor.cs b/Blazor/Blazor/Pages/Chapters/EditChapter.razor.cs index 4796468..e93875e 100644 --- a/Blazor/Blazor/Pages/Chapters/EditChapter.razor.cs +++ b/Blazor/Blazor/Pages/Chapters/EditChapter.razor.cs @@ -1,47 +1,47 @@ -using Blazor.Models; -using Blazor.Services; -using Microsoft.AspNetCore.Components; - -namespace Blazor.Pages.Chapters; - -public partial class EditChapter -{ - [Parameter] - public int Id { get; set; } - - private ChapterModel chapterModel = new(); - - [Inject] - public IDataService DataService { get; set; } - - [Inject] - public NavigationManager NavigationManager { get; set; } - - [Inject] - public IWebHostEnvironment WebHostEnvironment { get; set; } - - [Inject] - public ILogger Logger { get; set; } - - private string OldChapterName { get; set; } - - protected override async Task OnInitializedAsync() - { - var chapter = await DataService.GetById(Id); - OldChapterName = chapter.Name; - - // Set the model with the chapter - chapterModel = new ChapterModel - { - Id = chapter.Id, - Name = chapter.Name - }; - } - - private async void HandleValidSubmit() - { - await DataService.Update(Id, chapterModel); - +using Blazor.Models; +using Blazor.Services; +using Microsoft.AspNetCore.Components; + +namespace Blazor.Pages.Chapters; + +public partial class EditChapter +{ + [Parameter] + public int Id { get; set; } + + private ChapterModel chapterModel = new(); + + [Inject] + public IDataService DataService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + [Inject] + public IWebHostEnvironment WebHostEnvironment { get; set; } + + [Inject] + public ILogger Logger { get; set; } + + private string OldChapterName { get; set; } + + protected override async Task OnInitializedAsync() + { + var chapter = await DataService.GetById(Id); + OldChapterName = chapter.Name; + + // Set the model with the chapter + chapterModel = new ChapterModel + { + Id = chapter.Id, + Name = chapter.Name + }; + } + + private async void HandleValidSubmit() + { + await DataService.Update(Id, chapterModel); + var formData = new List>(); formData.Add(new KeyValuePair("name", chapterModel.Name)); @@ -54,7 +54,7 @@ public partial class EditChapter { var response = await httpClient.PostAsync(apiUri, formContent); - + if (response.IsSuccessStatusCode) { var responseBody = await response.Content.ReadAsStringAsync(); @@ -63,9 +63,9 @@ public partial class EditChapter { var errorResponse = await response.Content.ReadAsStringAsync(); } - } - - Logger.LogInformation("Chapter '{OldChapterModelName}' edited in '{NewChapterModelName}'", OldChapterName, chapterModel.Name); - NavigationManager.NavigateTo("chapters"); - } -} + } + + Logger.LogInformation("Chapter '{OldChapterModelName}' edited in '{NewChapterModelName}'", OldChapterName, chapterModel.Name); + NavigationManager.NavigateTo("chapters"); + } +} diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index f09b693..d8b91cf 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -1,321 +1,321 @@ -using Blazor.Models; -using Blazor.ViewClasses; -using Blazored.LocalStorage; -using Microsoft.AspNetCore.Components; +using Blazor.Models; +using Blazor.ViewClasses; +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; using static System.Net.WebRequestMethods; - -namespace Blazor.Services -{ - public class DataLocalService : IDataService - { - private readonly HttpClient _http; - private readonly ILocalStorageService _localStorage; - private readonly NavigationManager _navigationManager; - private readonly IWebHostEnvironment _webHostEnvironment; - - public DataLocalService( - ILocalStorageService localStorage, - HttpClient http, - IWebHostEnvironment webHostEnvironment, - NavigationManager navigationManager) - { - _localStorage = localStorage; - _http = http; - _webHostEnvironment = webHostEnvironment; - _navigationManager = navigationManager; - } - - public async Task GetById(int id) - { - // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); - - // Get the chapter int the list - var chapter = currentData.FirstOrDefault(w => w.Id == id); - - // Check if chapter exist - if (chapter == null) - { - throw new Exception($"Unable to found the item with ID: {id}"); - } - - return chapter; - } - - public async Task Update(int id, ChapterModel model) - { - // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); - - // Get the chapter int the list - var chapter = currentData.FirstOrDefault(w => w.Id == id); - - // Check if chapter exist - if (chapter == null) - { - throw new Exception($"Unable to found the item with ID: {id}"); - } - - // Modify the content of the chapter - chapter.Name = model.Name; - - // Save the data - await _localStorage.SetItemAsync("data", currentData); - } - - - public async Task Add(ChapterModel 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 Chapter - { - Id = model.Id, - Name = model.Name - }); - - - // Save the data - await _localStorage.SetItemAsync("data", currentData); - } - - public async Task Delete(int id) - { - // Get the current data - //var currentData = await _localStorage.GetItemAsync>("data"); - var currentData = _http.GetFromJsonAsync>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; - - // Get the chapter int the list - var chapter = currentData.FirstOrDefault(w => w.Id == id); - - // Delete chapter in - currentData.Remove(chapter); - - // Save the data - await _localStorage.SetItemAsync("data", currentData); - } - - public async Task Count() - { - // 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-data.json"); - await _localStorage.SetItemAsync("data", originalData); - } - - return (await _localStorage.GetItemAsync("data")).Length; - } - - public async Task> List(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-data.json"); - await _localStorage.SetItemAsync("data", originalData); - } - - return (await _localStorage.GetItemAsync("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 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}"); - } - - return admin; - } - - public async Task Update(int id, AdministratorModel 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(AdministratorModel model) - { - // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); - - // Simulate the Id - model.Id = currentData.Max(s => s.Id) + 1; - - // Add the admin 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(); - } - - - public async Task GetQuestionById(int id) - { - // Get the current data - var currentData = await _localStorage.GetItemAsync>("data"); - - // Get the question int the list - var question = currentData.FirstOrDefault(w => w.Id == id); - - // Check if question exist - if (question == null) - { - throw new Exception($"Unable to found the item with ID: {id}"); - } - - return question; - } - - //public async Task Update(int id, QuestionsModel model) - //{ - // // Get the current data - // var currentData = await _localStorage.GetItemAsync>("data"); - - // // Get the admin int the list - // var question = currentData.FirstOrDefault(w => w.Id == id); - - // // Check if admin exist - // if (question == null) - // { - // throw new Exception($"Unable to found the item with ID: {id}"); - // } - - // // Modify the content of the adminnistrator - // question.Username = model.Username; + +namespace Blazor.Services +{ + public class DataLocalService : IDataService + { + private readonly HttpClient _http; + private readonly ILocalStorageService _localStorage; + private readonly NavigationManager _navigationManager; + private readonly IWebHostEnvironment _webHostEnvironment; + + public DataLocalService( + ILocalStorageService localStorage, + HttpClient http, + IWebHostEnvironment webHostEnvironment, + NavigationManager navigationManager) + { + _localStorage = localStorage; + _http = http; + _webHostEnvironment = webHostEnvironment; + _navigationManager = navigationManager; + } + + public async Task GetById(int id) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the chapter int the list + var chapter = currentData.FirstOrDefault(w => w.Id == id); + + // Check if chapter exist + if (chapter == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + return chapter; + } + + public async Task Update(int id, ChapterModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the chapter int the list + var chapter = currentData.FirstOrDefault(w => w.Id == id); + + // Check if chapter exist + if (chapter == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + // Modify the content of the chapter + chapter.Name = model.Name; + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + + public async Task Add(ChapterModel 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 Chapter + { + Id = model.Id, + Name = model.Name + }); + + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + public async Task Delete(int id) + { + // Get the current data + //var currentData = await _localStorage.GetItemAsync>("data"); + var currentData = _http.GetFromJsonAsync>($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result; + + // Get the chapter int the list + var chapter = currentData.FirstOrDefault(w => w.Id == id); + + // Delete chapter in + currentData.Remove(chapter); + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + + public async Task Count() + { + // 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-data.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Length; + } + + public async Task> List(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-data.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("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 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}"); + } + + return admin; + } + + public async Task Update(int id, AdministratorModel 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(AdministratorModel model) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Simulate the Id + model.Id = currentData.Max(s => s.Id) + 1; + + // Add the admin 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(); + } + + + public async Task GetQuestionById(int id) + { + // Get the current data + var currentData = await _localStorage.GetItemAsync>("data"); + + // Get the question int the list + var question = currentData.FirstOrDefault(w => w.Id == id); + + // Check if question exist + if (question == null) + { + throw new Exception($"Unable to found the item with ID: {id}"); + } + + return question; + } + + //public async Task Update(int id, QuestionsModel model) + //{ + // // Get the current data + // var currentData = await _localStorage.GetItemAsync>("data"); + + // // Get the admin int the list + // var question = currentData.FirstOrDefault(w => w.Id == id); + + // // Check if admin exist + // if (question == null) + // { + // throw new Exception($"Unable to found the item with ID: {id}"); + // } + + // // Modify the content of the adminnistrator + // question.Username = model.Username; // question.HashedPassword = model.HashedPassword; - - // // Save the data - // await _localStorage.SetItemAsync("data", currentData); - //} - - //public async Task Add(QuestionsModel model) - //{ - // // Get the current data - // var currentData = await _localStorage.GetItemAsync>("data"); - - // // Simulate the Id - // model.Id = currentData.Max(s => s.Id) + 1; - - // // Add the admin to the current data - // currentData.Add(new Question - // { - // Id = model.Id, - // Username = model.Username, + + // // Save the data + // await _localStorage.SetItemAsync("data", currentData); + //} + + //public async Task Add(QuestionsModel model) + //{ + // // Get the current data + // var currentData = await _localStorage.GetItemAsync>("data"); + + // // Simulate the Id + // model.Id = currentData.Max(s => s.Id) + 1; + + // // Add the admin to the current data + // currentData.Add(new Question + // { + // Id = model.Id, + // Username = model.Username, // HashedPassword = model.HashedPassword - // }); - - - // // Save the data - // await _localStorage.SetItemAsync("data", currentData); - //} - - public async Task CountQuestion() - { - // 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-question.json"); - await _localStorage.SetItemAsync("data", originalData); - } - - return (await _localStorage.GetItemAsync("data")).Length; - } - - public async Task> ListQuestion(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-question.json"); - await _localStorage.SetItemAsync("data", originalData); - } - - return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); - } + // }); + + + // // Save the data + // await _localStorage.SetItemAsync("data", currentData); + //} + + public async Task CountQuestion() + { + // 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-question.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Length; + } + + public async Task> ListQuestion(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-question.json"); + await _localStorage.SetItemAsync("data", originalData); + } + + return (await _localStorage.GetItemAsync("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList(); + }