From 076c444c705ff61da33037eb003fe193e6eed7c8 Mon Sep 17 00:00:00 2001 From: runtenick Date: Sat, 12 Nov 2022 11:16:33 +0100 Subject: [PATCH] Factory pattern created and implemented in Edit.cs --- BlazorTp1/Factories/ItemFactory.cs | 48 ++++++++++++++++++++++++++ BlazorTp1/Pages/Edit.cs | 15 ++------ BlazorTp1/Services/DataLocalService.cs | 23 ++---------- 3 files changed, 54 insertions(+), 32 deletions(-) create mode 100644 BlazorTp1/Factories/ItemFactory.cs diff --git a/BlazorTp1/Factories/ItemFactory.cs b/BlazorTp1/Factories/ItemFactory.cs new file mode 100644 index 0000000..2a7e9fe --- /dev/null +++ b/BlazorTp1/Factories/ItemFactory.cs @@ -0,0 +1,48 @@ +using BlazorTp1.Models; + +namespace BlazorTp1.Factories +{ + public static class ItemFactory + { + public static ItemModel ToModel(Item item, byte[] imageContent) + { + return new ItemModel + { + Id = item.Id, + DisplayName = item.DisplayName, + Name = item.Name, + RepairWith = item.RepairWith, + EnchantCategories = item.EnchantCategories, + MaxDurability = item.MaxDurability, + StackSize = item.StackSize, + ImageContent = imageContent + }; + } + + public static Item Create(ItemModel model) + { + return 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 + }; + } + + public static void Update(Item item, ItemModel model) + { + 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; + } + } +} diff --git a/BlazorTp1/Pages/Edit.cs b/BlazorTp1/Pages/Edit.cs index 963ff13..cdf97ab 100644 --- a/BlazorTp1/Pages/Edit.cs +++ b/BlazorTp1/Pages/Edit.cs @@ -1,4 +1,5 @@ -using BlazorTp1.Models; +using BlazorTp1.Factories; +using BlazorTp1.Models; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; @@ -49,17 +50,7 @@ namespace BlazorTp1.Pages } // Set the model with the item - itemModel = new ItemModel - { - Id = item.Id, - DisplayName = item.DisplayName, - Name = item.Name, - RepairWith = item.RepairWith, - EnchantCategories = item.EnchantCategories, - MaxDurability = item.MaxDurability, - StackSize = item.StackSize, - ImageContent = fileContent - }; + itemModel = ItemFactory.ToModel(item, fileContent); } private async void HandleValidSubmit() diff --git a/BlazorTp1/Services/DataLocalService.cs b/BlazorTp1/Services/DataLocalService.cs index 7268f8d..03de625 100644 --- a/BlazorTp1/Services/DataLocalService.cs +++ b/BlazorTp1/Services/DataLocalService.cs @@ -1,4 +1,5 @@ using Blazored.LocalStorage; +using BlazorTp1.Factories; using BlazorTp1.Models; using Microsoft.AspNetCore.Components; @@ -30,17 +31,7 @@ public class DataLocalService : IDataService 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 - }); + currentData.Add(ItemFactory.Create(model)); // Save the image var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); @@ -51,8 +42,6 @@ public class DataLocalService : IDataService imagePathInfo.Create(); } - - // Determine the image name var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); @@ -144,13 +133,7 @@ public class DataLocalService : IDataService 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; + ItemFactory.Update(item, model); // Save the data await _localStorage.SetItemAsync("data", currentData);