summaryrefslogtreecommitdiff
path: root/src/marlin.web/Data/ApplicationDbContext.cs
blob: cbff18670055ecf3b2e642b21d80ba93d7bb03bf (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
namespace marlin.web.Data;

using System.Linq.Expressions;
using System.Text.Json;
using core;
using core.functional;
using core.models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Models;

// user-based session grant
// agent id/token grant
// agent job claim session grant
// service grant
public interface IActiveGrant
{
    bool CanAccessSource(Guid sourceId);
}

public class ApplicationDbContext : DbContext
{
    public DbSet<VersionControlSource> Sources => this.Set<VersionControlSource>();
    public DbSet<VersionControlledBranch> SourceBranches => this.Set<VersionControlledBranch>();
    public DbSet<VersionControlledCommit> SourceCommits => this.Set<VersionControlledCommit>();
    public DbSet<VersionControlledBlob> SourceBlobs => this.Set<VersionControlledBlob>();
    public DbSet<VersionControlSecret> Secrets => this.Set<VersionControlSecret>();
    public DbSet<Job> Jobs => this.Set<Job>();
    public DbSet<JobStatusUpdate> JobStatusUpdates => this.Set<JobStatusUpdate>();
    public DbSet<Agent> Agents => this.Set<Agent>();
    public DbSet<WorkflowSnapshot> WorkflowSnapshots => this.Set<WorkflowSnapshot>();
    public DbSet<LogEventRecord> Logs => this.Set<LogEventRecord>();
    
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Agent>()
            .Property(p => p.Capabilities)
            .HasJsonConversion();

        modelBuilder
            .Entity<VersionControlledCommit>()
            .HasAlternateKey(x => new { x.SourceId, x.Hash });

        modelBuilder
            .Entity<VersionControlSecret>()
            .Property(p => p.Content)
            .HasJsonConversion();

        modelBuilder
            .Entity<VersionControlledCommit>()
            .HasOne(x => x.WorkflowSnapshot)
            .WithOne(x => x.Commit)
            .HasForeignKey<WorkflowSnapshot>(x => x.CommitId);

        modelBuilder
            .Entity<WorkflowSnapshot>()
            .Property(p => p.Workflow)
            .HasJsonConversion();

        base.OnModelCreating(modelBuilder);
    }
}