Skip to content

Commit

Permalink
Adding static page objects (#53)
Browse files Browse the repository at this point in the history
* Added database objects for the Page

* Adding UI and cleaning up multiple instances of EF context

* Added MemoryCache for Pages since they don't change frequently

* First version of EditPage

* Completed the custom page display
  • Loading branch information
csharpfritz authored Nov 7, 2024
1 parent 762df2d commit 11166cc
Show file tree
Hide file tree
Showing 17 changed files with 699 additions and 136 deletions.
16 changes: 16 additions & 0 deletions SharpSite.Abstractions/IPageRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Linq.Expressions;

namespace SharpSite.Abstractions;

public interface IPageRepository
{

Task<Page> AddPage(Page page);
Task UpdatePage(Page page);
Task DeletePage(int id);
Task<Page?> GetPage(string slug);
Task<Page?> GetPage(int id);
Task<IEnumerable<Page>> GetPages();
Task<IEnumerable<Page>> GetPages(Expression<Func<Page, bool>> where);

}
19 changes: 19 additions & 0 deletions SharpSite.Abstractions/Page.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;

namespace SharpSite.Abstractions;

public class Page
{

[Key]
public int Id {get; set;}

[Required, MinLength(4), MaxLength(100)]
public string Title {get; set;} = string.Empty;

[Required]
public required string Slug {get; set;}

public string Content {get; set;} = string.Empty;

}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace SharpSite.Data.Postgres.Migrations
{
/// <inheritdoc />
public partial class AddPagetodatabase : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Pages",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Title = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Slug = table.Column<string>(type: "text", nullable: false),
Content = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Pages", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_Pages_Slug",
table: "Pages",
column: "Slug",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Pages");
}
}
}
29 changes: 29 additions & 0 deletions SharpSite.Data.Postgres/Migrations/PgContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ protected override void BuildModel(ModelBuilder modelBuilder)

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("SharpSite.Data.Postgres.PgPage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Content")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Slug")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.HasIndex("Slug")
.IsUnique();
b.ToTable("Pages");
});

modelBuilder.Entity("SharpSite.Data.Postgres.PgPost", b =>
{
b.Property<string>("Slug")
Expand Down
68 changes: 38 additions & 30 deletions SharpSite.Data.Postgres/PgContext.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace SharpSite.Data.Postgres;

public class PgContext : DbContext
{

public PgContext(DbContextOptions<PgContext> options) : base(options) { }

public DbSet<PgPost> Posts => Set<PgPost>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<PgPost>()
.Property(e => e.Published)
.HasConversion(new DateTimeOffsetConverter());
}

}

public class DateTimeOffsetConverter : ValueConverter<DateTimeOffset, DateTimeOffset>
{
public DateTimeOffsetConverter() : base(
v => v.UtcDateTime,
v => v)
{
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace SharpSite.Data.Postgres;

public class PgContext : DbContext
{

public PgContext(DbContextOptions<PgContext> options) : base(options) { }

public DbSet<PgPage> Pages => Set<PgPage>();

public DbSet<PgPost> Posts => Set<PgPost>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

modelBuilder.Entity<PgPage>()
.HasIndex(p => p.Slug)
.IsUnique();

modelBuilder
.Entity<PgPost>()
.Property(e => e.Published)
.HasConversion(new DateTimeOffsetConverter());

}

}

public class DateTimeOffsetConverter : ValueConverter<DateTimeOffset, DateTimeOffset>
{
public DateTimeOffsetConverter() : base(
v => v.UtcDateTime,
v => v)
{
}
}
44 changes: 44 additions & 0 deletions SharpSite.Data.Postgres/PgPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using SharpSite.Abstractions;
using System.ComponentModel.DataAnnotations;

namespace SharpSite.Data.Postgres;

public class PgPage
{

[Key]
public int Id {get; set;}

[Required, MinLength(4), MaxLength(100)]
public string Title {get; set;} = string.Empty;

[Required]
public required string Slug {get; set;}

public string Content {get; set;} = string.Empty;

public static explicit operator PgPage(Page page)
{

return new PgPage
{
Id = page.Id,
Title = page.Title,
Slug = page.Slug,
Content = page.Content
};

}

public static explicit operator Page(PgPage page)
{
return new Page
{
Id = page.Id,
Title = page.Title,
Slug = page.Slug,
Content = page.Content
};
}

}
Loading

0 comments on commit 11166cc

Please sign in to comment.