-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66ebc17
commit 69200da
Showing
10 changed files
with
453 additions
and
134 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,14 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace SharpSite.Abstractions; | ||
|
||
public interface IPageRepository | ||
{ | ||
|
||
Task<Page> AddPage(Page page); | ||
Task DeletePage(int id); | ||
Task<Page?> GetPage(string slug); | ||
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 | ||
}; | ||
} | ||
|
||
} |
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,75 @@ | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore; | ||
using SharpSite.Abstractions; | ||
|
||
namespace SharpSite.Data.Postgres; | ||
|
||
public class PgPageRepository(PgContext Context) : IPageRepository | ||
{ | ||
public async Task<Page> AddPage(Page page) | ||
{ | ||
|
||
// Add the page to the database | ||
await Context.Pages.AddAsync((PgPage)page); | ||
await Context.SaveChangesAsync(); | ||
|
||
return page; | ||
|
||
} | ||
|
||
public async Task DeletePage(int id) | ||
{ | ||
// delete the page identified with a given id | ||
var page = Context.Pages.Find(id); | ||
|
||
if (page == null) | ||
{ | ||
throw new Exception("Page not found"); | ||
} | ||
|
||
Context.Pages.Remove(page); | ||
await Context.SaveChangesAsync(); | ||
|
||
} | ||
|
||
public async Task<Page?> GetPage(string slug) | ||
{ | ||
|
||
// get the page with a given slug | ||
var page = await Context.Pages | ||
.AsNoTracking() | ||
.FirstOrDefaultAsync(p => p.Slug == slug); | ||
|
||
// check for a page with the given slug | ||
if (page == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return (Page?)page; | ||
|
||
} | ||
|
||
public async Task<IEnumerable<Page>> GetPages() | ||
{ | ||
// get all pages from the database | ||
return (await Context.Pages.AsNoTracking().ToListAsync()) | ||
.Select(p => (Page)p) | ||
.ToList(); | ||
|
||
} | ||
|
||
public async Task<IEnumerable<Page>> GetPages(Expression<Func<Page, bool>> where) | ||
{ | ||
|
||
// get all pages from the database that satisfy the given condition | ||
return await Context.Pages | ||
.AsNoTracking() | ||
.Where(p => where.Compile().Invoke((Page)p)) | ||
.Select(p => (Page)p) | ||
.ToListAsync(); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.