From f965be88b77e6f8dee4a473e59b81b22ed2a45ac Mon Sep 17 00:00:00 2001 From: koko Date: Mon, 26 Jun 2023 23:02:11 -0400 Subject: [PATCH] Website announcements fixes and additions (#818) * Render line breaks in announcement content * Track announcement publisher * Change paragraph tag to a div for consistency * Fix bottom padding for announcement content * Track publisher user entity rather than just the username * Potentially fix a migration failure * Final code cleanup * Fix linq statement chaining style --------- Co-authored-by: Dagg --- .../Pages/AnnouncePage.cshtml | 22 ++++++--- .../Pages/AnnouncePage.cshtml.cs | 9 ++-- ...624203015_AddPostedByToWebAnnouncements.cs | 49 +++++++++++++++++++ .../Migrations/DatabaseModelSnapshot.cs | 16 +++++- .../Website/WebsiteAnnouncementEntity.cs | 11 +++++ 5 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 ProjectLighthouse/Migrations/20230624203015_AddPostedByToWebAnnouncements.cs diff --git a/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml b/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml index 06710ad2..56e590f1 100644 --- a/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml @@ -37,11 +37,21 @@ @foreach (WebsiteAnnouncementEntity announcement in Model.Announcements) {
-

@announcement.Title

-

- @announcement.Content -

- +
+

@announcement.Title

+
+ @announcement.Content +
+ @if (announcement.Publisher != null) + { + + } +
@if (Model.User != null && Model.User.IsAdmin) {
@@ -57,7 +67,7 @@
}
- } + } } else { diff --git a/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml.cs b/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml.cs index bf4175d4..41b554c6 100644 --- a/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml.cs +++ b/ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml.cs @@ -22,6 +22,7 @@ public class AnnouncePage : BaseLayout public async Task OnGet() { this.Announcements = await this.Database.WebsiteAnnouncements + .Include(a => a.Publisher) .OrderByDescending(a => a.AnnouncementId) .ToListAsync(); @@ -42,8 +43,9 @@ public class AnnouncePage : BaseLayout WebsiteAnnouncementEntity announcement = new() { - Title = title, - Content = content, + Title = title.Trim(), + Content = content.Trim(), + PublisherId = user.UserId, }; await this.Database.WebsiteAnnouncements.AddAsync(announcement); @@ -55,8 +57,7 @@ public class AnnouncePage : BaseLayout ? content[..250] + $"... [read more]({ServerConfiguration.Instance.ExternalUrl}/announce)" : content; - await WebhookHelper.SendWebhook($":mega: {title}", - truncatedAnnouncement); + await WebhookHelper.SendWebhook($":mega: {title}", truncatedAnnouncement); } return this.RedirectToPage(); diff --git a/ProjectLighthouse/Migrations/20230624203015_AddPostedByToWebAnnouncements.cs b/ProjectLighthouse/Migrations/20230624203015_AddPostedByToWebAnnouncements.cs new file mode 100644 index 00000000..830ba80e --- /dev/null +++ b/ProjectLighthouse/Migrations/20230624203015_AddPostedByToWebAnnouncements.cs @@ -0,0 +1,49 @@ +using LBPUnion.ProjectLighthouse.Database; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ProjectLighthouse.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20230624203015_AddPostedByToWebAnnouncements")] + public partial class AddPostedByToWebAnnouncements : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "PublisherId", + table: "WebsiteAnnouncements", + type: "int", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_WebsiteAnnouncements_PublisherId", + table: "WebsiteAnnouncements", + column: "PublisherId"); + + migrationBuilder.AddForeignKey( + name: "FK_WebsiteAnnouncements_Users_PublisherId", + table: "WebsiteAnnouncements", + column: "PublisherId", + principalTable: "Users", + principalColumn: "UserId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_WebsiteAnnouncements_Users_PublisherId", + table: "WebsiteAnnouncements"); + + migrationBuilder.DropIndex( + name: "IX_WebsiteAnnouncements_PublisherId", + table: "WebsiteAnnouncements"); + + migrationBuilder.DropColumn( + name: "PublisherId", + table: "WebsiteAnnouncements"); + } + } +} diff --git a/ProjectLighthouse/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs b/ProjectLighthouse/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs index 847b390b..2e5991fa 100644 --- a/ProjectLighthouse/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs +++ b/ProjectLighthouse/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs @@ -1033,10 +1033,15 @@ namespace ProjectLighthouse.Migrations b.Property("Content") .HasColumnType("longtext"); + b.Property("PublisherId") + .HasColumnType("int"); + b.Property("Title") .HasColumnType("longtext"); - b.HasKey("Identifier"); + b.HasKey("AnnouncementId"); + + b.HasIndex("PublisherId"); b.ToTable("WebsiteAnnouncements"); }); @@ -1394,6 +1399,15 @@ namespace ProjectLighthouse.Migrations b.Navigation("User"); }); + modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Website.WebsiteAnnouncementEntity", b => + { + b.HasOne("LBPUnion.ProjectLighthouse.Types.Entities.Profile.UserEntity", "Publisher") + .WithMany() + .HasForeignKey("PublisherId"); + + b.Navigation("Publisher"); + }); + modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Profile.PhotoEntity", b => { b.Navigation("PhotoSubjects"); diff --git a/ProjectLighthouse/Types/Entities/Website/WebsiteAnnouncementEntity.cs b/ProjectLighthouse/Types/Entities/Website/WebsiteAnnouncementEntity.cs index 26c42488..8749936d 100644 --- a/ProjectLighthouse/Types/Entities/Website/WebsiteAnnouncementEntity.cs +++ b/ProjectLighthouse/Types/Entities/Website/WebsiteAnnouncementEntity.cs @@ -1,4 +1,6 @@ using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using LBPUnion.ProjectLighthouse.Types.Entities.Profile; namespace LBPUnion.ProjectLighthouse.Types.Entities.Website; @@ -10,4 +12,13 @@ public class WebsiteAnnouncementEntity public string Title { get; set; } public string Content { get; set; } + + #nullable enable + + public int? PublisherId { get; set; } + + [ForeignKey(nameof(PublisherId))] + public UserEntity? Publisher { get; set; } + + #nullable disable } \ No newline at end of file