From 8971510cbc2e3bedbaf948e20d9023403a51bf22 Mon Sep 17 00:00:00 2001 From: Evie Viau Date: Tue, 7 Feb 2023 05:50:38 -0800 Subject: [PATCH] Add featured image to post --- migration/src/lib.rs | 4 ++ .../src/m20230207_115847_create_images.rs | 45 +++++++++++++++++++ ...0207_115849_add_featured_image_to_posts.rs | 41 +++++++++++++++++ src/entities/image.rs | 32 +++++++++++++ src/entities/menu.rs | 3 +- src/entities/menu_entry.rs | 4 +- src/entities/mod.rs | 3 +- src/entities/page.rs | 2 +- src/entities/page_block.rs | 2 +- src/entities/post.rs | 17 ++++++- src/entities/post_block.rs | 2 +- src/entities/prelude.rs | 3 +- src/entities/settings.rs | 2 +- src/routes/mod.rs | 3 +- src/routes/post_resolver.rs | 14 +++++- templates/post.html | 23 ++++++++++ 16 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 migration/src/m20230207_115847_create_images.rs create mode 100644 migration/src/m20230207_115849_add_featured_image_to_posts.rs create mode 100644 src/entities/image.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 13ac27d..06f996a 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -7,6 +7,8 @@ mod m20230204_014838_create_page_blocks; mod m20230204_194846_create_menus; mod m20230204_201059_create_menu_entries; mod m20230205_022300_create_settings; +mod m20230207_115847_create_images; +mod m20230207_115849_add_featured_image_to_posts; pub struct Migrator; #[async_trait::async_trait] @@ -20,6 +22,8 @@ impl MigratorTrait for Migrator { Box::new(m20230204_194846_create_menus::Migration), Box::new(m20230204_201059_create_menu_entries::Migration), Box::new(m20230205_022300_create_settings::Migration), + Box::new(m20230207_115847_create_images::Migration), + Box::new(m20230207_115849_add_featured_image_to_posts::Migration), ] } } diff --git a/migration/src/m20230207_115847_create_images.rs b/migration/src/m20230207_115847_create_images.rs new file mode 100644 index 0000000..4c2e2a8 --- /dev/null +++ b/migration/src/m20230207_115847_create_images.rs @@ -0,0 +1,45 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Image::Table) + .if_not_exists() + .col( + ColumnDef::new(Image::Id) + .text() + .not_null() + .primary_key(), + ) + .col(ColumnDef::new(Image::Name).text().not_null()) + .col(ColumnDef::new(Image::Url).text().not_null()) + .col(ColumnDef::new(Image::Alt).text()) + .col(ColumnDef::new(Image::Description).text()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(Image::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +pub enum Image { + Table, + Id, + Name, + Url, + Alt, + Description +} diff --git a/migration/src/m20230207_115849_add_featured_image_to_posts.rs b/migration/src/m20230207_115849_add_featured_image_to_posts.rs new file mode 100644 index 0000000..75b98ad --- /dev/null +++ b/migration/src/m20230207_115849_add_featured_image_to_posts.rs @@ -0,0 +1,41 @@ +use sea_orm_migration::prelude::*; + +use crate::{m20230204_011034_create_posts::Post, m20230207_115847_create_images::Image}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Post::Table) + .add_column(ColumnDef::new(Alias::new("featured_image")).string()) + .add_foreign_key(&TableForeignKey::new() + .name("fk-featured_image-image") + .from_tbl(Post::Table) + .from_col(Alias::new("featured_image")) + .to_tbl(Image::Table) + .to_col(Image::Id) + .on_update(ForeignKeyAction::Cascade) + .on_delete(ForeignKeyAction::SetNull) + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Post::Table) + .drop_column(Alias::new("featured_image")) + .drop_foreign_key(Alias::new("fk-featured_image-image")) + .to_owned() + ) + .await + } +} \ No newline at end of file diff --git a/src/entities/image.rs b/src/entities/image.rs new file mode 100644 index 0000000..d380c6c --- /dev/null +++ b/src/entities/image.rs @@ -0,0 +1,32 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "image")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false, column_type = "Text")] + pub id: String, + #[sea_orm(column_type = "Text")] + pub name: String, + #[sea_orm(column_type = "Text")] + pub url: String, + #[sea_orm(column_type = "Text", nullable)] + pub alt: Option, + #[sea_orm(column_type = "Text", nullable)] + pub description: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::post::Entity")] + Post, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Post.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entities/menu.rs b/src/entities/menu.rs index 4fbbc04..9947b4d 100644 --- a/src/entities/menu.rs +++ b/src/entities/menu.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; @@ -7,6 +7,7 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub id: String, + #[sea_orm(column_type = "Text")] pub name: String, } diff --git a/src/entities/menu_entry.rs b/src/entities/menu_entry.rs index 34d25af..fe9f6fc 100644 --- a/src/entities/menu_entry.rs +++ b/src/entities/menu_entry.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; @@ -9,7 +9,9 @@ pub struct Model { pub id: String, pub owner: String, pub order: i32, + #[sea_orm(column_type = "Text")] pub text: String, + #[sea_orm(column_type = "Text")] pub referring_slug: String, } diff --git a/src/entities/mod.rs b/src/entities/mod.rs index 969fdf4..79caf9d 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.rs @@ -1,7 +1,8 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 pub mod prelude; +pub mod image; pub mod menu; pub mod menu_entry; pub mod page; diff --git a/src/entities/page.rs b/src/entities/page.rs index 3aacbdb..1f7acf1 100644 --- a/src/entities/page.rs +++ b/src/entities/page.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; diff --git a/src/entities/page_block.rs b/src/entities/page_block.rs index 0d58a8a..565d492 100644 --- a/src/entities/page_block.rs +++ b/src/entities/page_block.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; diff --git a/src/entities/post.rs b/src/entities/post.rs index 391d40f..981d4cf 100644 --- a/src/entities/post.rs +++ b/src/entities/post.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; @@ -16,14 +16,29 @@ pub struct Model { pub summary: Option, pub published: DateTime, pub updated: Option, + pub featured_image: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { + #[sea_orm( + belongs_to = "super::image::Entity", + from = "Column::FeaturedImage", + to = "super::image::Column::Id", + on_update = "Cascade", + on_delete = "SetNull" + )] + Image, #[sea_orm(has_many = "super::post_block::Entity")] PostBlock, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Image.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::PostBlock.def() diff --git a/src/entities/post_block.rs b/src/entities/post_block.rs index 6f84ffe..ea10489 100644 --- a/src/entities/post_block.rs +++ b/src/entities/post_block.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; diff --git a/src/entities/prelude.rs b/src/entities/prelude.rs index 8a9e6ff..31ef4aa 100644 --- a/src/entities/prelude.rs +++ b/src/entities/prelude.rs @@ -1,5 +1,6 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 +pub use super::image::Entity as Image; pub use super::menu::Entity as Menu; pub use super::menu_entry::Entity as MenuEntry; pub use super::page::Entity as Page; diff --git a/src/entities/settings.rs b/src/entities/settings.rs index 823fdac..0bbb455 100644 --- a/src/entities/settings.rs +++ b/src/entities/settings.rs @@ -1,4 +1,4 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7 +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0 use sea_orm::entity::prelude::*; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 23d6974..adfa9a1 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -37,7 +37,8 @@ pub(crate) struct PostTemplate { last_updated: Option, content_blocks: Vec, year: String, - settings: settings::Model + settings: settings::Model, + featured_image: Option } #[derive(Template)] diff --git a/src/routes/post_resolver.rs b/src/routes/post_resolver.rs index 1cbc13a..46a0ee0 100644 --- a/src/routes/post_resolver.rs +++ b/src/routes/post_resolver.rs @@ -87,6 +87,17 @@ pub(crate) async fn resolver( None => None, }; + let featured_image: Option = match post_meta.clone().featured_image { + Some(featured_image) => { + Image::find() + .filter(image::Column::Id.eq(featured_image)) + .one(&state.db_conn) + .await + .expect("Failed to get primary menu! HALTING") + }, + None => None, + }; + (StatusCode::FOUND, PostTemplate { draft: post_meta.clone().draft, title: post_meta.clone().title, @@ -96,6 +107,7 @@ pub(crate) async fn resolver( content_blocks, year: Utc::now().date_naive().year().to_string(), menu, - settings + settings, + featured_image, }).into_response() } \ No newline at end of file diff --git a/templates/post.html b/templates/post.html index b4a913d..76d32f3 100644 --- a/templates/post.html +++ b/templates/post.html @@ -33,6 +33,29 @@
+ {% match featured_image %} + {% when Some with (featured_image) %} + + +
+ {% when None %} + {% endmatch %} + {% for content_block in content_blocks %} {% include "resolve_content.html" %} {% endfor %}