diff --git a/App.razor b/App.razor index 6fd3ed1..111b4cd 100644 --- a/App.razor +++ b/App.razor @@ -1,12 +1,8 @@ - + - - Not found - -

Sorry, there's nothing at this address.

-
+

Sorry, there's nothing at this address.

diff --git a/Models/ItemModel.cs b/Models/ItemModel.cs new file mode 100644 index 0000000..72fc9d3 --- /dev/null +++ b/Models/ItemModel.cs @@ -0,0 +1,37 @@ +using System.ComponentModel.DataAnnotations; + +namespace ProjetBlaser.Models +{ + public class ItemModel + { + public int Id { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "The display name must not exceed 50 characters.")] + public string DisplayName { get; set; } + + [Required] + [StringLength(50, ErrorMessage = "The name must not exceed 50 characters.")] + [RegularExpression(@"^[a-z''-'\s]{1,40}$", ErrorMessage = "Only lowercase characters are accepted.")] + public string Name { get; set; } + + [Required] + [Range(1, 64)] + public int StackSize { get; set; } + + [Required] + [Range(1, 125)] + public int MaxDurability { get; set; } + + public List EnchantCategories { get; set; } + + public List RepairWith { get; set; } + + [Required] + [Range(typeof(bool), "true", "true", ErrorMessage = "You must agree to the terms.")] + public bool AcceptCondition { get; set; } + + [Required(ErrorMessage = "The image of the item is mandatory!")] + public byte[] ImageContent { get; set; } + } +} diff --git a/Models/PanelBody.cs b/Models/PanelBody.cs new file mode 100644 index 0000000..28cb734 --- /dev/null +++ b/Models/PanelBody.cs @@ -0,0 +1,8 @@ +namespace ProjetBlaser.Models +{ + public class PanelBody + { + public string? Text { get; set; } + public string? Style { get; set; } + } +} diff --git a/Pages/Add.razor b/Pages/Add.razor new file mode 100644 index 0000000..64b1710 --- /dev/null +++ b/Pages/Add.razor @@ -0,0 +1,69 @@ +@page "/add" + +

Add

+ + + + + +

+ +

+

+ +

+

+ +

+

+ +

+

+ Enchant categories: +

+ @foreach (var item in enchantCategories) + { + + } +
+

+

+ Repair with: +

+ @foreach (var item in repairWith) + { + + } +
+

+

+ +

+

+ +

