-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
762df2d
commit 11166cc
Showing
17 changed files
with
699 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
SharpSite.Data.Postgres/Migrations/20241031151541_Add Page to database.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
SharpSite.Data.Postgres/Migrations/20241031151541_Add Page to database.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.