86 lines
2.3 KiB
C#
86 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using YAPContracts.AdapterContracts;
|
|
using YAPContracts.BindingModels;
|
|
using YAPContracts.Enums;
|
|
using YAPDatabase;
|
|
using YAPDatabase.Models;
|
|
|
|
namespace YAPWebApplication.Pages.Views.Components
|
|
{
|
|
[Authorize(Roles = "Storekeeper")]
|
|
internal class EditModel : PageModel
|
|
{
|
|
private readonly IComponentAdapter _adapter;
|
|
|
|
public EditModel(IComponentAdapter adapter)
|
|
{
|
|
_adapter = adapter;
|
|
}
|
|
public IEnumerable<SelectListItem>? ComponentTypeList { get; set; }
|
|
|
|
|
|
[BindProperty]
|
|
public ComponentBindingModel Component { get; set; } = default!;
|
|
|
|
public IActionResult OnGet(string id)
|
|
{
|
|
if (id == null)
|
|
return NotFound();
|
|
|
|
var component = _adapter.GetComponentByData(id);
|
|
if (component == null)
|
|
return NotFound();
|
|
LoadComponentTypeList();
|
|
|
|
Component = new ComponentBindingModel
|
|
{
|
|
Id = component.Id,
|
|
Name = component.Name,
|
|
ComponentType = component.ComponentType.ToString(),
|
|
};
|
|
|
|
return Page();
|
|
}
|
|
|
|
public IActionResult OnPost()
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
LoadComponentTypeList();
|
|
return Page();
|
|
}
|
|
try
|
|
{
|
|
_adapter.Update(Component);
|
|
return RedirectToPage("./Index");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ModelState.AddModelError(string.Empty, $"Ошибка: {ex.Message}");
|
|
LoadComponentTypeList();
|
|
return Page();
|
|
}
|
|
}
|
|
|
|
private void LoadComponentTypeList()
|
|
{
|
|
ComponentTypeList = Enum.GetValues(typeof(ComponentType))
|
|
.Cast<ComponentType>()
|
|
.Select(ct => new SelectListItem
|
|
{
|
|
Value = ((int)ct).ToString(),
|
|
Text = ct.ToString()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|