using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace HyperBooru.Pages; public class ViewMediaModel : PageModel { public DbMedia Media { get; private set; } public DbTagDefinition[] UserTags { get; private set; } public IEnumerable TagDefinitions => db.TagDefinitions.Where(td => td.Source == TagSource.UserTag); private HyperBooruDbContext db; public ViewMediaModel(HyperBooruDbContext db) => this.db = db; public IActionResult OnGet([FromQuery(Name = "m")] Guid mediaId) { Media = db.Media.First(m => m.Guid == mediaId); if(Media is null) return NotFound(); UserTags = GetTagRecursive( Media.Tags .Select(t => t.TagDefinition)) .OrderBy(td => td.Namespace) .ThenBy(td => td.Name) .ToArray(); return Page(); } public bool IsImplicit(DbTagDefinition tagDef) => !Media.Tags .Select(t => t.TagDefinition.Guid) .Contains(tagDef.Guid); private IEnumerable GetTagRecursive(IEnumerable tagDefs) => tagDefs .Concat(tagDefs.SelectMany(td => GetTagRecursive(td.ImplicitTags))) .DistinctBy(td => td.Guid); }