diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index ecde1df..7268f8d 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -51,6 +51,8 @@ public class DataLocalService : IDataService imagePathInfo.Create(); } + + // Determine the image name var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); @@ -81,4 +83,76 @@ public class DataLocalService : IDataService 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}"); + } + + // 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); + } } \ No newline at end of file diff --git a/BlazorTp1/Services/IDataService.cs b/BlazorTp1/Services/IDataService.cs index 1b5ac23..c489a15 100644 --- a/BlazorTp1/Services/IDataService.cs +++ b/BlazorTp1/Services/IDataService.cs @@ -8,4 +8,8 @@ public interface IDataService Task Count(); Task> List(int currentPage, int pageSize); + + Task GetById(int id); + + Task Update(int id, ItemModel item); }