summaryrefslogtreecommitdiff
path: root/Pages/ViewMedia.cshtml.cs
blob: 76c515b31fc7a014f07434cde9140e7299be49fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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<DbTagDefinition> 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<DbTagDefinition> GetTagRecursive(IEnumerable<DbTagDefinition> tagDefs) =>
        tagDefs
            .Concat(tagDefs.SelectMany(td => GetTagRecursive(td.ImplicitTags)))
            .DistinctBy(td => td.Guid);
}