Spaces:
Running
Running
| using BakeryErp.Infrastructure.Database; | |
| using BakeryErp.Infrastructure.Repositories; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.RazorPages; | |
| namespace BakeryErp.Web.Pages; | |
| public sealed class ItemsModel : PageModel | |
| { | |
| private readonly ItemRepository _itemRepository; | |
| public ItemsModel(ItemRepository itemRepository) => _itemRepository = itemRepository; | |
| [] public LocalItem Form { get; set; } = new(); | |
| [] public string? Message { get; set; } | |
| [] public string? ErrorMessage { get; set; } | |
| public IReadOnlyList<LocalItem> Items { get; private set; } = []; | |
| public void OnGet(Guid? editId) | |
| { | |
| Items = _itemRepository.List(); | |
| Form = editId is null ? new LocalItem() : _itemRepository.Find(editId.Value) ?? new LocalItem(); | |
| } | |
| public IActionResult OnPostSave() | |
| { | |
| var result = _itemRepository.Save(Form); | |
| if (!result.IsSuccess) { ErrorMessage = result.ErrorMessage; return RedirectToPage(); } | |
| Message = "品項已儲存。"; | |
| return RedirectToPage(); | |
| } | |
| public IActionResult OnPostToggle(Guid id, bool isActive) | |
| { | |
| _itemRepository.SetActive(id, isActive); | |
| Message = isActive ? "品項已啟用。" : "品項已停用。"; | |
| return RedirectToPage(); | |
| } | |
| public string GetItemTypeName(LocalItemType type) => type switch | |
| { | |
| LocalItemType.RawMaterial => "原物料", | |
| LocalItemType.SemiFinished => "半成品", | |
| LocalItemType.FinishedProduct => "成品", | |
| LocalItemType.Packaging => "包材", | |
| LocalItemType.ExpenseSupply => "耗材", | |
| _ => type.ToString() | |
| }; | |
| } | |