Работа над маршрутизацией
This commit is contained in:
@@ -215,9 +215,10 @@ public class EntitySerializer
|
|||||||
{
|
{
|
||||||
var entity = (IEntity)value;
|
var entity = (IEntity)value;
|
||||||
var entityType = entity.Entity.Type;
|
var entityType = entity.Entity.Type;
|
||||||
|
var typeName = PgNameHelper.GetPgName(entityType);
|
||||||
var slug = entity.Entity.Slug;
|
var slug = entity.Entity.Slug;
|
||||||
var name = entity.Entity.Name[LocaleType.English];
|
var name = entity.Entity.Name[LocaleType.English];
|
||||||
return ($"<a href=\"/Entity/{entityType}/{slug}\">{name}</a>", true);
|
return ($"<a href=\"/{typeName}/{slug}\">{name}</a>", true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Обработка коллекций
|
// Обработка коллекций
|
||||||
|
|||||||
@@ -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<PgNameAttribute>();
|
||||||
|
return pgNameAttribute?.PgName ?? entityType.ToString().ToLower();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
@page
|
@page "{type}/{slug}"
|
||||||
@model Pf2e.Web.Pages.EntityModel
|
@model Pf2e.Web.Pages.EntityModel
|
||||||
|
|
||||||
@{
|
@{
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ using Pf2e.Web.Helpers;
|
|||||||
|
|
||||||
namespace Pf2e.Web.Pages
|
namespace Pf2e.Web.Pages
|
||||||
{
|
{
|
||||||
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||||
public class EntityModel(Pf2eDbContext context) : PageModel
|
public class EntityModel(Pf2eDbContext context) : PageModel
|
||||||
{
|
{
|
||||||
private readonly Pf2eDbContext _context = context;
|
private readonly Pf2eDbContext _context = context;
|
||||||
@@ -23,20 +24,29 @@ namespace Pf2e.Web.Pages
|
|||||||
public EntitySerializer.SerializationResult SerializationResult { get; set; } = null!;
|
public EntitySerializer.SerializationResult SerializationResult { get; set; } = null!;
|
||||||
|
|
||||||
public IActionResult OnGet(string type, string slug)
|
public IActionResult OnGet(string type, string slug)
|
||||||
|
{
|
||||||
|
// Добавим отладочную информацию
|
||||||
|
System.Diagnostics.Debug.WriteLine($"EntityModel.OnGet called with type: {type}, slug: {slug}");
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
// Получаем тип сущности
|
// Получаем тип сущности
|
||||||
EntityType entityType = EntityTypeModel.Enum<EntityType>(type);
|
EntityType entityType = EntityTypeModel.Enum<EntityType>(type);
|
||||||
|
System.Diagnostics.Debug.WriteLine($"EntityType resolved to: {entityType}");
|
||||||
|
|
||||||
// Находим сущность по slug и типу
|
// Находим сущность по slug и типу
|
||||||
Entity? entity = _context.Entities.FirstOrDefault(e =>
|
Entity? entity = _context.Entities.FirstOrDefault(e =>
|
||||||
e.Slug == slug && e.Type == entityType
|
e.Slug.ToLower() == slug.ToLower() && e.Type == entityType
|
||||||
);
|
);
|
||||||
|
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
{
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Entity not found: type={entityType}, slug={slug}");
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Entity found: {entity.Id} - {entity.Name[Pf2e.Database.Model.Common.I18n.LocaleType.English]}");
|
||||||
|
|
||||||
// Создаём FullEntity
|
// Создаём FullEntity
|
||||||
FullEntity = new(_context, entity);
|
FullEntity = new(_context, entity);
|
||||||
|
|
||||||
@@ -46,5 +56,11 @@ namespace Pf2e.Web.Pages
|
|||||||
|
|
||||||
return Page();
|
return Page();
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Error in EntityModel.OnGet: {ex.Message}");
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
@page
|
@page "/{type}"
|
||||||
@using Pf2e.Database.Model.Common.I18n
|
@using Pf2e.Database.Model.Common.I18n
|
||||||
@model EntityTypeModel
|
@model EntityTypeModel
|
||||||
|
|
||||||
@@ -45,7 +45,10 @@
|
|||||||
@Html.DisplayFor(modelItem => item.Id)
|
@Html.DisplayFor(modelItem => item.Id)
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a asp-page="./Entity" asp-route-type="@item.Type" asp-route-slug="@item.Slug">
|
@{
|
||||||
|
var typeName = Pf2e.Web.Helpers.PgNameHelper.GetPgName(item.Type);
|
||||||
|
}
|
||||||
|
<a href="/@typeName/@item.Slug">
|
||||||
@Html.DisplayFor(modelItem => item.Name[LocaleType.English])
|
@Html.DisplayFor(modelItem => item.Name[LocaleType.English])
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
@@ -75,14 +78,17 @@
|
|||||||
<nav aria-label="Page navigation example">
|
<nav aria-label="Page navigation example">
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
<li class="page-item @(Model.CurrentPage == 1 ? "disabled" : "")">
|
<li class="page-item @(Model.CurrentPage == 1 ? "disabled" : "")">
|
||||||
<a class="page-link" asp-page="./EntityType" asp-route-type="@Model.CurrentType" asp-route-pageNum="@(Model.CurrentPage - 1)">Previous</a>
|
@{
|
||||||
|
var currentTypeName = Pf2e.Web.Helpers.PgNameHelper.GetPgName(Model.CurrentType);
|
||||||
|
}
|
||||||
|
<a class="page-link" href="/@currentTypeName?pageNum=@(Model.CurrentPage - 1)">Previous</a>
|
||||||
</li>
|
</li>
|
||||||
@foreach (var num in Model.Pagination)
|
@foreach (var num in Model.Pagination)
|
||||||
{
|
{
|
||||||
@if (num == Model.CurrentPage)
|
@if (num == Model.CurrentPage)
|
||||||
{
|
{
|
||||||
<li class="page-item active">
|
<li class="page-item active">
|
||||||
<a class="page-link" asp-page="./EntityType" asp-route-type="@Model.CurrentType" asp-route-pageNum="@(num)">@num</a>
|
<a class="page-link" href="/@currentTypeName?pageNum=@(num)">@num</a>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
else if (num == -1)
|
else if (num == -1)
|
||||||
@@ -94,12 +100,12 @@
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
<li class="page-item">
|
<li class="page-item">
|
||||||
<a class="page-link" asp-page="./EntityType" asp-route-type="@Model.CurrentType" asp-route-pageNum="@(num)">@num</a>
|
<a class="page-link" href="/@currentTypeName?pageNum=@(num)">@num</a>
|
||||||
</li>
|
</li>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
<li class="page-item @(Model.CurrentPage == Model.LastPage ? "disabled" : "")">
|
<li class="page-item @(Model.CurrentPage == Model.LastPage ? "disabled" : "")">
|
||||||
<a class="page-link" asp-page="./EntityType" asp-route-type="@Model.CurrentType" asp-route-pageNum="@(Model.CurrentPage + 1)">Next</a>
|
<a class="page-link" href="/@currentTypeName?pageNum=@(Model.CurrentPage + 1)">Next</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
@@ -10,8 +10,11 @@ using Pf2e.Database;
|
|||||||
using Pf2e.Database.Model.Base;
|
using Pf2e.Database.Model.Base;
|
||||||
using Pf2e.Database.Model.Enum;
|
using Pf2e.Database.Model.Enum;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
namespace Pf2e.Web.Pages
|
namespace Pf2e.Web.Pages
|
||||||
{
|
{
|
||||||
|
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||||
public class EntityTypeModel : PageModel
|
public class EntityTypeModel : PageModel
|
||||||
{
|
{
|
||||||
private readonly Pf2eDbContext _context;
|
private readonly Pf2eDbContext _context;
|
||||||
@@ -22,7 +25,7 @@ namespace Pf2e.Web.Pages
|
|||||||
}
|
}
|
||||||
|
|
||||||
[BindProperty(SupportsGet = true)]
|
[BindProperty(SupportsGet = true)]
|
||||||
public int? PageNum { get; set; }
|
public int? PageNum { get; set; } = 1;
|
||||||
|
|
||||||
public IList<Entity> Entity { get; set; } = default!;
|
public IList<Entity> Entity { get; set; } = default!;
|
||||||
|
|
||||||
@@ -34,11 +37,24 @@ namespace Pf2e.Web.Pages
|
|||||||
|
|
||||||
public const int PerPage = 30;
|
public const int PerPage = 30;
|
||||||
|
|
||||||
public void OnGet(string type)
|
public IActionResult OnGet(string type)
|
||||||
|
{
|
||||||
|
// Добавим отладочную информацию
|
||||||
|
System.Diagnostics.Debug.WriteLine($"EntityTypeModel.OnGet called with type: {type}");
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
CurrentType = Enum<EntityType>(type);
|
CurrentType = Enum<EntityType>(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);
|
int count = _context.Entities.Count(entity => entity.Type == CurrentType);
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Count for {CurrentType}: {count}");
|
||||||
|
|
||||||
LastPage = (int)Math.Ceiling(count / (double)PerPage);
|
LastPage = (int)Math.Ceiling(count / (double)PerPage);
|
||||||
|
|
||||||
@@ -47,6 +63,9 @@ namespace Pf2e.Web.Pages
|
|||||||
.Skip((CurrentPage - 1) * PerPage)
|
.Skip((CurrentPage - 1) * PerPage)
|
||||||
.Take(PerPage)
|
.Take(PerPage)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Returning page with {Entity.Count} entities");
|
||||||
|
return Page();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -57,6 +76,8 @@ namespace Pf2e.Web.Pages
|
|||||||
public static T Enum<T>(string value)
|
public static T Enum<T>(string value)
|
||||||
where T : Enum
|
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())
|
foreach (System.Reflection.FieldInfo field in typeof(T).GetFields())
|
||||||
{
|
{
|
||||||
List<string> variants = [field.Name];
|
List<string> variants = [field.Name];
|
||||||
@@ -69,8 +90,11 @@ namespace Pf2e.Web.Pages
|
|||||||
variants.Add(pgAttribute.PgName);
|
variants.Add(pgAttribute.PgName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Checking field: {field.Name}, variants: {string.Join(", ", variants)}");
|
||||||
|
|
||||||
if (variants.Contains(value))
|
if (variants.Contains(value))
|
||||||
{
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Matched field: {field.Name}");
|
||||||
return (T)field.GetValue(null)!;
|
return (T)field.GetValue(null)!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,10 @@
|
|||||||
{
|
{
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<a asp-page="./EntityType" asp-route-type="@row.Key">
|
@{
|
||||||
|
var typeName = Pf2e.Web.Helpers.PgNameHelper.GetPgName(row.Key);
|
||||||
|
}
|
||||||
|
<a href="/@typeName">
|
||||||
@row.Key
|
@row.Key
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user