✨ Publication Sites aka Solian Pages
This commit is contained in:
44
DysonNetwork.Shared/Models/PublicationSite.cs
Normal file
44
DysonNetwork.Shared/Models/PublicationSite.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DysonNetwork.Shared.Models;
|
||||
|
||||
public class SnPublicationSite : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
[MaxLength(4096)] public string Slug { get; set; } = null!;
|
||||
[MaxLength(4096)] public string Name { get; set; } = null!;
|
||||
[MaxLength(8192)] public string? Description { get; set; }
|
||||
|
||||
public List<SnPublicationPage> Pages { get; set; } = [];
|
||||
|
||||
public Guid PublisherId { get; set; }
|
||||
public SnPublisher Publisher { get; set; } = null!;
|
||||
public Guid AccountId { get; set; }
|
||||
// Preloaded via the remote services
|
||||
[NotMapped] public SnAccount? Account { get; set; }
|
||||
}
|
||||
|
||||
public abstract class PublicationPagePresets
|
||||
{
|
||||
// Will told the Isolated Island to render according to prebuilt pages by us
|
||||
public const string Landing = "landing"; // Some kind of the mixed version of the profile and the posts
|
||||
public const string Profile = "profile";
|
||||
public const string Posts = "posts";
|
||||
|
||||
// Will told the Isolated Island to render according to the blocks to use custom stuff
|
||||
public const string Custom = "custom";
|
||||
}
|
||||
|
||||
public class SnPublicationPage : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
[MaxLength(8192)] public string Preset { get; set; } = PublicationPagePresets.Landing;
|
||||
[MaxLength(8192)] public string Path { get; set; } = "/";
|
||||
[Column(TypeName = "jsonb")] public Dictionary<string, object?> Config { get; set; } = new();
|
||||
|
||||
public Guid SiteId { get; set; }
|
||||
[JsonIgnore] public SnPublicationSite Site { get; set; } = null!;
|
||||
}
|
||||
@@ -52,6 +52,9 @@ public class AppDatabase(
|
||||
public DbSet<WebFeed> WebFeeds { get; set; } = null!;
|
||||
public DbSet<WebFeedSubscription> WebFeedSubscriptions { get; set; } = null!;
|
||||
|
||||
public DbSet<SnPublicationSite> PublicationSites { get; set; } = null!;
|
||||
public DbSet<SnPublicationPage> PublicationPages { get; set; } = null!;
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseNpgsql(
|
||||
|
||||
2061
DysonNetwork.Sphere/Migrations/20251118153823_AddPublicationSites.Designer.cs
generated
Normal file
2061
DysonNetwork.Sphere/Migrations/20251118153823_AddPublicationSites.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using NodaTime;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DysonNetwork.Sphere.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddPublicationSites : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "publication_sites",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
slug = table.Column<string>(type: "character varying(4096)", maxLength: 4096, nullable: false),
|
||||
name = table.Column<string>(type: "character varying(4096)", maxLength: 4096, nullable: false),
|
||||
description = table.Column<string>(type: "character varying(8192)", maxLength: 8192, nullable: true),
|
||||
publisher_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
account_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
created_at = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
updated_at = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
deleted_at = table.Column<Instant>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_publication_sites", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_publication_sites_publishers_publisher_id",
|
||||
column: x => x.publisher_id,
|
||||
principalTable: "publishers",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "publication_pages",
|
||||
columns: table => new
|
||||
{
|
||||
id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
preset = table.Column<string>(type: "character varying(8192)", maxLength: 8192, nullable: false),
|
||||
path = table.Column<string>(type: "character varying(8192)", maxLength: 8192, nullable: false),
|
||||
config = table.Column<Dictionary<string, object>>(type: "jsonb", nullable: false),
|
||||
site_id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
created_at = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
updated_at = table.Column<Instant>(type: "timestamp with time zone", nullable: false),
|
||||
deleted_at = table.Column<Instant>(type: "timestamp with time zone", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("pk_publication_pages", x => x.id);
|
||||
table.ForeignKey(
|
||||
name: "fk_publication_pages_publication_sites_site_id",
|
||||
column: x => x.site_id,
|
||||
principalTable: "publication_sites",
|
||||
principalColumn: "id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_publication_pages_site_id",
|
||||
table: "publication_pages",
|
||||
column: "site_id");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "ix_publication_sites_publisher_id",
|
||||
table: "publication_sites",
|
||||
column: "publisher_id");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "publication_pages");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "publication_sites");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "9.0.10")
|
||||
.HasAnnotation("ProductVersion", "9.0.11")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
@@ -881,6 +881,108 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
b.ToTable("post_tags", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublicationPage", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<Dictionary<string, object>>("Config")
|
||||
.IsRequired()
|
||||
.HasColumnType("jsonb")
|
||||
.HasColumnName("config");
|
||||
|
||||
b.Property<Instant>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<Instant?>("DeletedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("deleted_at");
|
||||
|
||||
b.Property<string>("Path")
|
||||
.IsRequired()
|
||||
.HasMaxLength(8192)
|
||||
.HasColumnType("character varying(8192)")
|
||||
.HasColumnName("path");
|
||||
|
||||
b.Property<string>("Preset")
|
||||
.IsRequired()
|
||||
.HasMaxLength(8192)
|
||||
.HasColumnType("character varying(8192)")
|
||||
.HasColumnName("preset");
|
||||
|
||||
b.Property<Guid>("SiteId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("site_id");
|
||||
|
||||
b.Property<Instant>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_publication_pages");
|
||||
|
||||
b.HasIndex("SiteId")
|
||||
.HasDatabaseName("ix_publication_pages_site_id");
|
||||
|
||||
b.ToTable("publication_pages", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublicationSite", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("id");
|
||||
|
||||
b.Property<Guid>("AccountId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("account_id");
|
||||
|
||||
b.Property<Instant>("CreatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("created_at");
|
||||
|
||||
b.Property<Instant?>("DeletedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("deleted_at");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasMaxLength(8192)
|
||||
.HasColumnType("character varying(8192)")
|
||||
.HasColumnName("description");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(4096)
|
||||
.HasColumnType("character varying(4096)")
|
||||
.HasColumnName("name");
|
||||
|
||||
b.Property<Guid>("PublisherId")
|
||||
.HasColumnType("uuid")
|
||||
.HasColumnName("publisher_id");
|
||||
|
||||
b.Property<string>("Slug")
|
||||
.IsRequired()
|
||||
.HasMaxLength(4096)
|
||||
.HasColumnType("character varying(4096)")
|
||||
.HasColumnName("slug");
|
||||
|
||||
b.Property<Instant>("UpdatedAt")
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasColumnName("updated_at");
|
||||
|
||||
b.HasKey("Id")
|
||||
.HasName("pk_publication_sites");
|
||||
|
||||
b.HasIndex("PublisherId")
|
||||
.HasDatabaseName("ix_publication_sites_publisher_id");
|
||||
|
||||
b.ToTable("publication_sites", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublisher", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
@@ -1691,6 +1793,30 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
b.Navigation("Post");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublicationPage", b =>
|
||||
{
|
||||
b.HasOne("DysonNetwork.Shared.Models.SnPublicationSite", "Site")
|
||||
.WithMany("Pages")
|
||||
.HasForeignKey("SiteId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_publication_pages_publication_sites_site_id");
|
||||
|
||||
b.Navigation("Site");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublicationSite", b =>
|
||||
{
|
||||
b.HasOne("DysonNetwork.Shared.Models.SnPublisher", "Publisher")
|
||||
.WithMany()
|
||||
.HasForeignKey("PublisherId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired()
|
||||
.HasConstraintName("fk_publication_sites_publishers_publisher_id");
|
||||
|
||||
b.Navigation("Publisher");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublisherFeature", b =>
|
||||
{
|
||||
b.HasOne("DysonNetwork.Shared.Models.SnPublisher", "Publisher")
|
||||
@@ -1895,6 +2021,11 @@ namespace DysonNetwork.Sphere.Migrations
|
||||
b.Navigation("Reactions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublicationSite", b =>
|
||||
{
|
||||
b.Navigation("Pages");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DysonNetwork.Shared.Models.SnPublisher", b =>
|
||||
{
|
||||
b.Navigation("Collections");
|
||||
|
||||
249
DysonNetwork.Sphere/Publication/PublicationSiteController.cs
Normal file
249
DysonNetwork.Sphere/Publication/PublicationSiteController.cs
Normal file
@@ -0,0 +1,249 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using DysonNetwork.Shared.Models;
|
||||
using DysonNetwork.Sphere.Publisher;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PublicationPagePresets = DysonNetwork.Shared.Models.PublicationPagePresets;
|
||||
|
||||
namespace DysonNetwork.Sphere.Publication;
|
||||
|
||||
[ApiController]
|
||||
[Route("/api/sites")]
|
||||
public class PublicationSiteController(
|
||||
PublicationSiteService publicationService,
|
||||
PublisherService publisherService
|
||||
) : ControllerBase
|
||||
{
|
||||
[HttpGet("{slug}")]
|
||||
public async Task<ActionResult<SnPublicationSite>> GetSite(string slug)
|
||||
{
|
||||
var site = await publicationService.GetSiteBySlug(slug);
|
||||
if (site == null)
|
||||
return NotFound();
|
||||
return Ok(site);
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<List<SnPublicationSite>>> ListOwnedSites()
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
// list sites for publishers user is member of
|
||||
var publishers = await publisherService.GetUserPublishers(accountId);
|
||||
var publisherIds = publishers.Select(p => p.Id).ToList();
|
||||
|
||||
var sites = await publicationService.GetSitesByPublisherIds(publisherIds);
|
||||
return Ok(sites);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<SnPublicationSite>> CreateSite([FromBody] PublicationSiteRequest request)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
var site = new SnPublicationSite
|
||||
{
|
||||
Slug = request.Slug,
|
||||
Name = request.Name,
|
||||
Description = request.Description,
|
||||
PublisherId = request.PublisherId,
|
||||
AccountId = accountId
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
site = await publicationService.CreateSite(site, accountId);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
return Ok(site);
|
||||
}
|
||||
|
||||
[HttpPatch("{id:guid}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<SnPublicationSite>> UpdateSite(Guid id, [FromBody] PublicationSiteRequest request)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var site = await publicationService.GetSiteById(id);
|
||||
if (site == null)
|
||||
return NotFound();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
site.Slug = request.Slug;
|
||||
site.Name = request.Name;
|
||||
site.Description = request.Description ?? site.Description;
|
||||
|
||||
try
|
||||
{
|
||||
site = await publicationService.UpdateSite(site, accountId);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
return Ok(site);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> DeleteSite(Guid id)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
try
|
||||
{
|
||||
await publicationService.DeleteSite(id, accountId);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{slug}/page")]
|
||||
public async Task<ActionResult<SnPublicationPage>> RenderPage(string slug, [FromQuery] string path = "/")
|
||||
{
|
||||
var page = await publicationService.RenderPage(slug, path);
|
||||
if (page == null)
|
||||
return NotFound();
|
||||
return Ok(page);
|
||||
}
|
||||
|
||||
[HttpGet("{siteId:guid}/pages")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<List<SnPublicationPage>>> ListPagesForSite(Guid siteId)
|
||||
{
|
||||
var pages = await publicationService.GetPagesForSite(siteId);
|
||||
return Ok(pages);
|
||||
}
|
||||
|
||||
[HttpGet("page/{id:guid}")]
|
||||
public async Task<ActionResult<SnPublicationPage>> GetPage(Guid id)
|
||||
{
|
||||
var page = await publicationService.GetPageById(id);
|
||||
if (page == null)
|
||||
return NotFound();
|
||||
return Ok(page);
|
||||
}
|
||||
|
||||
[HttpPost("{siteId:guid}/pages")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<SnPublicationPage>> CreatePage(Guid siteId, [FromBody] PublicationPageRequest request)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
var page = new SnPublicationPage
|
||||
{
|
||||
Preset = request.Preset ?? PublicationPagePresets.Landing,
|
||||
Path = request.Path ?? "/",
|
||||
Config = request.Config ?? new(),
|
||||
SiteId = siteId
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
page = await publicationService.CreatePage(page, accountId);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
return Ok(page);
|
||||
}
|
||||
|
||||
[HttpPatch("page/{id:guid}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<SnPublicationPage>> UpdatePage(Guid id, [FromBody] PublicationPageRequest request)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var page = await publicationService.GetPageById(id);
|
||||
if (page == null)
|
||||
return NotFound();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
if (request.Preset != null) page.Preset = request.Preset;
|
||||
if (request.Path != null) page.Path = request.Path;
|
||||
if (request.Config != null) page.Config = request.Config;
|
||||
|
||||
try
|
||||
{
|
||||
page = await publicationService.UpdatePage(page, accountId);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
return Ok(page);
|
||||
}
|
||||
|
||||
[HttpDelete("page/{id:guid}")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> DeletePage(Guid id)
|
||||
{
|
||||
if (HttpContext.Items["CurrentUser"] is not Shared.Proto.Account currentUser)
|
||||
return Unauthorized();
|
||||
|
||||
var accountId = Guid.Parse(currentUser.Id);
|
||||
|
||||
try
|
||||
{
|
||||
await publicationService.DeletePage(id, accountId);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Forbid();
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
public class PublicationSiteRequest
|
||||
{
|
||||
[MaxLength(4096)] public string Slug { get; set; } = null!;
|
||||
[MaxLength(4096)] public string Name { get; set; } = null!;
|
||||
[MaxLength(8192)] public string? Description { get; set; }
|
||||
|
||||
public Guid PublisherId { get; set; }
|
||||
}
|
||||
|
||||
public class PublicationPageRequest
|
||||
{
|
||||
[MaxLength(8192)] public string? Preset { get; set; }
|
||||
[MaxLength(8192)] public string? Path { get; set; }
|
||||
public Dictionary<string, object?>? Config { get; set; }
|
||||
}
|
||||
}
|
||||
185
DysonNetwork.Sphere/Publication/PublicationSiteService.cs
Normal file
185
DysonNetwork.Sphere/Publication/PublicationSiteService.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using DysonNetwork.Shared.Models;
|
||||
using DysonNetwork.Sphere.Publisher;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Sphere.Publication;
|
||||
|
||||
public class PublicationSiteService(AppDatabase db, PublisherService publisherService)
|
||||
{
|
||||
public async Task<SnPublicationSite?> GetSiteById(Guid id)
|
||||
{
|
||||
return await db.PublicationSites
|
||||
.Include(s => s.Pages)
|
||||
.ThenInclude(p => p.Site)
|
||||
.Include(s => s.Publisher)
|
||||
.FirstOrDefaultAsync(s => s.Id == id);
|
||||
}
|
||||
|
||||
public async Task<SnPublicationSite?> GetSiteBySlug(string slug)
|
||||
{
|
||||
return await db.PublicationSites
|
||||
.Include(s => s.Pages)
|
||||
.ThenInclude(p => p.Site)
|
||||
.Include(s => s.Publisher)
|
||||
.FirstOrDefaultAsync(s => s.Slug == slug);
|
||||
}
|
||||
|
||||
public async Task<List<SnPublicationSite>> GetSitesByPublisherIds(List<Guid> publisherIds)
|
||||
{
|
||||
return await db.PublicationSites
|
||||
.Include(s => s.Pages)
|
||||
.ThenInclude(p => p.Site)
|
||||
.Include(s => s.Publisher)
|
||||
.Where(s => publisherIds.Contains(s.PublisherId))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<SnPublicationSite> CreateSite(SnPublicationSite site, Guid accountId)
|
||||
{
|
||||
// Check if account already has a site
|
||||
var existingSite = await db.PublicationSites.FirstOrDefaultAsync(s => s.AccountId == accountId);
|
||||
if (existingSite != null)
|
||||
throw new InvalidOperationException("Account already has a site.");
|
||||
|
||||
// Check if account is member of the publisher
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Editor);
|
||||
if (!isMember)
|
||||
throw new UnauthorizedAccessException("Account is not a member of the publisher with sufficient role.");
|
||||
|
||||
db.PublicationSites.Add(site);
|
||||
await db.SaveChangesAsync();
|
||||
return site;
|
||||
}
|
||||
|
||||
public async Task<SnPublicationSite> UpdateSite(SnPublicationSite site, Guid accountId)
|
||||
{
|
||||
// Check permission
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Editor);
|
||||
if (!isMember)
|
||||
throw new UnauthorizedAccessException("Account is not a member of the publisher with sufficient role.");
|
||||
|
||||
db.PublicationSites.Update(site);
|
||||
await db.SaveChangesAsync();
|
||||
return site;
|
||||
}
|
||||
|
||||
public async Task DeleteSite(Guid id, Guid accountId)
|
||||
{
|
||||
var site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Id == id);
|
||||
if (site != null)
|
||||
{
|
||||
// Check permission
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Owner);
|
||||
if (!isMember)
|
||||
throw new UnauthorizedAccessException("Account is not an owner of the publisher.");
|
||||
|
||||
db.PublicationSites.Remove(site);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SnPublicationPage?> GetPageById(Guid id)
|
||||
{
|
||||
return await db.PublicationPages
|
||||
.Include(p => p.Site)
|
||||
.ThenInclude(s => s.Publisher)
|
||||
.FirstOrDefaultAsync(p => p.Id == id);
|
||||
}
|
||||
|
||||
public async Task<List<SnPublicationPage>> GetPagesForSite(Guid siteId)
|
||||
{
|
||||
return await db.PublicationPages
|
||||
.Include(p => p.Site)
|
||||
.Where(p => p.SiteId == siteId)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<SnPublicationPage> CreatePage(SnPublicationPage page, Guid accountId)
|
||||
{
|
||||
var site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Id == page.SiteId);
|
||||
if (site == null)
|
||||
throw new InvalidOperationException("Site not found.");
|
||||
|
||||
// Check permission
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Editor);
|
||||
if (!isMember)
|
||||
throw new UnauthorizedAccessException("Account is not a member of the publisher with sufficient role.");
|
||||
|
||||
db.PublicationPages.Add(page);
|
||||
await db.SaveChangesAsync();
|
||||
return page;
|
||||
}
|
||||
|
||||
public async Task<SnPublicationPage> UpdatePage(SnPublicationPage page, Guid accountId)
|
||||
{
|
||||
// Fetch current site
|
||||
var site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Id == page.SiteId);
|
||||
if (site == null)
|
||||
throw new InvalidOperationException("Site not found.");
|
||||
|
||||
// Check permission
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Editor);
|
||||
if (!isMember)
|
||||
throw new UnauthorizedAccessException("Account is not a member of the publisher with sufficient role.");
|
||||
|
||||
db.PublicationPages.Update(page);
|
||||
await db.SaveChangesAsync();
|
||||
return page;
|
||||
}
|
||||
|
||||
public async Task DeletePage(Guid id, Guid accountId)
|
||||
{
|
||||
var page = await db.PublicationPages.FirstOrDefaultAsync(p => p.Id == id);
|
||||
if (page != null)
|
||||
{
|
||||
var site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Id == page.SiteId);
|
||||
if (site != null)
|
||||
{
|
||||
// Check permission
|
||||
var isMember = await publisherService.IsMemberWithRole(site.PublisherId, accountId, PublisherMemberRole.Editor);
|
||||
if (!isMember)
|
||||
throw new UnauthorizedAccessException("Account is not a member of the publisher with sufficient role.");
|
||||
|
||||
db.PublicationPages.Remove(page);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Special retrieval method
|
||||
|
||||
public async Task<SnPublicationPage?> GetPageBySlugAndPath(string slug, string path)
|
||||
{
|
||||
var site = await GetSiteBySlug(slug);
|
||||
if (site == null) return null;
|
||||
|
||||
foreach (var page in site.Pages)
|
||||
{
|
||||
if (Regex.IsMatch(path, page.Path))
|
||||
{
|
||||
return page;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<SnPublicationPage?> RenderPage(string slug, string path)
|
||||
{
|
||||
var site = await GetSiteBySlug(slug);
|
||||
if (site == null) return null;
|
||||
|
||||
// Find exact match first
|
||||
var exactPage = site.Pages.FirstOrDefault(p => p.Path == path);
|
||||
if (exactPage != null) return exactPage;
|
||||
|
||||
// Then wildcard match
|
||||
var wildcardPage = site.Pages.FirstOrDefault(p => Regex.IsMatch(path, p.Path));
|
||||
if (wildcardPage != null) return wildcardPage;
|
||||
|
||||
// Finally, default page (e.g., "/")
|
||||
var defaultPage = site.Pages.FirstOrDefault(p => p.Path == "/");
|
||||
return defaultPage;
|
||||
}
|
||||
}
|
||||
@@ -417,7 +417,7 @@ public class PublisherService(
|
||||
}
|
||||
|
||||
public async Task<bool> IsMemberWithRole(Guid publisherId, Guid accountId,
|
||||
Shared.Models.PublisherMemberRole requiredRole)
|
||||
PublisherMemberRole requiredRole)
|
||||
{
|
||||
var member = await db.Publishers
|
||||
.Where(p => p.Id == publisherId)
|
||||
|
||||
Reference in New Issue
Block a user