~jpgleeson/caint

c0919f8167ba786387da6cc06696ed0348d545ae — jpgleeson 1 year, 5 months ago 82c2441
Move to SQLite as database for now.

Changed to sqlite. Also removed admin stuff from the regular js file. DB migrations added.
M Controllers/CommentsController.cs => Controllers/CommentsController.cs +5 -4
@@ 6,6 6,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using Microsoft.EntityFrameworkCore;
using caint.Data;
using caint.Models;

namespace caint.Controllers


@@ 14,9 15,9 @@ namespace caint.Controllers
    [ApiController]
    public class CommentsController : ControllerBase
    {
        private readonly CommentContext _context;
        private readonly caintDBContext _context;

        public CommentsController(CommentContext context)
        public CommentsController(caintDBContext context)
        {
            _context = context;
        }


@@ 77,7 78,7 @@ namespace caint.Controllers
            return ItemToDTO(comment);
        }

        [EnableCors("ExternalCORS")]
        //[EnableCors]
        [HttpGet("thread/{id}")]
        public async Task<ActionResult<IEnumerable<Comment>>> GetCommentThread(long id)
        {


@@ 133,7 134,7 @@ namespace caint.Controllers

        // POST: api/Comments
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [EnableCors("ExternalCORS")]
        //[EnableCors]
        [HttpPost]
        public async Task<ActionResult<CommentDTO>> PostComment(CommentDTO commentDTO)
        {

M Controllers/ThreadsController.cs => Controllers/ThreadsController.cs +13 -3
@@ 6,6 6,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using Microsoft.EntityFrameworkCore;
using caint.Data;
using caint.Models;

namespace caint.Controllers


@@ 14,14 15,22 @@ namespace caint.Controllers
    [ApiController]
    public class ThreadsController : ControllerBase
    {
        private readonly ThreadContext _context;
        private readonly caintDBContext _context;

        public ThreadsController(ThreadContext context)
        public ThreadsController(caintDBContext context)
        {
            _context = context;
        }

        [HttpOptions]
        public IActionResult PreflightRoute()
        {
            Console.WriteLine("preflight");
            return NoContent();
        }

        // GET: api/threads
        [EnableCors]
        [HttpGet]
        public async Task<ActionResult<IEnumerable<ThreadDTO>>> GetThread(string hostname, string pathname)
        {


@@ 29,6 38,7 @@ namespace caint.Controllers
        }

        // GET: api/threads/5
        [EnableCors]
        [HttpGet("{id}")]
        public async Task<ActionResult<ThreadDTO>> GetComment(long id)
        {


@@ 44,7 54,7 @@ namespace caint.Controllers

        // POST: api/threads
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [EnableCors("ExternalCORS")]
        [EnableCors]
        [HttpPost]
        public async Task<long> NewThread(NewThreadDTO newThread)
        {

R Models/CommentContext.cs => Data/caintDBContext.cs +5 -3
@@ 1,14 1,16 @@
using Microsoft.EntityFrameworkCore;
using caint.Models;

namespace caint.Models
namespace caint.Data
{
    public class CommentContext : DbContext
    public class caintDBContext : DbContext
    {
        public CommentContext(DbContextOptions<CommentContext> options) : base(options)
        public caintDBContext(DbContextOptions<caintDBContext> options) : base(options)
        {

        }

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

A Migrations/20210219180922_InitialCreate.Designer.cs => Migrations/20210219180922_InitialCreate.Designer.cs +62 -0
@@ 0,0 1,62 @@
// <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("20210219180922_InitialCreate")]
    partial class InitialCreate
    {
        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.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/20210219180922_InitialCreate.cs => Migrations/20210219180922_InitialCreate.cs +49 -0
@@ 0,0 1,49 @@
using Microsoft.EntityFrameworkCore.Migrations;

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

            migrationBuilder.CreateTable(
                name: "threads",
                columns: table => new
                {
                    id = table.Column<long>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    hostname = table.Column<string>(type: "TEXT", nullable: true),
                    path = table.Column<string>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_threads", x => x.id);
                });
        }

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

            migrationBuilder.DropTable(
                name: "threads");
        }
    }
}

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

namespace caint.Migrations
{
    [DbContext(typeof(caintDBContext))]
    partial class caintDBContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(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.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
        }
    }
}

D Models/ThreadContext.cs => Models/ThreadContext.cs +0 -14
@@ 1,14 0,0 @@
using Microsoft.EntityFrameworkCore;

namespace caint.Models
{
    public class ThreadContext : DbContext
    {
        public ThreadContext(DbContextOptions<ThreadContext> options) : base(options)
        {

        }

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

M Startup.cs => Startup.cs +40 -11
@@ 4,6 4,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;


@@ 12,7 13,9 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using caint.Data;
using caint.Models;
using Pomelo.EntityFrameworkCore.MySql;

namespace caint
{


@@ 25,24 28,48 @@ namespace caint

        public IConfiguration Configuration { get; }


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<CommentContext>(opt => opt.UseInMemoryDatabase("caintcomments"));
            services.AddDbContext<ThreadContext>(opt => opt.UseInMemoryDatabase("caintthreads"));
            services.AddControllers();
            //services.AddDbContext<CommentContext>(opt => opt.UseInMemoryDatabase("caintcomments"));
            //services.AddDbContext<ThreadContext>(opt => opt.UseInMemoryDatabase("caintthreads"));

            //SQLITE
            services.AddDbContext<caintDBContext>(options => options.UseSqlite(Configuration.GetConnectionString("caintDBContext")));

            /*services.AddDbContextPool<CommentContext>(
                dbContextOptions => dbContextOptions
                    .UseMySql(
                        // Replace with your connection string.
                        Configuration.GetConnectionString("caintDb"),
                        // Replace with your server version and type.
                        // For common usages, see pull request #1233.
                        new MySqlServerVersion(new Version(8, 0, 21))
            ));

            services.AddDbContextPool<ThreadContext>(
                dbContextOptions => dbContextOptions
                    .UseMySql(
                        // Replace with your connection string.
                        Configuration.GetConnectionString("caintDb"),
                        // Replace with your server version and type.
                        // For common usages, see pull request #1233.
                        new MySqlServerVersion(new Version(8, 0, 21))
            ));*/
            

            

            services.AddCors(options =>
        {
            options.AddPolicy("ExternalCORS",
                builder =>
            {
            options.AddDefaultPolicy(builder =>
                {
                    builder.WithOrigins("http://jpgleeson.com",
                                        "https://jpgleeson.com",
                                        "http://www.jpgleeson.com",
                                        "https://www.jpgleeson.com");
                    builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
                });
        });
            });

            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.


@@ 56,6 83,8 @@ namespace caint
            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseForwardedHeaders();

            app.UseHttpsRedirection();

            app.UseRouting();

M appsettings.Development.json => appsettings.Development.json +4 -0
@@ 5,5 5,9 @@
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings":
  {
    "caintDBContext": "Data Source=caintDB.db"
  }
}

M appsettings.json => appsettings.json +5 -1
@@ 6,5 6,9 @@
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
  "AllowedHosts": "*",
  "ConnectionStrings":
  {
    "caintDBContext": "Data Source=caintDB.db"
  }
}

M caint.csproj => caint.csproj +3 -0
@@ 7,14 7,17 @@
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SQLite" Version="5.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.0-alpha.2" />
  </ItemGroup>

</Project>

A caintDB.db => caintDB.db +0 -0
M wwwroot/css/caint.css => wwwroot/css/caint.css +15 -1
@@ 76,10 76,24 @@ button:hover, input[type="submit"]:hover{
    background-color: #efefef;
}

.commentSection{
    background-color: #fefefe;
    color: #222;
    border-top: solid;
    border-color: #dddddd;
    border-top-width: 2px;
    margin-top: 25px;
    padding-bottom: 5px;
    display: block;

    line-height: 1.5;
    font-weight: 400;
}

.commentThread{
    background-color: #fefefe;
    color: #222;
    width: 50%;
    width: 100%;
    padding-bottom: 5px;
    display: block;


M wwwroot/js/caint.js => wwwroot/js/caint.js +0 -56
@@ 145,62 145,6 @@ function _displayCount(itemCount) {
  document.getElementById('counter').innerText = `${itemCount} ${name}`;
}

function _displayItems(data) {
  const tBody = document.getElementById('comments');
  tBody.innerHTML = '';

  _displayCount(data.length);

  const button = document.createElement('button');

  data.forEach(item => {
    let isCompleteCheckbox = document.createElement('input');
    isCompleteCheckbox.type = 'checkbox';
    isCompleteCheckbox.disabled = true;
    isCompleteCheckbox.checked = item.approved;

    let approveButton = button.cloneNode(false);
    approveButton.innerText = 'Approve';
    approveButton.setAttribute('onclick', `approveItem(${item.id})`);

    let editButton = button.cloneNode(false);
    editButton.innerText = 'Edit';
    editButton.setAttribute('onclick', `displayEditForm(${item.id})`);

    let deleteButton = button.cloneNode(false);
    deleteButton.innerText = 'Delete';
    deleteButton.setAttribute('onclick', `deleteItem(${item.id})`);

    let tr = tBody.insertRow();
    
    let td1 = tr.insertCell(0);
    td1.appendChild(isCompleteCheckbox);

    let td2 = tr.insertCell(1);
    let threadIdNode = document.createTextNode(item.threadId);
    td2.appendChild(threadIdNode);

    let td3 = tr.insertCell(2);
    let commenterNode = document.createTextNode(item.name);
    td3.appendChild(commenterNode);

    let td4 = tr.insertCell(3);
    let bodyNode = document.createTextNode(item.body);
    td4.appendChild(bodyNode);

    let td5 = tr.insertCell(4);
    td5.appendChild(approveButton);

    let td6 = tr.insertCell(5);
    td6.appendChild(editButton);

    let td7 = tr.insertCell(6);
    td7.appendChild(deleteButton);
  });

  comments = data;
}

function _displayThread(data) {
  const threadBody = document.getElementById('commentThread');
  threadBody.setAttribute('class', 'commentThread');