Skip to content

Commit

Permalink
Quick and dirty database support
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicMaas committed Jul 10, 2023
1 parent c514868 commit 4ee545e
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/Website/Common/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Website.Models.Database;

namespace Website.Common;

public class DatabaseContext : DbContext
{
public DbSet<ShortLink> ShortLinks { get; set; }

public DatabaseContext()
{ }

public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
}
37 changes: 37 additions & 0 deletions src/Website/Migrations/20230710082646_InitialCreate.Designer.cs

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

33 changes: 33 additions & 0 deletions src/Website/Migrations/20230710082646_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Website.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ShortLinks",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
RedirectLink = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ShortLinks", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShortLinks");
}
}
}
34 changes: 34 additions & 0 deletions src/Website/Migrations/DatabaseContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Website.Common;

#nullable disable

namespace Website.Migrations
{
[DbContext(typeof(DatabaseContext))]
partial class DatabaseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.8");

modelBuilder.Entity("Website.Models.Database.ShortLink", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("RedirectLink")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ShortLinks");
});
#pragma warning restore 612, 618
}
}
}
10 changes: 10 additions & 0 deletions src/Website/Models/Database/ShortLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#nullable disable

namespace Website.Models.Database;

public class ShortLink
{
public string Id { get; set; }

public string RedirectLink { get; set; }
}
23 changes: 10 additions & 13 deletions src/Website/Pages/Fw.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Website.Common;

namespace Website.Pages;

public class FwModel : PageModel
{
private readonly DatabaseContext _databaseContext;

public string? RequestedUri { get; set; }

public string? Message { get; set; }

public bool IsSuccess { get; set; }

private readonly Dictionary<string, string> _links = new()
public FwModel(DatabaseContext databaseContext)
{
{ "4toUN4j53iK", "https://github.com/DominicMaas/SpaceChunks" },
{ "yENyTb", "https://twitter.com/SoundByteUWP" },
{ "Y5jGLtoFXs", "https://dominicmaas.co.nz/privacy" },
{ "GvC5iXmJSo", "https://dominicmaas.co.nz/projects/soundbyte" },

{ "github", "https://github.com/DominicMaas" },
{ "twitter", "https://twitter.com/dominicjmaas" }
};
_databaseContext = databaseContext;
}

public void OnGet(string? id)
public async Task OnGetAsync(string? id)
{
if (string.IsNullOrEmpty(id))
{
Expand All @@ -31,9 +28,9 @@ public void OnGet(string? id)
return;
}

var url = _links.GetValueOrDefault(id);
var link = await _databaseContext.ShortLinks.FindAsync(id);

if (string.IsNullOrEmpty(url))
if (link == null || string.IsNullOrEmpty(link.RedirectLink))
{
// This URI does not exist
RequestedUri = string.Empty;
Expand All @@ -42,7 +39,7 @@ public void OnGet(string? id)
return;
}

RequestedUri = url;
RequestedUri = link.RedirectLink;
IsSuccess = true;
}
}
17 changes: 17 additions & 0 deletions src/Website/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Azure.Security.KeyVault.Secrets;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.EntityFrameworkCore;
using Website.Common;
using Website.Services;

Expand Down Expand Up @@ -34,6 +35,10 @@
});
}

// Database
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlite(config.GetConnectionString("WebsiteDatabase")));

services.AddMvc();

// Services
Expand Down Expand Up @@ -61,6 +66,10 @@
app.UseExceptionHandler("/error/500");
app.UseHsts();
}
else
{
app.UseDeveloperExceptionPage();
}

app.UseStatusCodePagesWithReExecute("/error/{0}");

Expand All @@ -84,4 +93,12 @@
await next();
});


// Run our migrations on start up
using (var serviceScope = app.Services.GetService<IServiceScopeFactory>()!.CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
context.Database.Migrate();
}

app.Run();
5 changes: 5 additions & 0 deletions src/Website/Website.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.18.1" />
</ItemGroup>

Expand Down
3 changes: 3 additions & 0 deletions src/Website/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"WebsiteDatabase": "Data Source=local.db"
}
}

0 comments on commit 4ee545e

Please sign in to comment.