~jpgleeson/caint

54cf4dd961eb7261b7cf1ea9bda5b9012d02d7c8 — jpgleeson 1 year, 5 months ago 783e27e
Beginning to add multi-tenant capabilities.

Basic run through of checking the tenant names against a table with approved tenants. If tenantName is found in that list, then the comment is taken into the database, even if the tenant is not active. This includes migrations to add this to the database.
M Controllers/CommentsController.cs => Controllers/CommentsController.cs +23 -4
@@ 145,11 145,30 @@ namespace caint.Controllers
                body = commentDTO.body,
                threadId = commentDTO.threadId
            };
            
            _context.comments.Add(comment);
            await _context.SaveChangesAsync();

            return CreatedAtAction(nameof(GetComment), new { id = comment.id }, ItemToDTO(comment));
            var thread = await _context.threads.FindAsync(comment.threadId);

            string tenantName = thread.hostname;

            var tenant = _context.tenants.Where(x => x.tenantName == tenantName).FirstOrDefault();
            if (tenant == null)
            {

            }
            else
            {
                if (tenant.active)
                {
                    //It might be worthwhile putting the acceptance in here so that we catch comments
                    //for users who's accounts have lapsed, but they just can't approve them from the dashboard.
                }
                _context.comments.Add(comment);
                await _context.SaveChangesAsync();

                return CreatedAtAction(nameof(GetComment), new { id = comment.id }, ItemToDTO(comment));
            }
            //Not sure if this is the best response here, as it returns a 404 to the AJAX request, but it's sufficient for now.
            return NotFound();
        }



M Data/caintDBContext.cs => Data/caintDBContext.cs +1 -0
@@ 12,5 12,6 @@ namespace caint.Data

        public DbSet<Comment> comments { get; set; }
        public DbSet<Thread> threads { get; set; }
        public DbSet<Tenant> tenants { get; set; }
    }
}
\ No newline at end of file

A Migrations/20210223194036_AddTenantTable.Designer.cs => Migrations/20210223194036_AddTenantTable.Designer.cs +79 -0
@@ 0,0 1,79 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using caint.Data;

namespace caint.Migrations
{
    [DbContext(typeof(caintDBContext))]
    [Migration("20210223194036_AddTenantTable")]
    partial class AddTenantTable
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "5.0.3");

            modelBuilder.Entity("caint.Models.Comment", b =>
                {
                    b.Property<long>("id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<bool>("approved")
                        .HasColumnType("INTEGER");

                    b.Property<string>("body")
                        .HasColumnType("TEXT");

                    b.Property<string>("name")
                        .HasColumnType("TEXT");

                    b.Property<long>("threadId")
                        .HasColumnType("INTEGER");

                    b.HasKey("id");

                    b.ToTable("comments");
                });

            modelBuilder.Entity("caint.Models.Tenant", b =>
                {
                    b.Property<long>("id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<bool>("active")
                        .HasColumnType("INTEGER");

                    b.Property<string>("tenantName")
                        .HasColumnType("TEXT");

                    b.HasKey("id");

                    b.ToTable("tenants");
                });

            modelBuilder.Entity("caint.Models.Thread", b =>
                {
                    b.Property<long>("id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<string>("hostname")
                        .HasColumnType("TEXT");

                    b.Property<string>("path")
                        .HasColumnType("TEXT");

                    b.HasKey("id");

                    b.ToTable("threads");
                });
#pragma warning restore 612, 618
        }
    }
}

A Migrations/20210223194036_AddTenantTable.cs => Migrations/20210223194036_AddTenantTable.cs +30 -0
@@ 0,0 1,30 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace caint.Migrations
{
    public partial class AddTenantTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "tenants",
                columns: table => new
                {
                    id = table.Column<long>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    tenantName = table.Column<string>(type: "TEXT", nullable: true),
                    active = table.Column<bool>(type: "INTEGER", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_tenants", x => x.id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "tenants");
        }
    }
}

M Migrations/caintDBContextModelSnapshot.cs => Migrations/caintDBContextModelSnapshot.cs +17 -0
@@ 38,6 38,23 @@ namespace caint.Migrations
                    b.ToTable("comments");
                });

            modelBuilder.Entity("caint.Models.Tenant", b =>
                {
                    b.Property<long>("id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<bool>("active")
                        .HasColumnType("INTEGER");

                    b.Property<string>("tenantName")
                        .HasColumnType("TEXT");

                    b.HasKey("id");

                    b.ToTable("tenants");
                });

            modelBuilder.Entity("caint.Models.Thread", b =>
                {
                    b.Property<long>("id")

A Models/Tenant.cs => Models/Tenant.cs +11 -0
@@ 0,0 1,11 @@
namespace caint.Models
{
    public class Tenant
    {
        public long id { get; set; }

        public string tenantName { get; set; }

        public bool active { get; set; }
    }
}
\ No newline at end of file

M caintDB.db => caintDB.db +0 -0