Skip to content

Commit

Permalink
add database and GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Sep 9, 2024
1 parent 61933e0 commit 67c81c4
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 32 deletions.
1 change: 1 addition & 0 deletions Sherlog.Server/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class Config
public string SmtpUser { get; set; }
public string SmtpPassword { get; set; }
public string MailFrom { get; set; }
public string MailReplyTo { get; set; }
}
1 change: 0 additions & 1 deletion Sherlog.Server/LogEntryDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace Sherlog.Server;

public class LogEntryDto
{
public int Id { get; set; }
public string Tenant { get; set; }
public List<string> RecipientGroups { get; set; }
public string LogType { get; set; }
Expand Down
20 changes: 17 additions & 3 deletions Sherlog.Server/LogbookController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Net;
using System.Net.Mail;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Sherlog.Server;

Expand All @@ -19,6 +20,20 @@ public LogbookController()
public async Task<ActionResult> AddLogEntry(LogEntryDto logEntry)
{
// Save the entry to database
await using LogbookContext db = new LogbookContext();

var dbEntry = new LogEntry
{
Tenant = logEntry.Tenant,
Project = logEntry.Project,
Message = logEntry.Message,
LogType = logEntry.LogType,
RecipientGroups = string.Join(",", logEntry.RecipientGroups),
CreatedAt = DateTime.Now,
};
db.LogEntries.Add(dbEntry);
await db.SaveChangesAsync();


// Get all contacts
List<Contact> contacts = new();
Expand Down Expand Up @@ -53,6 +68,7 @@ public async Task<ActionResult> AddLogEntry(LogEntryDto logEntry)
mail.To.Add(contact.Email);
}
mail.CC.Add(Program.Configuration.MailFrom);
mail.ReplyToList.Add(Program.Configuration.MailReplyTo);
mail.Subject = subject;
mail.Body = message;

Expand All @@ -62,9 +78,7 @@ public async Task<ActionResult> AddLogEntry(LogEntryDto logEntry)
smtpServer.EnableSsl = true;

smtpServer.Send(mail);


return CreatedAtAction(nameof(AddLogEntry), new { id = logEntry.Id }, logEntry);
return CreatedAtAction(nameof(AddLogEntry), new { id = dbEntry.LogEntryId }, logEntry);
}

[HttpGet("get-tenants")]
Expand Down
59 changes: 59 additions & 0 deletions Sherlog.Server/Migrations/20240909164415_Initial.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions Sherlog.Server/Migrations/20240909164415_Initial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Sherlog.Server.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "LogEntries",
columns: table => new
{
LogEntryId = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Tenant = table.Column<string>(type: "TEXT", nullable: false),
RecipientGroups = table.Column<string>(type: "TEXT", nullable: false),
LogType = table.Column<string>(type: "TEXT", nullable: false),
Project = table.Column<string>(type: "TEXT", nullable: false),
Message = table.Column<string>(type: "TEXT", nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_LogEntries", x => x.LogEntryId);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "LogEntries");
}
}
}
56 changes: 56 additions & 0 deletions Sherlog.Server/Migrations/LogbookContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Sherlog.Server;

#nullable disable

namespace Sherlog.Server.Migrations
{
[DbContext(typeof(LogbookContext))]
partial class LogbookContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.8");

modelBuilder.Entity("Sherlog.Server.LogEntry", b =>
{
b.Property<int>("LogEntryId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("LogType")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Message")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Project")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("RecipientGroups")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Tenant")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("LogEntryId");
b.ToTable("LogEntries");
});
#pragma warning restore 612, 618
}
}
}
34 changes: 34 additions & 0 deletions Sherlog.Server/Pages/Logbook.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@page
@model LogbookModel
@{
ViewData["Title"] = "DevOps Logbook";
}
<h1>@ViewData["Title"]</h1>

<p>This page shows sent log events:</p>

<table class="table">
<thead>
<tr>
<th>Date/Time</th>
<th>Tenant</th>
<th>Project</th>
<th>Type</th>
<th>Recipient Groups</th>
<th>Message</th>
</tr>
</thead>
<tbody>
@foreach (var le in Model.LogEntries)
{
<tr>
<td>@le.CreatedAt.ToString("U")</td>
<td>@le.Tenant</td>
<td>@le.Project</td>
<td>@le.LogType</td>
<td>@le.RecipientGroups</td>
<td>@le.Message</td>
</tr>
}
</tbody>
</table>
23 changes: 23 additions & 0 deletions Sherlog.Server/Pages/Logbook.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace Sherlog.Server.Pages;

public class LogbookModel : PageModel
{
private readonly ILogger<LogbookModel> _logger;

public LogbookModel(ILogger<LogbookModel> logger)
{
_logger = logger;
}

public async Task OnGetAsync()
{
var db = new LogbookContext();
LogEntries = await db.LogEntries.ToListAsync();
}

public List<LogEntry> LogEntries { get; set; }
}
8 changes: 0 additions & 8 deletions Sherlog.Server/Pages/Privacy.cshtml

This file was deleted.

18 changes: 0 additions & 18 deletions Sherlog.Server/Pages/Privacy.cshtml.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Sherlog.Server/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
<a class="nav-link text-dark" asp-area="" asp-page="/Logbook">Logbook</a>
</li>
</ul>
</div>
Expand Down
41 changes: 40 additions & 1 deletion Sherlog.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Microsoft.EntityFrameworkCore;

namespace Sherlog.Server;

public class Program
Expand All @@ -12,6 +14,17 @@ public static void Main(string[] args)
Configuration.SmtpUser = Environment.GetEnvironmentVariable("SMTP_USER");
Configuration.SmtpPassword = Environment.GetEnvironmentVariable("SMTP_PASSWORD");
Configuration.MailFrom = Environment.GetEnvironmentVariable("MAIL_FROM");
Configuration.MailReplyTo = Environment.GetEnvironmentVariable("MAIL_REPLYTO");

// Database migration
Console.WriteLine("Running Database migrations...");
using (var db = new LogbookContext())
{
db.Database.Migrate();
}

Console.WriteLine("Migrations complete.");


var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -39,4 +52,30 @@ public static void Main(string[] args)
app.Run();
}

}
}

public class LogbookContext
: DbContext
{
public DbSet<LogEntry> LogEntries { get; set; }

public string DbPath { get; set; }
public LogbookContext()
{
DbPath = Path.Combine(Environment.GetEnvironmentVariable("DB_PATH") ?? ".","sherlog.db");
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");
}

public class LogEntry
{
public int LogEntryId { get; set; }
public string Tenant { get; set; }
public string RecipientGroups { get; set; }
public string LogType { get; set; }
public string Project { get; set; }
public string Message { get; set; }
public DateTime CreatedAt { get; set; }
}
8 changes: 8 additions & 0 deletions Sherlog.Server/Sherlog.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
</ItemGroup>

</Project>

0 comments on commit 67c81c4

Please sign in to comment.