diff --git a/Blazor/.dockerignore b/Blazor/.dockerignore new file mode 100644 index 0000000..bdca33b --- /dev/null +++ b/Blazor/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Blazor/Blazor/Blazor.csproj b/Blazor/Blazor/Blazor.csproj index 05b5d69..6da9495 100644 --- a/Blazor/Blazor/Blazor.csproj +++ b/Blazor/Blazor/Blazor.csproj @@ -4,6 +4,8 @@ net6.0 enable enable + 41bf93e1-56c6-4caf-8e48-e953cbb0f8a0 + Linux @@ -12,6 +14,7 @@ + diff --git a/Blazor/Blazor/Components/Crafting.razor b/Blazor/Blazor/Components/Crafting.razor new file mode 100644 index 0000000..a3e1bd2 --- /dev/null +++ b/Blazor/Blazor/Components/Crafting.razor @@ -0,0 +1,51 @@ + +
+
+
+ +
Available items:
+
+
+ + @foreach (var item in Items) + { + + } +
+
+ +
+ +
+
Recipe
+ +
+ +
+ + + + + + + + + +
+
+ +
Result
+
+ +
+
+ +
+
Actions
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/Blazor/Blazor/Components/Crafting.razor.cs b/Blazor/Blazor/Components/Crafting.razor.cs new file mode 100644 index 0000000..92c09b8 --- /dev/null +++ b/Blazor/Blazor/Components/Crafting.razor.cs @@ -0,0 +1,80 @@ +using Blazor.Models; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using System.Collections.ObjectModel; +using System.Collections.Specialized; + +namespace Blazor.Components +{ + public partial class Crafting + { + private Item _recipeResult; + + public Crafting() + { + Actions = new ObservableCollection(); + Actions.CollectionChanged += OnActionsCollectionChanged; + this.RecipeItems = new List { null, null, null, null, null, null, null, null, null }; + } + + public ObservableCollection Actions { get; set; } + public Item CurrentDragItem { get; set; } + + [Parameter] + public List Items { get; set; } + + public List RecipeItems { get; set; } + + public Item RecipeResult + { + get => this._recipeResult; + set + { + if (this._recipeResult == value) + { + return; + } + + this._recipeResult = value; + this.StateHasChanged(); + } + } + + [Parameter] + public List Recipes { get; set; } + + /// + /// Gets or sets the java script runtime. + /// + [Inject] + internal IJSRuntime JavaScriptRuntime { get; set; } + + public void CheckRecipe() + { + RecipeResult = null; + + // Get the current model + var currentModel = string.Join("|", this.RecipeItems.Select(s => s != null ? s.Name : string.Empty)); + + this.Actions.Add(new CraftingAction { Action = $"Items : {currentModel}" }); + + foreach (var craftingRecipe in Recipes) + { + // Get the recipe model + var recipeModel = string.Join("|", craftingRecipe.Have.SelectMany(s => s)); + + this.Actions.Add(new CraftingAction { Action = $"Recipe model : {recipeModel}" }); + + if (currentModel == recipeModel) + { + RecipeResult = craftingRecipe.Give; + } + } + } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); + } + } +} diff --git a/Blazor/Blazor/Components/Crafting.razor.css b/Blazor/Blazor/Components/Crafting.razor.css new file mode 100644 index 0000000..f8e0615 --- /dev/null +++ b/Blazor/Blazor/Components/Crafting.razor.css @@ -0,0 +1,19 @@ +.css-grid { + grid-template-columns: repeat(4,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 286px; +} + +.css-recipe { + grid-template-columns: repeat(3,minmax(0,1fr)); + gap: 10px; + display: grid; + width: 212px; +} + +.actions { + border: 1px solid black; + height: 250px; + overflow: scroll; +} \ No newline at end of file diff --git a/Blazor/Blazor/Components/Crafting.razor.js b/Blazor/Blazor/Components/Crafting.razor.js new file mode 100644 index 0000000..3fcb76d --- /dev/null +++ b/Blazor/Blazor/Components/Crafting.razor.js @@ -0,0 +1,16 @@ +window.Crafting = +{ + AddActions: function (data) { + + data.forEach(element => { + var div = document.createElement('div'); + div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; + + if (element.item) { + div.innerHTML += ' - Item Name: ' + element.item.name; + } + + document.getElementById('actions').appendChild(div); + }); + } +} \ No newline at end of file diff --git a/Blazor/Blazor/Components/CraftingAction.cs b/Blazor/Blazor/Components/CraftingAction.cs new file mode 100644 index 0000000..e2efbb2 --- /dev/null +++ b/Blazor/Blazor/Components/CraftingAction.cs @@ -0,0 +1,11 @@ +using Blazor.Models; + +namespace Blazor.Components +{ + public class CraftingAction + { + public string Action { get; set; } + public int Index { get; set; } + public Item Item { get; set; } + } +} diff --git a/Blazor/Blazor/Components/CraftingItem.razor b/Blazor/Blazor/Components/CraftingItem.razor new file mode 100644 index 0000000..ed6a1f6 --- /dev/null +++ b/Blazor/Blazor/Components/CraftingItem.razor @@ -0,0 +1,15 @@ +@using Blazor.Models +
+ + @if (Item != null) + { + @Item.DisplayName + } +
diff --git a/Blazor/Blazor/Components/CraftingItem.razor.cs b/Blazor/Blazor/Components/CraftingItem.razor.cs new file mode 100644 index 0000000..74b13e5 --- /dev/null +++ b/Blazor/Blazor/Components/CraftingItem.razor.cs @@ -0,0 +1,63 @@ +using Blazor.Models; +using Microsoft.AspNetCore.Components; + +namespace Blazor.Components +{ + public partial class CraftingItem + { + [Parameter] + public int Index { get; set; } + + [Parameter] + public Item Item { get; set; } + + [Parameter] + public bool NoDrop { get; set; } + + [CascadingParameter] + public Crafting Parent { get; set; } + + internal void OnDragEnter() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + } + + internal void OnDragLeave() + { + if (NoDrop) + { + return; + } + + Parent.Actions.Add(new CraftingAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + } + + internal void OnDrop() + { + if (NoDrop) + { + return; + } + + this.Item = Parent.CurrentDragItem; + Parent.RecipeItems[this.Index] = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drop", Item = this.Item, Index = this.Index }); + + // Check recipe + Parent.CheckRecipe(); + } + + private void OnDragStart() + { + Parent.CurrentDragItem = this.Item; + + Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + } + } +} diff --git a/Blazor/Blazor/Components/CraftingItem.razor.css b/Blazor/Blazor/Components/CraftingItem.razor.css new file mode 100644 index 0000000..1499794 --- /dev/null +++ b/Blazor/Blazor/Components/CraftingItem.razor.css @@ -0,0 +1,6 @@ +.item { + width: 64px; + height: 64px; + border: 1px solid; + overflow: hidden; +} \ No newline at end of file diff --git a/Blazor/Blazor/Components/CraftingRecipe.cs b/Blazor/Blazor/Components/CraftingRecipe.cs new file mode 100644 index 0000000..1f2face --- /dev/null +++ b/Blazor/Blazor/Components/CraftingRecipe.cs @@ -0,0 +1,10 @@ +using Blazor.Models; + +namespace Blazor.Components +{ + public class CraftingRecipe + { + public Item Give { get; set; } + public List> Have { get; set; } + } +} diff --git a/Blazor/Blazor/Dockerfile b/Blazor/Blazor/Dockerfile new file mode 100644 index 0000000..432afa3 --- /dev/null +++ b/Blazor/Blazor/Dockerfile @@ -0,0 +1,22 @@ +#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base +WORKDIR /app +EXPOSE 80 +EXPOSE 443 + +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +WORKDIR /src +COPY ["Blazor/Blazor.csproj", "Blazor/"] +RUN dotnet restore "Blazor/Blazor.csproj" +COPY . . +WORKDIR "/src/Blazor" +RUN dotnet build "Blazor.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "Blazor.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Blazor.dll"] \ No newline at end of file diff --git a/Blazor/Blazor/Factories/ItemFactory.cs b/Blazor/Blazor/Factories/ItemFactory.cs index a46ec2d..d2c094e 100644 --- a/Blazor/Blazor/Factories/ItemFactory.cs +++ b/Blazor/Blazor/Factories/ItemFactory.cs @@ -15,7 +15,8 @@ namespace Blazor.Factories EnchantCategories = item.EnchantCategories, MaxDurability = item.MaxDurability, StackSize = item.StackSize, - ImageContent = imageContent + ImageContent = imageContent, + ImageBase64 = string.IsNullOrWhiteSpace(item.ImageBase64) ? Convert.ToBase64String(imageContent) : item.ImageBase64 }; } @@ -30,7 +31,8 @@ namespace Blazor.Factories EnchantCategories = model.EnchantCategories, MaxDurability = model.MaxDurability, StackSize = model.StackSize, - CreatedDate = DateTime.Now + CreatedDate = DateTime.Now, + ImageBase64 = Convert.ToBase64String(model.ImageContent) }; } @@ -43,6 +45,7 @@ namespace Blazor.Factories item.MaxDurability = model.MaxDurability; item.StackSize = model.StackSize; item.UpdatedDate = DateTime.Now; + item.ImageBase64 = Convert.ToBase64String(model.ImageContent); } } } diff --git a/Blazor/Blazor/Models/Item.cs b/Blazor/Blazor/Models/Item.cs index 84545c4..c5410a1 100644 --- a/Blazor/Blazor/Models/Item.cs +++ b/Blazor/Blazor/Models/Item.cs @@ -11,5 +11,6 @@ public List RepairWith { get; set; } public DateTime CreatedDate { get; set; } public DateTime? UpdatedDate { get; set; } + public string ImageBase64 { get; set; } } } diff --git a/Blazor/Blazor/Models/ItemModel.cs b/Blazor/Blazor/Models/ItemModel.cs index da86696..21bc32b 100644 --- a/Blazor/Blazor/Models/ItemModel.cs +++ b/Blazor/Blazor/Models/ItemModel.cs @@ -33,5 +33,6 @@ namespace Blazor.Models [Required(ErrorMessage = "The image of the item is mandatory!")] public byte[] ImageContent { get; set; } + public string ImageBase64 { get; set; } } } diff --git a/Blazor/Blazor/Pages/Edit.razor b/Blazor/Blazor/Pages/Edit.razor index 57cedae..3f92e5a 100644 --- a/Blazor/Blazor/Pages/Edit.razor +++ b/Blazor/Blazor/Pages/Edit.razor @@ -55,14 +55,7 @@

