using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using Pf2e.Common; using Pf2e.Database; using Pf2e.Database.Model; using Pf2e.Database.Model.Base; using Pf2e.Database.Model.Enum; using Pf2e.Database.Model.Structure; using Pf2e.Web.Helpers; using Pf2e.Web.Helpers.Serialization; namespace Pf2e.Web.Pages { [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public class Entity(Pf2eDbContext context) : PageModel { private readonly Pf2eDbContext _context = context; public FullEntity FullEntity { get; set; } = null!; public SerializationResult SerializationResult { get; set; } = null!; public IActionResult OnGet(string type, string slug) { try { EntityType entityType = EntityTypeModel.Enum(type); // Находим сущность по slug и типу Database.Model.Base.Entity? entity = _context .Entities.AsNoTracking() .FirstOrDefault(e => e.Slug.ToLower() == slug.ToLower() && e.Type == entityType ); if (entity == null) { return NotFound(); } // Создаём FullEntity FullEntity = new(_context, entityType, entity.Id); // Сериализуем для отображения EntitySerialization serializer = new(); SerializationResult = serializer.Serialize(FullEntity.Entity); if (FullEntity.SubEntity is not null) { SerializationResult.ExtendWith(serializer.Serialize(FullEntity.SubEntity)); } // Set breadcrumbs string typeName = PgNameHelper.GetPgName(entityType); ViewData["Breadcrumbs"] = new List<(string, string)> { ("Home", "/"), (entityType.ToString(), $"/{typeName}"), (entity.Name[Pf2e.Database.Model.Common.I18n.LocaleType.English], ""), }; return Page(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Error in Entity.OnGet: {ex.Message}"); Console.WriteLine($"Error in Entity.OnGet: {ex.Message}"); return NotFound(); } } } }