diff --git a/Pf2e/Web/Helpers/EntitySerializer.cs b/Pf2e/Web/Helpers/EntitySerializer.cs index b7de6af..2ce26b4 100644 --- a/Pf2e/Web/Helpers/EntitySerializer.cs +++ b/Pf2e/Web/Helpers/EntitySerializer.cs @@ -215,9 +215,10 @@ public class EntitySerializer { var entity = (IEntity)value; var entityType = entity.Entity.Type; + var typeName = PgNameHelper.GetPgName(entityType); var slug = entity.Entity.Slug; var name = entity.Entity.Name[LocaleType.English]; - return ($"{name}", true); + return ($"{name}", true); } // Обработка коллекций diff --git a/Pf2e/Web/Helpers/PgNameHelper.cs b/Pf2e/Web/Helpers/PgNameHelper.cs new file mode 100644 index 0000000..091c31a --- /dev/null +++ b/Pf2e/Web/Helpers/PgNameHelper.cs @@ -0,0 +1,17 @@ +using System; +using System.Reflection; +using NpgsqlTypes; +using Pf2e.Database.Model.Enum; + +namespace Pf2e.Web.Helpers +{ + public static class PgNameHelper + { + public static string GetPgName(EntityType entityType) + { + var fieldInfo = entityType.GetType().GetField(entityType.ToString()); + var pgNameAttribute = fieldInfo?.GetCustomAttribute(); + return pgNameAttribute?.PgName ?? entityType.ToString().ToLower(); + } + } +} diff --git a/Pf2e/Web/Pages/Entity.cshtml b/Pf2e/Web/Pages/Entity.cshtml index dd2fc53..8e84cf7 100644 --- a/Pf2e/Web/Pages/Entity.cshtml +++ b/Pf2e/Web/Pages/Entity.cshtml @@ -1,4 +1,4 @@ -@page +@page "{type}/{slug}" @model Pf2e.Web.Pages.EntityModel @{ diff --git a/Pf2e/Web/Pages/Entity.cshtml.cs b/Pf2e/Web/Pages/Entity.cshtml.cs index 802e95d..311b48b 100644 --- a/Pf2e/Web/Pages/Entity.cshtml.cs +++ b/Pf2e/Web/Pages/Entity.cshtml.cs @@ -15,6 +15,7 @@ using Pf2e.Web.Helpers; namespace Pf2e.Web.Pages { + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public class EntityModel(Pf2eDbContext context) : PageModel { private readonly Pf2eDbContext _context = context; @@ -24,27 +25,42 @@ namespace Pf2e.Web.Pages public IActionResult OnGet(string type, string slug) { - // Получаем тип сущности - EntityType entityType = EntityTypeModel.Enum(type); - - // Находим сущность по slug и типу - Entity? entity = _context.Entities.FirstOrDefault(e => - e.Slug == slug && e.Type == entityType - ); - - if (entity == null) + // Добавим отладочную информацию + System.Diagnostics.Debug.WriteLine($"EntityModel.OnGet called with type: {type}, slug: {slug}"); + + try { + // Получаем тип сущности + EntityType entityType = EntityTypeModel.Enum(type); + System.Diagnostics.Debug.WriteLine($"EntityType resolved to: {entityType}"); + + // Находим сущность по slug и типу + Entity? entity = _context.Entities.FirstOrDefault(e => + e.Slug.ToLower() == slug.ToLower() && e.Type == entityType + ); + + if (entity == null) + { + System.Diagnostics.Debug.WriteLine($"Entity not found: type={entityType}, slug={slug}"); + return NotFound(); + } + + System.Diagnostics.Debug.WriteLine($"Entity found: {entity.Id} - {entity.Name[Pf2e.Database.Model.Common.I18n.LocaleType.English]}"); + + // Создаём FullEntity + FullEntity = new(_context, entity); + + // Сериализуем для отображения + EntitySerializer serializer = new(); + SerializationResult = serializer.Serialize(FullEntity); + + return Page(); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Error in EntityModel.OnGet: {ex.Message}"); return NotFound(); } - - // Создаём FullEntity - FullEntity = new(_context, entity); - - // Сериализуем для отображения - EntitySerializer serializer = new(); - SerializationResult = serializer.Serialize(FullEntity); - - return Page(); } } } diff --git a/Pf2e/Web/Pages/EntityType.cshtml b/Pf2e/Web/Pages/EntityType.cshtml index 9316c02..0de3b66 100644 --- a/Pf2e/Web/Pages/EntityType.cshtml +++ b/Pf2e/Web/Pages/EntityType.cshtml @@ -1,4 +1,4 @@ -@page +@page "/{type}" @using Pf2e.Database.Model.Common.I18n @model EntityTypeModel @@ -45,7 +45,10 @@ @Html.DisplayFor(modelItem => item.Id) - + @{ + var typeName = Pf2e.Web.Helpers.PgNameHelper.GetPgName(item.Type); + } + @Html.DisplayFor(modelItem => item.Name[LocaleType.English]) @@ -75,14 +78,17 @@ \ No newline at end of file + diff --git a/Pf2e/Web/Pages/EntityType.cshtml.cs b/Pf2e/Web/Pages/EntityType.cshtml.cs index bbc8fad..7be9ff6 100644 --- a/Pf2e/Web/Pages/EntityType.cshtml.cs +++ b/Pf2e/Web/Pages/EntityType.cshtml.cs @@ -10,8 +10,11 @@ using Pf2e.Database; using Pf2e.Database.Model.Base; using Pf2e.Database.Model.Enum; +using Microsoft.AspNetCore.Mvc.RazorPages; + namespace Pf2e.Web.Pages { + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public class EntityTypeModel : PageModel { private readonly Pf2eDbContext _context; @@ -22,7 +25,7 @@ namespace Pf2e.Web.Pages } [BindProperty(SupportsGet = true)] - public int? PageNum { get; set; } + public int? PageNum { get; set; } = 1; public IList Entity { get; set; } = default!; @@ -34,11 +37,24 @@ namespace Pf2e.Web.Pages public const int PerPage = 30; - public void OnGet(string type) + public IActionResult OnGet(string type) { - CurrentType = Enum(type); + // Добавим отладочную информацию + System.Diagnostics.Debug.WriteLine($"EntityTypeModel.OnGet called with type: {type}"); + + try + { + CurrentType = Enum(type); + System.Diagnostics.Debug.WriteLine($"CurrentType resolved to: {CurrentType}"); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Error resolving enum: {ex.Message}"); + return NotFound(); + } int count = _context.Entities.Count(entity => entity.Type == CurrentType); + System.Diagnostics.Debug.WriteLine($"Count for {CurrentType}: {count}"); LastPage = (int)Math.Ceiling(count / (double)PerPage); @@ -47,6 +63,9 @@ namespace Pf2e.Web.Pages .Skip((CurrentPage - 1) * PerPage) .Take(PerPage) .ToList(); + + System.Diagnostics.Debug.WriteLine($"Returning page with {Entity.Count} entities"); + return Page(); } /// @@ -57,6 +76,8 @@ namespace Pf2e.Web.Pages public static T Enum(string value) where T : Enum { + System.Diagnostics.Debug.WriteLine($"Trying to resolve enum value: {value} for type {typeof(T)}"); + foreach (System.Reflection.FieldInfo field in typeof(T).GetFields()) { List variants = [field.Name]; @@ -69,8 +90,11 @@ namespace Pf2e.Web.Pages variants.Add(pgAttribute.PgName); } + System.Diagnostics.Debug.WriteLine($"Checking field: {field.Name}, variants: {string.Join(", ", variants)}"); + if (variants.Contains(value)) { + System.Diagnostics.Debug.WriteLine($"Matched field: {field.Name}"); return (T)field.GetValue(null)!; } } diff --git a/Pf2e/Web/Pages/Index.cshtml b/Pf2e/Web/Pages/Index.cshtml index 3c49a0e..bf53529 100644 --- a/Pf2e/Web/Pages/Index.cshtml +++ b/Pf2e/Web/Pages/Index.cshtml @@ -21,7 +21,10 @@ { - + @{ + var typeName = Pf2e.Web.Helpers.PgNameHelper.GetPgName(row.Key); + } + @row.Key @@ -29,4 +32,4 @@ } - \ No newline at end of file +