diff --git a/ValblazeProject/Pages/Add.razor.cs b/ValblazeProject/Pages/Add.razor.cs
index 7e0ad07..5e3586d 100644
--- a/ValblazeProject/Pages/Add.razor.cs
+++ b/ValblazeProject/Pages/Add.razor.cs
@@ -2,30 +2,17 @@
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
using ValblazeProject.Models;
+using ValblazeProject.Services;
namespace ValblazeProject.Pages
{
public partial class Add
{
- [Inject]
- public ILocalStorageService LocalStorage { get; set; }
-
- [Inject]
- public NavigationManager NavigationManager { get; set; }
-
- [Inject]
- public IWebHostEnvironment WebHostEnvironment { get; set; }
-
///
/// The default enchant categories.
///
private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" };
- ///
- /// The default repair with.
- ///
- private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" };
-
///
/// The current item model
///
@@ -35,44 +22,20 @@ namespace ValblazeProject.Pages
RepairWith = new List()
};
- private async void HandleValidSubmit()
- {
- // Get the current data
- var currentData = await LocalStorage.GetItemAsync>("data");
-
- // Simulate the Id
- itemModel.Id = currentData.Max(s => s.Id) + 1;
-
- // Add the item to the current data
- currentData.Add(new Item
- {
- Id = itemModel.Id,
- DisplayName = itemModel.DisplayName,
- Name = itemModel.Name,
- RepairWith = itemModel.RepairWith,
- EnchantCategories = itemModel.EnchantCategories,
- MaxDurability = itemModel.MaxDurability,
- StackSize = itemModel.StackSize,
- CreatedDate = DateTime.Now
- });
-
- // Save the image
- var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images");
-
- // Check if the folder "images" exist
- if (!imagePathInfo.Exists)
- {
- imagePathInfo.Create();
- }
+ ///
+ /// The default repair with.
+ ///
+ private List repairWith = new List() { "oak_planks", "spruce_planks", "birch_planks", "jungle_planks", "acacia_planks", "dark_oak_planks", "crimson_planks", "warped_planks" };
- // Determine the image name
- var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
+ [Inject]
+ public IDataService DataService { get; set; }
- // Write the file content
- await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
+ [Inject]
+ public NavigationManager NavigationManager { get; set; }
- // Save the data
- await LocalStorage.SetItemAsync("data", currentData);
+ private async void HandleValidSubmit()
+ {
+ await DataService.Add(itemModel);
NavigationManager.NavigateTo("list");
}
diff --git a/ValblazeProject/Pages/List.razor.cs b/ValblazeProject/Pages/List.razor.cs
index c82ccf2..c770a89 100644
--- a/ValblazeProject/Pages/List.razor.cs
+++ b/ValblazeProject/Pages/List.razor.cs
@@ -2,6 +2,7 @@
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
using ValblazeProject.Models;
+using ValblazeProject.Services;
namespace ValblazeProject.Pages
{
@@ -12,36 +13,11 @@ namespace ValblazeProject.Pages
private int totalItem;
[Inject]
- public HttpClient Http { get; set; }
-
- [Inject]
- public ILocalStorageService LocalStorage { get; set; }
+ public IDataService DataService { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
- [Inject]
- public NavigationManager NavigationManager { get; set; }
-
- protected override async Task OnAfterRenderAsync(bool firstRender)
- {
- // Do not treat this action if is not the first render
- if (!firstRender)
- {
- return;
- }
-
- 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
- ($"{NavigationManager.BaseUri}fake-data.json").Result;
- await LocalStorage.SetItemAsync("data", originalData);
- }
- }
-
private async Task OnReadData(DataGridReadDataEventArgs
- e)
{
if (e.CancellationToken.IsCancellationRequested)
@@ -49,14 +25,10 @@ namespace ValblazeProject.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 LocalStorage.GetItemAsync
- ("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
-
if (!e.CancellationToken.IsCancellationRequested)
{
- totalItem = (await LocalStorage.GetItemAsync
>("data")).Count;
- items = new List- (response); // an actual data for the current page
+ items = await DataService.List(e.Page, e.PageSize);
+ totalItem = await DataService.Count();
}
}
}
diff --git a/ValblazeProject/Program.cs b/ValblazeProject/Program.cs
index c8b7485..0779dc9 100644
--- a/ValblazeProject/Program.cs
+++ b/ValblazeProject/Program.cs
@@ -5,10 +5,14 @@ using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using ValblazeProject.Data;
+using ValblazeProject.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
+
+builder.Services.AddScoped();
+
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton();
diff --git a/ValblazeProject/Services/DataLocalService.cs b/ValblazeProject/Services/DataLocalService.cs
new file mode 100644
index 0000000..03a50ef
--- /dev/null
+++ b/ValblazeProject/Services/DataLocalService.cs
@@ -0,0 +1,87 @@
+using Blazored.LocalStorage;
+using Microsoft.AspNetCore.Components;
+using ValblazeProject.Models;
+
+namespace ValblazeProject.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 Add(ItemModel model)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync
>("data");
+
+ // Simulate the Id
+ model.Id = currentData.Max(s => s.Id) + 1;
+
+ // Add the item to the current data
+ currentData.Add(new Item
+ {
+ Id = model.Id,
+ DisplayName = model.DisplayName,
+ Name = model.Name,
+ RepairWith = model.RepairWith,
+ EnchantCategories = model.EnchantCategories,
+ MaxDurability = model.MaxDurability,
+ StackSize = model.StackSize,
+ CreatedDate = DateTime.Now
+ });
+
+ // Save the image
+ var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
+
+ // Check if the folder "images" exist
+ if (!imagePathInfo.Exists)
+ {
+ imagePathInfo.Create();
+ }
+
+ // Determine the image name
+ var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
+
+ // Write the file content
+ await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
+
+ public async Task Count()
+ {
+ 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/ValblazeProject/Services/IDataService.cs b/ValblazeProject/Services/IDataService.cs
new file mode 100644
index 0000000..d7f645c
--- /dev/null
+++ b/ValblazeProject/Services/IDataService.cs
@@ -0,0 +1,13 @@
+using ValblazeProject.Models;
+
+namespace ValblazeProject.Services
+{
+ public interface IDataService
+ {
+ Task Add(ItemModel model);
+
+ Task Count();
+
+ Task
> List(int currentPage, int pageSize);
+ }
+}