summaryrefslogtreecommitdiff
path: root/Services/MediaService.cs
blob: be2657ab911365c6cf201df2230ae7ccddff8ae6 (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
using Microsoft.EntityFrameworkCore;

namespace HyperBooru.Services;

public interface IMediaService {
    public void SetIngest(Media media, bool ingest);
}

public class MediaService : IMediaService {
    private IDbContextFactory<HBContext> dbFactory;

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

    public void SetIngest(Media media, bool ingest) {
        using var db = dbFactory.CreateDbContext();
        var ingestTag = db.TagDefinitions
            .First(td => td.Source == TagSource.Internal && td.Name == "ingest");

        if(ingest)
            media.Tags.Add(new() { TagDefinition = ingestTag });
        else
            media.Tags.Remove(
                media.Tags.First(t => t.TagDefinition.Guid == ingestTag.Guid));

        db.SaveChanges();
    }
}