diff --git a/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs b/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs index 40c97bb..c0443a5 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs +++ b/myBlazorApp/Minecraft.Crafting.Api/Controllers/InventoryController.cs @@ -116,7 +116,7 @@ namespace Minecraft.Crafting.Api.Controllers /// The async task. [HttpPut] [Route("")] - public Task UpdateInventory(InventoryModel item) + public Task UpdateInventory(string[] infos) { var data = JsonSerializer.Deserialize>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions); @@ -125,15 +125,9 @@ namespace Minecraft.Crafting.Api.Controllers throw new Exception("Unable to get the inventory."); } - var inventoryItem = data.FirstOrDefault(w => w.ItemName == item.ItemName && w.Position == item.Position); - - if (inventoryItem == null) - { - throw new Exception($"Unable to found the item with name: {item.ItemName} at position: {item.Position}"); - } - - inventoryItem.ItemName = item.ItemName; - inventoryItem.Position = item.Position; + data.ToArray()[Int32.Parse(infos[1])-1].ItemName = infos[0]; + data.ToArray()[Int32.Parse(infos[1])-1].Position = Int32.Parse(infos[1]); + data.ToArray()[Int32.Parse(infos[1])-1].Number = Int32.Parse(infos[2]); System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions)); diff --git a/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json b/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json new file mode 100644 index 0000000..a562add --- /dev/null +++ b/myBlazorApp/Minecraft.Crafting.Api/Data/inventory.json @@ -0,0 +1,76 @@ +[ + { + "itemName": "null", + "position": 1 + }, + { + "itemName": "lili", + "position": 2, + "number": 11 + }, + { + "itemName": "null", + "position": 3 + }, + { + "itemName": "null", + "position": 4 + }, + { + "itemName": "null", + "position": 5 + }, + { + "itemName": "null", + "position": 6 + }, + { + "itemName": "null", + "position": 7 + }, + { + "itemName": "null", + "position": 8 + }, + { + "itemName": "null", + "position": 9 + }, + { + "itemName": "lili", + "position": 10, + "number": 11 + }, + { + "itemName": "null", + "position": 11 + }, + { + "itemName": "null", + "position": 12 + }, + { + "itemName": "null", + "position": 13 + }, + { + "itemName": "null", + "position": 14 + }, + { + "itemName": "null", + "position": 15 + }, + { + "itemName": "null", + "position": 16 + }, + { + "itemName": "null", + "position": 17 + }, + { + "itemName": "null", + "position": 18 + } +] \ No newline at end of file diff --git a/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs b/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs index 0fa1936..22b1b81 100644 --- a/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs +++ b/myBlazorApp/Minecraft.Crafting.Api/Models/InventoryModel.cs @@ -20,5 +20,7 @@ namespace Minecraft.Crafting.Api.Models /// Gets or sets the position. /// public int Position { get; set; } + + public int Number { get; set; } } } \ No newline at end of file diff --git a/myBlazorApp/myBlazorApp/Components/InventoryAction.cs b/myBlazorApp/myBlazorApp/Components/InventoryAction.cs index 6fb225c..bdb47b6 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryAction.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryAction.cs @@ -6,7 +6,8 @@ namespace myBlazorApp.Components { public string Action { get; set; } public int Index { get; set; } - public Item Item { get; set; } + public string ItemName { get; set; } + public int Number { get; set; } } } diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor index 3755db3..7502998 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor @@ -1,17 +1,16 @@  -
- @if (Item != null) +
+ @if (Item != "null") { - @Item.DisplayName +

@Item

+

@Number

} else { diff --git a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs index 3efe7a0..85cc073 100644 --- a/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/InventoryItem.razor.cs @@ -1,5 +1,7 @@ using System; namespace myBlazorApp.Components; + +using System.Reflection.Metadata; using Blazorise; using Microsoft.AspNetCore.Components; using myBlazorApp.Models; @@ -7,11 +9,14 @@ using myBlazorApp.Models; public partial class InventoryItem { [Parameter] - public Item Item { get; set; } + public string Item { get; set; } [Parameter] public int Index { get; set; } + [Parameter] + public int Number { get; set; } + [Parameter] public bool NoDrop { get; set; } @@ -24,7 +29,7 @@ public partial class InventoryItem { return; } - Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", ItemName = this.Item, Index = this.Index, Number = this.Number }); } internal void OnDragLeave() @@ -33,7 +38,7 @@ public partial class InventoryItem { return; } - Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); + Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", ItemName = this.Item, Index = this.Index, Number = this.Number }); } internal void OnDrop() @@ -42,15 +47,60 @@ public partial class InventoryItem { return; } - this.Item = Parent.CurrentDragItem; - Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); + if (Parent.CurrentDragItem != "null") + { + if(this.Item == Parent.CurrentDragItem) + { + this.Number = this.Number + Parent.CurrentDragNumber; + Parent.CurrentInventoryItem.Number = 0; + Parent.CurrentInventoryItem.Item = "null"; + Parent.ItemsInventory[Parent.CurrentInventoryItem.Index-1] = new InventoryListItem(Parent.CurrentInventoryItem.Item, Parent.CurrentInventoryItem.Index, Parent.CurrentInventoryItem.Number); + Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = Parent.CurrentInventoryItem.Item, Index = Parent.CurrentInventoryItem.Index, Number = Parent.CurrentInventoryItem.Number }); + } + else + { + string tmpItem = this.Item; + int tmpNumber = this.Number; + Parent.ItemsInventory[Parent.CurrentDragIndex-1] = new InventoryListItem(tmpItem, Parent.CurrentDragIndex, tmpNumber); + this.Item = Parent.CurrentDragItem; + this.Number = Parent.CurrentDragNumber; + Parent.CurrentInventoryItem.Item = tmpItem; + Parent.CurrentInventoryItem.Number = tmpNumber; + } + + } + Parent.Actions.Add(new InventoryAction { Action = "Drop", ItemName = this.Item, Index = this.Index, Number = this.Number }); + } + + internal void OnDragEnd() + { + if(NoDrop || Parent.CurrentDragItem == "null") + { + return; + } + if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex != this.Index) + { + Parent.ItemsInventory[this.Index-1] = new InventoryListItem(Parent.CurrentDragItem, Parent.CurrentDragIndex, Parent.CurrentDragNumber); + this.Item = Parent.CurrentDragItem; + this.Number = Parent.CurrentDragNumber; + } + Parent.CurrentDragIndex = -1; + Parent.CurrentDragItem = "null"; + Parent.CurrentDragNumber = 0; + Parent.Actions.Add(new InventoryAction { Action = "End", ItemName = this.Item, Index = this.Index, Number = this.Number }); } private void OnDragStart() { + Parent.CurrentInventoryItem = this; + Parent.CurrentDragIndex = this.Index; Parent.CurrentDragItem = this.Item; - Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); + Parent.CurrentDragNumber = this.Number; + this.Item = "null"; + this.Number = 0; + Parent.Actions.Add(new InventoryAction { Action = "Drag Start", ItemName = this.Item, Index = this.Index, Number = this.Number}); + } } diff --git a/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs b/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs new file mode 100644 index 0000000..f7a3ef1 --- /dev/null +++ b/myBlazorApp/myBlazorApp/Components/InventoryListItem.cs @@ -0,0 +1,22 @@ +using System; +using myBlazorApp.Models; +namespace myBlazorApp.Components +{ + public class InventoryListItem + { + //public InventoryListItem() { } + + public InventoryListItem(string itemName, int position, int number) + { + this.itemName = itemName; + this.position = position; + this.number = number; + } + + public string itemName { get; set; } + public int position { get; set; } + public int number { get; set; } + } + +} + diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor b/myBlazorApp/myBlazorApp/Components/MyInventory.razor index f4718ae..cef2ec2 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor @@ -1,17 +1,13 @@ - +@using myBlazorApp.Models +

My Inventory

- - @foreach (var item in ItemsInventory) - { - - index++; - } - @for (int i = @index; i < 18; i++) + @foreach(InventoryListItem i in ItemsInventory) { - + } +
diff --git a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs index ae5b809..0359582 100644 --- a/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Components/MyInventory.razor.cs @@ -10,19 +10,22 @@ namespace myBlazorApp.Components public partial class MyInventory { [Parameter] - public List ItemsInventory { get; set; } = new List(); + public List ItemsInventory { get; set; } - [Parameter] public int index { get; set; } = 0; public ObservableCollection Actions { get; set; } - public Item CurrentDragItem { get; set; } = new Item(); + public string CurrentDragItem { get; set; } + public int CurrentDragIndex { get; set; } + public int CurrentDragNumber { get; set; } + public InventoryItem CurrentInventoryItem {get; set;} = new InventoryItem(); public MyInventory() { Actions = new ObservableCollection(); Actions.CollectionChanged += OnActionsCollectionChanged; + } [Inject] @@ -37,9 +40,14 @@ namespace myBlazorApp.Components [Inject] internal IJSRuntime JavaScriptRuntime { get; set; } - private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) + private async void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("MyInventory.AddActions", e.NewItems); + if (this.Actions.Last().Action == "End" || this.Actions.Last().Action == "Drop" || this.Actions.Last().Action == "Start") + { + string[] infos = new string[] { this.Actions.Last().ItemName, this.Actions.Last().Index.ToString(), this.Actions.Last().Number.ToString() }; + await DataService.UpdateInventory(infos); + } } } } diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor b/myBlazorApp/myBlazorApp/Pages/Inventory.razor index 6c7b877..cc16521 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor @@ -6,7 +6,7 @@
- +
diff --git a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs index 2708fbf..ceef206 100644 --- a/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs +++ b/myBlazorApp/myBlazorApp/Pages/Inventory.razor.cs @@ -7,16 +7,19 @@ using Blazorise; using Blazorise.DataGrid; using Blazorise.Extensions; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Localization; +using myBlazorApp.Components; +using myBlazorApp.Factories; using myBlazorApp.Modals; using myBlazorApp.Models; -using myBlazorApp.Services; +using myBlazorApp.Services; using IModalService = Blazored.Modal.Services.IModalService; - + namespace myBlazorApp.Pages { public partial class Inventory { - public List itemsInv = new List(); + public List itemsInv = new List(); private List items = new List(); @@ -26,8 +29,8 @@ namespace myBlazorApp.Pages public string SearchText = ""; - private int totalItem; - + private int totalItem; + private int currentPage; private int pageSize; @@ -47,16 +50,16 @@ namespace myBlazorApp.Pages [CascadingParameter] public IModalService Modal { get; set; } - + private bool isSorted = false; private async Task OnReadData(DataGridReadDataEventArgs e) { - if (e.CancellationToken.IsCancellationRequested) - { - return; + if (e.CancellationToken.IsCancellationRequested) + { + return; } if (!e.CancellationToken.IsCancellationRequested) @@ -64,7 +67,7 @@ namespace myBlazorApp.Pages totalItem = await DataService.Count(); items = await DataService.List(e.Page, e.PageSize); full = await DataService.List(e.Page, totalItem); - itemsInv = await DataService.List(e.Page, e.PageSize); + itemsInv = await DataService.GetInventoryItems(); currentPage = e.Page; pageSize = e.PageSize; if(isSorted==false) @@ -88,7 +91,7 @@ namespace myBlazorApp.Pages StateHasChanged(); } } - + private List choseList() { if (SearchText.IsNullOrEmpty()) @@ -107,14 +110,14 @@ namespace myBlazorApp.Pages StateHasChanged(); return choix; } - + private void inputValue() { Filtered = full.Where( itm => itm.DisplayName.ToLower().Contains(SearchText.ToLower())).ToList(); choseList(); } - + private void OnPress() { if (isSorted == true) @@ -124,7 +127,7 @@ namespace myBlazorApp.Pages else isSorted = true; SortList(); } - + private List SortList() { if (isSorted == false) diff --git a/myBlazorApp/myBlazorApp/Services/DataApiService.cs b/myBlazorApp/myBlazorApp/Services/DataApiService.cs index 07f3749..06c87eb 100644 --- a/myBlazorApp/myBlazorApp/Services/DataApiService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataApiService.cs @@ -58,9 +58,15 @@ namespace myBlazorApp.Services return await _http.GetFromJsonAsync>("https://localhost:8635/api/Crafting/recipe"); } - public async Task> FullList(int pageSize) + /* TASKS FOR INVENTORY */ + public async Task> GetInventoryItems() { - return await _http.GetFromJsonAsync>("https://localhost:8635/api/Crafting/all"); + return await _http.GetFromJsonAsync>("https://localhost:8635/api/Inventory"); + } + + public async Task UpdateInventory(string[] infos) + { + await _http.PutAsJsonAsync("https://localhost:8635/api/Inventory", infos); } } } diff --git a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs index 5abbd1e..05f43ab 100644 --- a/myBlazorApp/myBlazorApp/Services/DataLocalService.cs +++ b/myBlazorApp/myBlazorApp/Services/DataLocalService.cs @@ -183,19 +183,15 @@ namespace myBlazorApp.Services return Task.FromResult(items); } - public async Task> FullList(int pageSize) - { - 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")).Take(pageSize).ToList(); + public async Task> GetInventoryItems() + { + return new List(); + } + + public async Task UpdateInventory(string[] infos) + { + } } } - diff --git a/myBlazorApp/myBlazorApp/Services/IDataService.cs b/myBlazorApp/myBlazorApp/Services/IDataService.cs index 07d07fd..a455022 100644 --- a/myBlazorApp/myBlazorApp/Services/IDataService.cs +++ b/myBlazorApp/myBlazorApp/Services/IDataService.cs @@ -9,11 +9,12 @@ namespace myBlazorApp.Services Task Add(ItemModel model); Task Count(); Task> List(int currentPage, int pageSize); - Task> FullList(int pageSize); Task GetById(int id); Task Update(int id, ItemModel model); Task Delete(int id); Task> GetRecipes(); + Task> GetInventoryItems(); + Task UpdateInventory(string[] infos); } }