Factory pattern created and implemented in Edit.cs

tpBlazor
runtenick 2 years ago
parent ed8e380f4e
commit 076c444c70

@ -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;
}
}
}

@ -1,4 +1,5 @@
using BlazorTp1.Models; using BlazorTp1.Factories;
using BlazorTp1.Models;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Components.Forms;
@ -49,17 +50,7 @@ namespace BlazorTp1.Pages
} }
// Set the model with the item // Set the model with the item
itemModel = new ItemModel itemModel = ItemFactory.ToModel(item, fileContent);
{
Id = item.Id,
DisplayName = item.DisplayName,
Name = item.Name,
RepairWith = item.RepairWith,
EnchantCategories = item.EnchantCategories,
MaxDurability = item.MaxDurability,
StackSize = item.StackSize,
ImageContent = fileContent
};
} }
private async void HandleValidSubmit() private async void HandleValidSubmit()

@ -1,4 +1,5 @@
using Blazored.LocalStorage; using Blazored.LocalStorage;
using BlazorTp1.Factories;
using BlazorTp1.Models; using BlazorTp1.Models;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -30,17 +31,7 @@ public class DataLocalService : IDataService
model.Id = currentData.Max(s => s.Id) + 1; model.Id = currentData.Max(s => s.Id) + 1;
// Add the item to the current data // Add the item to the current data
currentData.Add(new Item currentData.Add(ItemFactory.Create(model));
{
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 // Save the image
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images"); var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
@ -51,8 +42,6 @@ public class DataLocalService : IDataService
imagePathInfo.Create(); imagePathInfo.Create();
} }
// Determine the image name // Determine the image name
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png"); var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
@ -144,13 +133,7 @@ public class DataLocalService : IDataService
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent); await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
// Modify the content of the item // Modify the content of the item
item.DisplayName = model.DisplayName; ItemFactory.Update(item, model);
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 // Save the data
await _localStorage.SetItemAsync("data", currentData); await _localStorage.SetItemAsync("data", currentData);

Loading…
Cancel
Save