+ + +
\ No newline at end of file diff --git a/Pages/Add.razor.cs b/Pages/Add.razor.cs new file mode 100644 index 0000000..3ec541f --- /dev/null +++ b/Pages/Add.razor.cs @@ -0,0 +1,89 @@ +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Forms; +using ProjetBlaser.Models; +using ProjetBlaser.Services; + +namespace ProjetBlaser.Pages +{ + public partial class Add + { + /// + /// The default enchant categories. + /// + private List enchantCategories = new List() { "armor", "armor_head", "armor_chest", "weapon", "digger", "breakable", "vanishable" }; + + /// + /// The current item model + /// + private ItemModel itemModel = new() + { + EnchantCategories = new List(), + RepairWith = new List() + }; + + /// + /// 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" }; + + [Inject] + public IDataService DataService { get; set; } + + [Inject] + public NavigationManager NavigationManager { get; set; } + + private async void HandleValidSubmit() + { + await DataService.Add(itemModel); + + NavigationManager.NavigateTo("list"); + } + + private async Task LoadImage(InputFileChangeEventArgs e) + { + // Set the content of the image to the model + using (var memoryStream = new MemoryStream()) + { + await e.File.OpenReadStream().CopyToAsync(memoryStream); + itemModel.ImageContent = memoryStream.ToArray(); + } + } + + private void OnEnchantCategoriesChange(string item, object checkedValue) + { + if ((bool)checkedValue) + { + if (!itemModel.EnchantCategories.Contains(item)) + { + itemModel.EnchantCategories.Add(item); + } + + return; + } + + if (itemModel.EnchantCategories.Contains(item)) + { + itemModel.EnchantCategories.Remove(item); + } + } + + private void OnRepairWithChange(string item, object checkedValue) + { + if ((bool)checkedValue) + { + if (!itemModel.RepairWith.Contains(item)) + { + itemModel.RepairWith.Add(item); + } + + return; + } + + if (itemModel.RepairWith.Contains(item)) + { + itemModel.RepairWith.Remove(item); + } + } + } +} diff --git a/Pages/BlazorRoute.razor b/Pages/BlazorRoute.razor new file mode 100644 index 0000000..65969f3 --- /dev/null +++ b/Pages/BlazorRoute.razor @@ -0,0 +1,3 @@ +@page "/BlazorRoute" +@page "/DifferentBlazorRoute" +

BlazorRoute

\ No newline at end of file diff --git a/Pages/Edit.razor b/Pages/Edit.razor new file mode 100644 index 0000000..d201ae0 --- /dev/null +++ b/Pages/Edit.razor @@ -0,0 +1,3 @@ +@page "/edit/{Id:int}" +

Edit

+
Mon paramètre: @Id
diff --git a/Pages/Edit.razor.cs b/Pages/Edit.razor.cs new file mode 100644 index 0000000..39df9c7 --- /dev/null +++ b/Pages/Edit.razor.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Components; + +namespace ProjetBlaser.Pages +{ + public partial class Edit + { + [Parameter] + public int Id { get; set; } + } +} diff --git a/Pages/List.razor b/Pages/List.razor index b7e5b37..7b223e8 100644 --- a/Pages/List.razor +++ b/Pages/List.razor @@ -2,7 +2,11 @@ @using ProjetBlaser.Models

List

- +
+ + Ajouter + +
+ + + @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + @@ -25,4 +41,9 @@ + + + Editer + + \ No newline at end of file diff --git a/Pages/List.razor.cs b/Pages/List.razor.cs index 761adcd..c71334b 100644 --- a/Pages/List.razor.cs +++ b/Pages/List.razor.cs @@ -2,6 +2,7 @@ using Blazorise.DataGrid; using Microsoft.AspNetCore.Components; using ProjetBlaser.Models; +using ProjetBlaser.Services; namespace ProjetBlaser.Pages { @@ -12,32 +13,10 @@ namespace ProjetBlaser.Pages private int totalItem; [Inject] - public HttpClient Http { get; set; } + public IDataService DataService { get; set; } [Inject] - public ILocalStorageService LocalStorage { 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); - } - } + public IWebHostEnvironment WebHostEnvironment { get; set; } private async Task OnReadData(DataGridReadDataEventArgs e) { @@ -46,14 +25,10 @@ namespace ProjetBlaser.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/Pages/ParameterParen.razor b/Pages/ParameterParen.razor new file mode 100644 index 0000000..7b5ae80 --- /dev/null +++ b/Pages/ParameterParen.razor @@ -0,0 +1,9 @@ +@page "/parameter-parent" + +

Child component (without attribute values)

+ + + +

Child component (with attribute values)

+ + \ No newline at end of file diff --git a/Pages/RouteParameter.razor b/Pages/RouteParameter.razor new file mode 100644 index 0000000..5d1f6d5 --- /dev/null +++ b/Pages/RouteParameter.razor @@ -0,0 +1,13 @@ +@page "/RouteParameter/{text?}" + +

Blazor is @Text!

+ +@code { + [Parameter] + public string? Text { get; set; } + + protected override void OnInitialized() + { + Text = Text ?? "fantastic"; + } +} diff --git a/Pages/User.razor b/Pages/User.razor new file mode 100644 index 0000000..907e345 --- /dev/null +++ b/Pages/User.razor @@ -0,0 +1,17 @@ +@page "/user/{Id:int}/{Option:bool?}" + +

+ Id: @Id +

+ +

+ Option: @Option +

+ +@code { + [Parameter] + public int Id { get; set; } + + [Parameter] + public bool Option { get; set; } +} diff --git a/Program.cs b/Program.cs index 3be1df5..98dbedc 100644 --- a/Program.cs +++ b/Program.cs @@ -5,6 +5,7 @@ using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; using ProjetBlaser.Data; +using ProjetBlaser.Services; var builder = WebApplication.CreateBuilder(args); @@ -13,6 +14,7 @@ builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); builder.Services.AddHttpClient(); +builder.Services.AddScoped(); builder.Services .AddBlazorise() .AddBootstrapProviders() diff --git a/Services/DataLocalService.cs b/Services/DataLocalService.cs new file mode 100644 index 0000000..0309c4d --- /dev/null +++ b/Services/DataLocalService.cs @@ -0,0 +1,156 @@ +using Blazored.LocalStorage; +using Microsoft.AspNetCore.Components; +using ProjetBlaser.Models; + +namespace ProjetBlaser.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(); + } + 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}"); + } + + // Save the image + var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); + + // Check if the folder "images" exist + if (!imagePathInfo.Exists) + { + imagePathInfo.Create(); + } + + // Delete the previous image + if (item.Name != model.Name) + { + var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png"); + + if (oldFileName.Exists) + { + File.Delete(oldFileName.FullName); + } + } + + // Determine the image name + var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); + + // Write the file content + await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); + + // Modify the content of the item + item.DisplayName = model.DisplayName; + item.Name = model.Name; + item.RepairWith = model.RepairWith; + item.EnchantCategories = model.EnchantCategories; + item.MaxDurability = model.MaxDurability; + item.StackSize = model.StackSize; + item.UpdatedDate = DateTime.Now; + + // Save the data + await _localStorage.SetItemAsync("data", currentData); + } + } +} diff --git a/Services/IDataService.cs b/Services/IDataService.cs new file mode 100644 index 0000000..01c9c6b --- /dev/null +++ b/Services/IDataService.cs @@ -0,0 +1,17 @@ +using ProjetBlaser.Models; + +namespace ProjetBlaser.Services +{ + public interface IDataService + { + Task Add(ItemModel model); + + Task Count(); + + Task> List(int currentPage, int pageSize); + + Task GetById(int id); + + Task Update(int id, ItemModel model); + } +} diff --git a/Shared/ParameterChild.razor b/Shared/ParameterChild.razor new file mode 100644 index 0000000..fa33924 --- /dev/null +++ b/Shared/ParameterChild.razor @@ -0,0 +1,20 @@ +@using ProjetBlaser.Models +
+
@Title
+
+ @Body.Text +
+
+ +@code { + [Parameter] + public string Title { get; set; } = "Set By Child"; + + [Parameter] + public PanelBody Body { get; set; } = + new() + { + Text = "Set by child.", + Style = "normal" + }; +} \ No newline at end of file diff --git a/wwwroot/images/default.png b/wwwroot/images/default.png new file mode 100644 index 0000000..a7446c9 Binary files /dev/null and b/wwwroot/images/default.png differ diff --git a/wwwroot/images/tatatu.png b/wwwroot/images/tatatu.png new file mode 100644 index 0000000..b2c1be8 Binary files /dev/null and b/wwwroot/images/tatatu.png differ