@@ -19,9 +19,9 @@
- @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png"))
+ @if (!string.IsNullOrWhiteSpace(context.ImageBase64))
{
-
+
}
else
{
@@ -42,5 +42,11 @@
@(string.Join(", ", ((Item)context).RepairWith))
-
+
+
+
+ Editer
+
+
+
diff --git a/BlazorTp1/Pages/List.razor.cs b/BlazorTp1/Pages/List.razor.cs
index 4fd69ef..f20a1fb 100644
--- a/BlazorTp1/Pages/List.razor.cs
+++ b/BlazorTp1/Pages/List.razor.cs
@@ -2,6 +2,10 @@
using BlazorTp1.Models;
using Blazorise.DataGrid;
using Blazored.LocalStorage;
+using Blazored.Modal.Services;
+using Blazored.Modal;
+using BlazorTp1.Modals;
+using Microsoft.Extensions.Localization;
namespace BlazorTp1.Pages
{
@@ -12,7 +16,7 @@ namespace BlazorTp1.Pages
private int totalItem;
[Inject]
- public HttpClient Http { get; set; }
+ public IDataService DataService { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
@@ -20,6 +24,12 @@ namespace BlazorTp1.Pages
[Inject]
public NavigationManager NavigationManager { get; set; }
+ [CascadingParameter]
+ public IModalService Modal { get; set; }
+
+ [Inject]
+ public IStringLocalizer Localizer { get; set; }
+
private async Task OnReadData(DataGridReadDataEventArgs e)
{
if (e.CancellationToken.IsCancellationRequested)
@@ -27,15 +37,29 @@ namespace BlazorTp1.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;
- items = new List(response); // an actual data for the current page
+ items = await DataService.List(e.Page, e.PageSize);
+ totalItem = await DataService.Count();
}
}
+ private async void OnDelete(int id)
+ {
+ var parameters = new ModalParameters();
+ parameters.Add(nameof(Item.Id), id);
+
+ var modal = Modal.Show("Delete Confirmation", parameters);
+ var result = await modal.Result;
+
+ if (result.Cancelled)
+ {
+ return;
+ }
+
+ await DataService.Delete(id);
+
+ // Reload the page
+ NavigationManager.NavigateTo("list", true);
+ }
}
}
diff --git a/BlazorTp1/Pages/_Layout.cshtml b/BlazorTp1/Pages/_Layout.cshtml
index 641ca97..a2962dc 100644
--- a/BlazorTp1/Pages/_Layout.cshtml
+++ b/BlazorTp1/Pages/_Layout.cshtml
@@ -4,35 +4,38 @@
-
-
-
-
-
-
-
-
-
-
- @RenderBody()
+
+
+
+
+
+
+
+
+
+
+
+ @RenderBody()
-
-
- An error has occurred. This application may no longer respond until reloaded.
-
-
- An unhandled exception has occurred. See browser dev tools for details.
-
- Reload
- 🗙
-
+
+
+ An error has occurred. This application may no longer respond until reloaded.
+
+
+ An unhandled exception has occurred. See browser dev tools for details.
+
+ Reload
+ 🗙
+
-
+
+
+
-
+
-
-
+
+
-
+
diff --git a/BlazorTp1/Program.cs b/BlazorTp1/Program.cs
index 8d82ec6..a52bc75 100644
--- a/BlazorTp1/Program.cs
+++ b/BlazorTp1/Program.cs
@@ -1,10 +1,13 @@
using Blazored.LocalStorage;
+using Blazored.Modal;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using BlazorTp1.Data;
-using Microsoft.AspNetCore.Components;
-using Microsoft.AspNetCore.Components.Web;
+using BlazorTp1.Services;
+using Microsoft.AspNetCore.Localization;
+using Microsoft.Extensions.Options;
+using System.Globalization;
var builder = WebApplication.CreateBuilder(args);
@@ -15,6 +18,8 @@ builder.Services.AddSingleton();
builder.Services.AddHttpClient();
+builder.Services.AddBlazoredModal();
+
builder.Services
.AddBlazorise()
.AddBootstrapProviders()
@@ -22,6 +27,29 @@ builder.Services
builder.Services.AddBlazoredLocalStorage();
+builder.Services.AddScoped();
+
+builder.Services.AddScoped();
+
+// Add the controller of the app
+builder.Services.AddControllers();
+
+// Add the localization to the app and specify the resources path
+builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
+
+builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
+
+// Configure the localtization
+builder.Services.Configure(options =>
+{
+ // Set the default culture of the web site
+ options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en-US"));
+
+ // Declare the supported culture
+ options.SupportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") };
+ options.SupportedUICultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR") };
+});
+
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -38,6 +66,21 @@ app.UseStaticFiles();
app.UseRouting();
+// Get the current localization options
+var options = ((IApplicationBuilder)app).ApplicationServices.GetService>();
+
+if (options?.Value != null)
+{
+ // use the default localization
+ app.UseRequestLocalization(options.Value);
+}
+
+// Add the controller to the endpoint
+app.UseEndpoints(endpoints =>
+{
+ endpoints.MapControllers();
+});
+
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
diff --git a/BlazorTp1/Resources/Pages.List.fr-FR.resx b/BlazorTp1/Resources/Pages.List.fr-FR.resx
new file mode 100644
index 0000000..941e1c7
--- /dev/null
+++ b/BlazorTp1/Resources/Pages.List.fr-FR.resx
@@ -0,0 +1,123 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ Liste d'éléments
+
+
\ No newline at end of file
diff --git a/BlazorTp1/Resources/Pages.List.resx b/BlazorTp1/Resources/Pages.List.resx
new file mode 100644
index 0000000..ae67689
--- /dev/null
+++ b/BlazorTp1/Resources/Pages.List.resx
@@ -0,0 +1,123 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ Items List
+
+
\ No newline at end of file
diff --git a/BlazorTp1/Services/DataApiService.cs b/BlazorTp1/Services/DataApiService.cs
new file mode 100644
index 0000000..661478e
--- /dev/null
+++ b/BlazorTp1/Services/DataApiService.cs
@@ -0,0 +1,58 @@
+using BlazorTp1.Factories;
+using BlazorTp1.Components;
+using BlazorTp1.Models;
+namespace BlazorTp1.Services
+{
+ public class DataApiService : IDataService
+ {
+ private readonly HttpClient _http;
+
+ public DataApiService(
+ HttpClient http)
+ {
+ _http = http;
+ }
+
+ public async Task Add(ItemModel model)
+ {
+ // Get the item
+ var item = ItemFactory.Create(model);
+
+ // Save the data
+ await _http.PostAsJsonAsync("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/", item);
+ }
+
+ public async Task Count()
+ {
+ return await _http.GetFromJsonAsync("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/count");
+ }
+
+ public async Task> List(int currentPage, int pageSize)
+ {
+ return await _http.GetFromJsonAsync>($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
+ }
+
+ public async Task GetById(int id)
+ {
+ return await _http.GetFromJsonAsync($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/{id}");
+ }
+
+ public async Task Update(int id, ItemModel model)
+ {
+ // Get the item
+ var item = ItemFactory.Create(model);
+
+ await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item);
+ }
+
+ public async Task Delete(int id)
+ {
+ await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}");
+ }
+
+ public async Task> GetRecipes()
+ {
+ return await _http.GetFromJsonAsync>("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/recipe");
+ }
+ }
+}
diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs
new file mode 100644
index 0000000..1dca040
--- /dev/null
+++ b/BlazorTp1/Services/DataLocalService.cs
@@ -0,0 +1,145 @@
+using Blazored.LocalStorage;
+using BlazorTp1.Components;
+using BlazorTp1.Factories;
+using BlazorTp1.Models;
+using Microsoft.AspNetCore.Components;
+
+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(ItemFactory.Create(model));
+
+ // 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();
+ }
+
+ //new methods
+
+ public async Task GetById(int id)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync>("data");
+
+ // Get the item int the list
+ var item = currentData.FirstOrDefault(w => w.Id == id);
+
+ // Check if item exist
+ if (item == null)
+ {
+ throw new Exception($"Unable to found the item with ID: {id}");
+ }
+
+ return item;
+ }
+
+ public async Task Update(int id, ItemModel model)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync>("data");
+
+ // Get the item int the list
+ var item = currentData.FirstOrDefault(w => w.Id == id);
+
+ // Check if item exist
+ if (item == null)
+ {
+ throw new Exception($"Unable to found the item with ID: {id}");
+ }
+
+ // Modify the content of the item
+ ItemFactory.Update(item, model);
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
+
+ public async Task Delete(int id)
+ {
+ // Get the current data
+ var currentData = await _localStorage.GetItemAsync>("data");
+
+ // Get the item int the list
+ var item = currentData.FirstOrDefault(w => w.Id == id);
+
+ // Delete item in
+ currentData.Remove(item);
+
+ // Save the data
+ await _localStorage.SetItemAsync("data", currentData);
+ }
+
+ public Task> GetRecipes()
+ {
+ var items = new List
+ {
+ new CraftingRecipe
+ {
+ Give = new Item { DisplayName = "Diamond", Name = "diamond" },
+ Have = new List>
+ {
+ new List { "dirt", "dirt", "dirt" },
+ new List { "dirt", null, "dirt" },
+ new List { "dirt", "dirt", "dirt" }
+ }
+ }
+ };
+
+ return Task.FromResult(items);
+ }
+}
\ No newline at end of file
diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs
new file mode 100644
index 0000000..a03b7ad
--- /dev/null
+++ b/BlazorTp1/Services/IDataService.cs
@@ -0,0 +1,20 @@
+using BlazorTp1.Components;
+using BlazorTp1.Models;
+using System;
+
+public interface IDataService
+{
+ Task Add(ItemModel model);
+
+ Task Count();
+
+ Task> List(int currentPage, int pageSize);
+
+ Task GetById(int id);
+
+ Task Update(int id, ItemModel item);
+
+ Task Delete(int id);
+
+ Task> GetRecipes();
+}
diff --git a/BlazorTp1/Shared/CultureSelector.razor b/BlazorTp1/Shared/CultureSelector.razor
new file mode 100644
index 0000000..0f46a4e
--- /dev/null
+++ b/BlazorTp1/Shared/CultureSelector.razor
@@ -0,0 +1,43 @@
+@using System.Globalization
+@inject NavigationManager NavigationManager
+
+
+
+
+
+@code
+{
+ private CultureInfo[] supportedCultures = new[]
+ {
+ new CultureInfo("en-US"),
+ new CultureInfo("fr-FR")
+ };
+
+ private CultureInfo Culture
+ {
+ get => CultureInfo.CurrentCulture;
+ set
+ {
+ if (CultureInfo.CurrentUICulture == value)
+ {
+ return;
+ }
+
+ var culture = value.Name.ToLower(CultureInfo.InvariantCulture);
+
+ var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
+ var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}";
+
+ // Redirect the user to the culture controller to set the cookie
+ this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
+ }
+ }
+}
diff --git a/BlazorTp1/Shared/MainLayout.razor b/BlazorTp1/Shared/MainLayout.razor
index 46ade29..e497265 100644
--- a/BlazorTp1/Shared/MainLayout.razor
+++ b/BlazorTp1/Shared/MainLayout.razor
@@ -12,6 +12,10 @@
About
+
+
+
+
@Body
diff --git a/BlazorTp1/_Imports.razor b/BlazorTp1/_Imports.razor
index 009cdfc..fb7258e 100644
--- a/BlazorTp1/_Imports.razor
+++ b/BlazorTp1/_Imports.razor
@@ -9,3 +9,5 @@
@using BlazorTp1
@using BlazorTp1.Shared
@using Blazorise.DataGrid
+@using Blazored.Modal
+@using Blazored.Modal.Services
\ No newline at end of file
diff --git a/BlazorTp1/wwwroot/appsettings.json b/BlazorTp1/wwwroot/appsettings.json
new file mode 100644
index 0000000..ef6372d
--- /dev/null
+++ b/BlazorTp1/wwwroot/appsettings.json
@@ -0,0 +1,9 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Default": "Trace",
+ "Microsoft": "Warning",
+ "Microsoft.Hosting.Lifetime": "Information"
+ }
+ }
+}
\ No newline at end of file
diff --git a/BlazorTp1/wwwroot/index.html b/BlazorTp1/wwwroot/index.html
new file mode 100644
index 0000000..8cb9f7a
--- /dev/null
+++ b/BlazorTp1/wwwroot/index.html
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file