summaryrefslogtreecommitdiff
path: root/Pages/Index.cshtml.cs
blob: 07d24f0617edd8ee76daec7cd2a870524c2adb63 (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 IndexModel : PageModel {
    public IEnumerable<DbMedia> Media { get; private set; }

    private HyperBooruDbContext db;

    public IndexModel(HyperBooruDbContext db) =>
        this.db = db;

    public void OnGet([FromQuery(Name = "q")] string? query) {
        IEnumerable<DbMedia> media;

        if(query is null) {
            media = db.UploadedFiles
                .OrderByDescending(uf => uf.UploadTime)
                .Select(uf => uf.Media)
                .Distinct();
        } else {
            query = query.ToLower();

            var matchingTags = db.TagDefinitions
            .Where(td => td.Name.ToLower().Contains(query))
            .Select(td => td.Guid);

            media = db.Media
                .Where(m =>
                    m.Tags
                        .Select(t => t.TagDefinition.Guid)
                        .Intersect(matchingTags)
                        .Any());
        }

        Media = media.OrderByDescending(m =>
            m.UploadedFiles
                .Select(uf => uf.UploadTime)
                .Order()
                .Last());
    }
}