summaryrefslogtreecommitdiff
path: root/Controllers/ApiTagController.cs
blob: afd5b055245b5a15cd7d9413cb1c15df79a61d91 (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
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace HyperBooru.Controllers;

[ApiController]
[Route("/api/tag")]
public class ApiTagController : Controller {
    private IDbContextFactory<HBContext> dbFactory;

    public ApiTagController(IDbContextFactory<HBContext> dbFactory) =>
        this.dbFactory = dbFactory;

    [HttpGet("definition")]
    public async Task<IActionResult> GetAllTagDefinitionsAsync() {
        using var db = dbFactory.CreateDbContext();

        var definitions = await db.TagDefinitions
            .Include(td => td.ImplicitTags)
            .Select(td => (ApiModels.TagDefinition)td)
            .ToArrayAsync();

        return Ok(definitions);
    }

    [HttpGet("definition/{tagDefinitionId}")]
    public async Task<IActionResult> GetTagDefinitionAsync([FromRoute] Guid tagDefinitionId) {
        using var db = dbFactory.CreateDbContext();

        var tagDefinition = await db.TagDefinitions
            .Include(td => td.ImplicitTags)
            .FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);

        return tagDefinition is not null ? Ok(tagDefinition) : NotFound();
    }
}