diff --git a/Blazor/Blazor/Pages/Edit.razor.cs b/Blazor/Blazor/Pages/Edit.razor.cs index 442d2b0..0229107 100644 --- a/Blazor/Blazor/Pages/Edit.razor.cs +++ b/Blazor/Blazor/Pages/Edit.razor.cs @@ -45,11 +45,6 @@ namespace Blazor.Pages var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png"); - if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png")) - { - fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png"); - } - // Set the model with the item itemModel = ItemFactory.ToModel(item, fileContent); } diff --git a/Blazor/Blazor/Pages/Index.razor b/Blazor/Blazor/Pages/Index.razor index 3aba0f3..6765355 100644 --- a/Blazor/Blazor/Pages/Index.razor +++ b/Blazor/Blazor/Pages/Index.razor @@ -1,31 +1,5 @@ @page "/" -@using System.Globalization -@using Blazor.Components - - -Index - -

Hello, world!

- -Welcome to your new app. - - - -

- CurrentCulture: @CultureInfo.CurrentCulture -

- - - - -
- Cake Token Number - @cakeContext.Id -
-
- -
-
@cakeContext.Name
-
$ @cakeContext.Cost
-
-
-
+@using Blazor.Components; +
+ +
diff --git a/Blazor/Blazor/Pages/Index.razor.cs b/Blazor/Blazor/Pages/Index.razor.cs index be574ee..0a77a38 100644 --- a/Blazor/Blazor/Pages/Index.razor.cs +++ b/Blazor/Blazor/Pages/Index.razor.cs @@ -1,14 +1,32 @@ -using Blazor.Models; +using Blazor.Components; +using Blazor.Models; +using Blazor.Services; +using Microsoft.AspNetCore.Components; namespace Blazor.Pages { public partial class Index { - private Cake CakeItem = new Cake + [Inject] + public IDataService DataService { get; set; } + + public List Items { get; set; } = new List(); + + private List Recipes { get; set; } = new List(); + + protected override async Task OnAfterRenderAsync(bool firstRender) { - Id = 1, - Name = "Black Forest", - Cost = 50 - }; + base.OnAfterRenderAsync(firstRender); + + if (!firstRender) + { + return; + } + + Items = await DataService.List(0, await DataService.Count()); + Recipes = await DataService.GetRecipes(); + + StateHasChanged(); + } } } diff --git a/Blazor/Blazor/Pages/List.razor b/Blazor/Blazor/Pages/List.razor index 2f954fc..8a55786 100644 --- a/Blazor/Blazor/Pages/List.razor +++ b/Blazor/Blazor/Pages/List.razor @@ -21,17 +21,17 @@ Responsive> - - @if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) - { - @context.DisplayName - } - else - { - @context.DisplayName - } - - + + @if (!string.IsNullOrWhiteSpace(context.ImageBase64)) + { + @context.DisplayName + } + else + { + @context.DisplayName + } + + diff --git a/Blazor/Blazor/Pages/_Layout.cshtml b/Blazor/Blazor/Pages/_Layout.cshtml index 839e817..f627359 100644 --- a/Blazor/Blazor/Pages/_Layout.cshtml +++ b/Blazor/Blazor/Pages/_Layout.cshtml @@ -33,6 +33,7 @@ + diff --git a/Blazor/Blazor/Program.cs b/Blazor/Blazor/Program.cs index da2e740..8de0abd 100644 --- a/Blazor/Blazor/Program.cs +++ b/Blazor/Blazor/Program.cs @@ -25,7 +25,6 @@ builder.Services .AddFontAwesomeIcons(); builder.Services.AddBlazoredLocalStorage(); -builder.Services.AddScoped(); builder.Services.AddBlazoredModal(); @@ -35,6 +34,9 @@ builder.Services.AddControllers(); // Add the localization to the app and specify the resources path builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); +// Add api service +builder.Services.AddScoped(); + // Configure the localtization builder.Services.Configure(options => { diff --git a/Blazor/Blazor/Properties/launchSettings.json b/Blazor/Blazor/Properties/launchSettings.json index cfdb2a5..dcf4542 100644 --- a/Blazor/Blazor/Properties/launchSettings.json +++ b/Blazor/Blazor/Properties/launchSettings.json @@ -10,12 +10,12 @@ "profiles": { "Blazor": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7027;http://localhost:5027", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "https://localhost:7027;http://localhost:5027", + "dotnetRunMessages": true }, "IIS Express": { "commandName": "IISExpress", @@ -23,6 +23,13 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "publishAllPorts": true, + "useSSL": true } } -} +} \ No newline at end of file diff --git a/Blazor/Blazor/Services/DataApiService.cs b/Blazor/Blazor/Services/DataApiService.cs new file mode 100644 index 0000000..8694471 --- /dev/null +++ b/Blazor/Blazor/Services/DataApiService.cs @@ -0,0 +1,58 @@ +using Blazor.Components; +using Blazor.Factories; +using Blazor.Models; + +namespace Blazor.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://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/{id}", item); + } + + public async Task Delete(int id) + { + await _http.DeleteAsync($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/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/Blazor/Blazor/Services/DataLocalService.cs b/Blazor/Blazor/Services/DataLocalService.cs index 4176cd1..c92584c 100644 --- a/Blazor/Blazor/Services/DataLocalService.cs +++ b/Blazor/Blazor/Services/DataLocalService.cs @@ -1,4 +1,5 @@ -using Blazor.Factories; +using Blazor.Components; +using Blazor.Factories; using Blazor.Models; using Blazored.LocalStorage; using Microsoft.AspNetCore.Components; @@ -35,21 +36,6 @@ namespace Blazor.Services // Add the item to the current data currentData.Add(ItemFactory.Create(model)); - // 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); } @@ -106,32 +92,6 @@ namespace Blazor.Services 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 ItemFactory.Update(item, model); @@ -162,5 +122,24 @@ namespace Blazor.Services // 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); + } } } diff --git a/Blazor/Blazor/Services/IDataService.cs b/Blazor/Blazor/Services/IDataService.cs index e7d0728..cf8c5db 100644 --- a/Blazor/Blazor/Services/IDataService.cs +++ b/Blazor/Blazor/Services/IDataService.cs @@ -1,4 +1,5 @@ -using Blazor.Models; +using Blazor.Components; +using Blazor.Models; namespace Blazor.Services { @@ -15,5 +16,7 @@ namespace Blazor.Services Task Update(int id, ItemModel model); Task Delete(int id); + + Task> GetRecipes(); } } diff --git a/Blazor/Blazor/Shared/TableTemplate.razor b/Blazor/Blazor/Shared/TableTemplate.razor new file mode 100644 index 0000000..447764b --- /dev/null +++ b/Blazor/Blazor/Shared/TableTemplate.razor @@ -0,0 +1,28 @@ +@typeparam TItem +@using System.Diagnostics.CodeAnalysis + + + + @TableHeader + + + @foreach (var item in Items) + { + if (RowTemplate is not null) + { + @RowTemplate(item) + } + } + +
+ +@code { + [Parameter] + public RenderFragment? TableHeader { get; set; } + + [Parameter] + public RenderFragment? RowTemplate { get; set; } + + [Parameter, AllowNull] + public IReadOnlyList Items { get; set; } +}