+
+
+
+
+
+
diff --git a/Blazor/Blazor/Pages/Administrators.razor.cs b/Blazor/Blazor/Pages/Administrators.razor.cs
new file mode 100644
index 0000000..82fb218
--- /dev/null
+++ b/Blazor/Blazor/Pages/Administrators.razor.cs
@@ -0,0 +1,37 @@
+using Microsoft.AspNetCore.Components;
+using Blazorise.DataGrid;
+using Blazor.ViewClasses;
+
+namespace Blazor.Pages
+{
+ public partial class Administrators
+ {
+ public List administrators;
+
+ private int totalItem;
+
+ [Inject]
+ public HttpClient Http { get; set; }
+
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
+
+ private async Task OnReadData(DataGridReadDataEventArgs e)
+ {
+ if (e.CancellationToken.IsCancellationRequested)
+ {
+ return;
+ }
+
+ // When you use a real API, we use this follow code
+ //var response = await Http.GetJsonAsync( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
+ var response = (await Http.GetFromJsonAsync($"{NavigationManager.BaseUri}fake-administrator.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
+
+ if (!e.CancellationToken.IsCancellationRequested)
+ {
+ totalItem = (await Http.GetFromJsonAsync>($"{NavigationManager.BaseUri}fake-administrator.json")).Count;
+ administrators = new List(response); // an actual data for the current page
+ }
+ }
+ }
+}
diff --git a/Blazor/Blazor/Pages/Chapters.razor b/Blazor/Blazor/Pages/Chapters.razor
index d9df7c8..f705ba1 100644
--- a/Blazor/Blazor/Pages/Chapters.razor
+++ b/Blazor/Blazor/Pages/Chapters.razor
@@ -1,16 +1,30 @@
@page "/chapters"
-@using Blazor.Models
-
Liste
-
-Chapters
+
+
+
+ Ajouter
+
+
+ Exporter
+
+
+
+
-
-
-
-
+
+
+
+
+ Editer
+
+
+
\ No newline at end of file
diff --git a/Blazor/Blazor/Pages/Chapters.razor.cs b/Blazor/Blazor/Pages/Chapters.razor.cs
index cc65a84..be82912 100644
--- a/Blazor/Blazor/Pages/Chapters.razor.cs
+++ b/Blazor/Blazor/Pages/Chapters.razor.cs
@@ -1,22 +1,25 @@
-using Blazor.Models;
-using Blazored.LocalStorage;
+using Blazored.LocalStorage;
+using Blazorise;
+using Blazor.Services;
using Blazorise.DataGrid;
using ChoETL;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Text;
+using Blazor.ViewClasses;
namespace Blazor.Pages;
public partial class Chapters
-{
- private List chapters;
+ {
+ public List chapters;
private int totalChapter;
- [Inject]
+ [Inject]
+ public IDataService DataService { get; set; }
public IWebHostEnvironment WebHostEnvironment { get; set; }
- [Inject]
+ [Inject]
public HttpClient Http { get; set; }
[Inject]
@@ -36,32 +39,32 @@ public partial class Chapters
return;
}
- var currentData = await LocalStorage.GetItemAsync("data");
+ 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 (we load the data sync for initialize the data before load the OnReadData method)
- var originalData = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
+ var originalData = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
await LocalStorage.SetItemAsync("data", originalData);
}
}
- private async Task OnReadData(DataGridReadDataEventArgs e)
- {
- if (e.CancellationToken.IsCancellationRequested)
+ private async Task OnReadData(DataGridReadDataEventArgs e)
{
- return;
- }
+ if (e.CancellationToken.IsCancellationRequested)
+ {
+ return;
+ }
// When you use a real API, we use this follow code
//var response = await Http.GetFromJsonAsync( $"https://trusting-panini.87-106-126-109.plesk.page/api/chapters?page={e.Page}&pageSize={e.PageSize}" );
- var response = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
+ var response = Http.GetFromJsonAsync($"https://trusting-panini.87-106-126-109.plesk.page/api/chapters").Result;
- if (!e.CancellationToken.IsCancellationRequested)
- {
- totalChapter = (await LocalStorage.GetItemAsync>("data")).Count;
- chapters = new List(response); // an actual data for the current page
+ if (!e.CancellationToken.IsCancellationRequested)
+ {
+ totalChapter = (await LocalStorage.GetItemAsync>("data")).Count;
+ chapters = new List(response); // an actual data for the current page
}
}
@@ -76,7 +79,7 @@ public partial class Chapters
{
csvFile.Write(jsonFile);
}
- }
+ }
var sentFile = new MemoryStream(Encoding.UTF32.GetBytes(sb.ToString()));
@@ -85,4 +88,4 @@ public partial class Chapters
await IJSRuntime.InvokeVoidAsync("downloadFileFromStream", "data.csv", streamRef);
}
}
-}
\ No newline at end of file
+}
diff --git a/Blazor/Blazor/Pages/EditChapter.razor b/Blazor/Blazor/Pages/EditChapter.razor
new file mode 100644
index 0000000..c1f51ba
--- /dev/null
+++ b/Blazor/Blazor/Pages/EditChapter.razor
@@ -0,0 +1,18 @@
+@page "/editChapter/{Id:int}"
+
+
Edit Chapter
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Blazor/Blazor/Pages/EditChapter.razor.cs b/Blazor/Blazor/Pages/EditChapter.razor.cs
new file mode 100644
index 0000000..52af970
--- /dev/null
+++ b/Blazor/Blazor/Pages/EditChapter.razor.cs
@@ -0,0 +1,42 @@
+using Blazor.Models;
+using Blazor.Services;
+using Microsoft.AspNetCore.Components;
+
+namespace Blazor.Pages
+{
+ 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; }
+
+ protected override async Task OnInitializedAsync()
+ {
+ var chapter = await DataService.GetById(Id);
+
+ // Set the model with the chapter
+ chapterModel = new ChapterModel
+ {
+ Id = chapter.Id,
+ Name = chapter.Name
+ };
+ }
+
+ private async void HandleValidSubmit()
+ {
+ await DataService.Update(Id, chapterModel);
+
+ NavigationManager.NavigateTo("chapters");
+ }
+ }
+}
diff --git a/Blazor/Blazor/Program.cs b/Blazor/Blazor/Program.cs
index be52cc7..5c73e4b 100644
--- a/Blazor/Blazor/Program.cs
+++ b/Blazor/Blazor/Program.cs
@@ -1,4 +1,5 @@
using Blazor.Data;
+using Blazor.Services;
using Blazored.LocalStorage;
using Blazorise;
using Blazorise.Bootstrap;
@@ -20,6 +21,17 @@ builder.Services
.AddFontAwesomeIcons();
builder.Services.AddBlazoredLocalStorage();
+builder.Services.AddHttpClient();
+
+builder.Services
+ .AddBlazorise()
+ .AddBootstrapProviders()
+ .AddFontAwesomeIcons();
+
+builder.Services.AddBlazoredLocalStorage();
+
+builder.Services.AddScoped();
+
var app = builder.Build();
// Configure the HTTP request pipeline.
diff --git a/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs
new file mode 100644
index 0000000..6a4c64a
--- /dev/null
+++ b/Blazor/Blazor/Services/DataLocalService.cs
@@ -0,0 +1,119 @@
+using Blazor.Models;
+using Blazor.ViewClasses;
+using Blazored.LocalStorage;
+using Microsoft.AspNetCore.Components;
+
+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 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();
+ }
+ }
+}
diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs
new file mode 100644
index 0000000..580e304
--- /dev/null
+++ b/Blazor/Blazor/Services/IDataService.cs
@@ -0,0 +1,18 @@
+using Blazor.Models;
+using Blazor.ViewClasses;
+
+namespace Blazor.Services
+{
+ public interface IDataService
+ {
+ Task Add(ChapterModel model);
+
+ Task Count();
+
+ Task> List(int currentPage, int pageSize);
+
+ Task GetById(int id);
+
+ Task Update(int id, ChapterModel model);
+ }
+}
diff --git a/Blazor/Blazor/Shared/NavMenu.razor b/Blazor/Blazor/Shared/NavMenu.razor
index 617155c..6c502bb 100644
--- a/Blazor/Blazor/Shared/NavMenu.razor
+++ b/Blazor/Blazor/Shared/NavMenu.razor
@@ -15,18 +15,18 @@