diff --git a/Blazor/Blazor.sln b/Blazor/Blazor.sln
index f68a3ac..2c17fbd 100644
--- a/Blazor/Blazor.sln
+++ b/Blazor/Blazor.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazor", "Blazor\Blazor.csproj", "{F9B19564-ED8F-49F7-97D7-2132F92DE3C2}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor", "Blazor\Blazor.csproj", "{F9B19564-ED8F-49F7-97D7-2132F92DE3C2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/Blazor/Blazor/Models/ChapterModel.cs b/Blazor/Blazor/Models/ChapterModel.cs
new file mode 100644
index 0000000..44915a5
--- /dev/null
+++ b/Blazor/Blazor/Models/ChapterModel.cs
@@ -0,0 +1,13 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Blazor.Models
+{
+ public class ChapterModel
+ {
+ public int Id { get; set; }
+
+ [Required(ErrorMessage = "Name is required")]
+ [StringLength(50, ErrorMessage = "Name is too long.")]
+ public string Name { get; set; }
+ }
+}
diff --git a/Blazor/Blazor/Pages/AddChapter.razor b/Blazor/Blazor/Pages/AddChapter.razor
new file mode 100644
index 0000000..3be1097
--- /dev/null
+++ b/Blazor/Blazor/Pages/AddChapter.razor
@@ -0,0 +1,20 @@
+@page "/addChapter"
+@using Blazor.Models
+
+
Add Chapter
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Blazor/Blazor/Pages/AddChapter.razor.cs b/Blazor/Blazor/Pages/AddChapter.razor.cs
new file mode 100644
index 0000000..20e75cf
--- /dev/null
+++ b/Blazor/Blazor/Pages/AddChapter.razor.cs
@@ -0,0 +1,28 @@
+using Blazored.LocalStorage;
+using Microsoft.AspNetCore.Components.Forms;
+using Microsoft.AspNetCore.Components;
+using Blazor.Models;
+using Blazor.Services;
+
+namespace Blazor.Pages
+{
+ public partial class AddChapter
+ {
+ private ChapterModel chapterModel = new();
+
+ [Inject]
+ public IDataService DataService { get; set; }
+
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
+
+
+ private async void HandleValidSubmit()
+ {
+ await DataService.Add(chapterModel);
+
+ NavigationManager.NavigateTo("chapters");
+ }
+
+ }
+}
diff --git a/Blazor/Blazor/Pages/Chapters.razor b/Blazor/Blazor/Pages/Chapters.razor
index bdf3267..5504b64 100644
--- a/Blazor/Blazor/Pages/Chapters.razor
+++ b/Blazor/Blazor/Pages/Chapters.razor
@@ -3,6 +3,12 @@
@using Blazorise.DataGrid
Chapters
+
+
+ Ajouter
+
+
+
+
+
+ Editer
+
+
diff --git a/Blazor/Blazor/Pages/Chapters.razor.cs b/Blazor/Blazor/Pages/Chapters.razor.cs
index a893535..2dd498f 100644
--- a/Blazor/Blazor/Pages/Chapters.razor.cs
+++ b/Blazor/Blazor/Pages/Chapters.razor.cs
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Components;
using Blazor.Models;
using Blazorise.DataGrid;
+using Blazored.LocalStorage;
+using Blazorise;
+using Blazor.Services;
namespace Blazor.Pages
{
@@ -11,10 +14,10 @@ namespace Blazor.Pages
private int totalItem;
[Inject]
- public HttpClient Http { get; set; }
+ public IDataService DataService { get; set; }
[Inject]
- public NavigationManager NavigationManager { get; set; }
+ public IWebHostEnvironment WebHostEnvironment { get; set; }
private async Task OnReadData(DataGridReadDataEventArgs e)
{
@@ -23,14 +26,10 @@ namespace Blazor.Pages
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-data.json")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
-
if (!e.CancellationToken.IsCancellationRequested)
{
- totalItem = (await Http.GetFromJsonAsync
>($"{NavigationManager.BaseUri}fake-data.json")).Count;
- chapters = new List(response); // an actual data for the current page
+ chapters = await DataService.List(e.Page, e.PageSize);
+ totalItem = await DataService.Count();
}
}
}
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 f886838..2d9e404 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;
@@ -22,6 +23,8 @@ builder.Services
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..37c6ea2
--- /dev/null
+++ b/Blazor/Blazor/Services/DataLocalService.cs
@@ -0,0 +1,118 @@
+using Blazor.Models;
+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..5a6bd6c
--- /dev/null
+++ b/Blazor/Blazor/Services/IDataService.cs
@@ -0,0 +1,17 @@
+using Blazor.Models;
+
+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);
+ }
+}