Add featured image to post
This commit is contained in:
parent
1b032f98ff
commit
8971510cbc
16 changed files with 189 additions and 11 deletions
|
@ -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),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
45
migration/src/m20230207_115847_create_images.rs
Normal file
45
migration/src/m20230207_115847_create_images.rs
Normal file
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
32
src/entities/image.rs
Normal file
32
src/entities/image.rs
Normal file
|
@ -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<String>,
|
||||
#[sea_orm(column_type = "Text", nullable)]
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::post::Entity")]
|
||||
Post,
|
||||
}
|
||||
|
||||
impl Related<super::post::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Post.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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::*;
|
||||
|
||||
|
|
|
@ -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::*;
|
||||
|
||||
|
|
|
@ -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<String>,
|
||||
pub published: DateTime,
|
||||
pub updated: Option<DateTime>,
|
||||
pub featured_image: Option<String>,
|
||||
}
|
||||
|
||||
#[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<super::image::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Image.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::post_block::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::PostBlock.def()
|
||||
|
|
|
@ -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::*;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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::*;
|
||||
|
||||
|
|
|
@ -37,7 +37,8 @@ pub(crate) struct PostTemplate {
|
|||
last_updated: Option<String>,
|
||||
content_blocks: Vec<BlockTypes>,
|
||||
year: String,
|
||||
settings: settings::Model
|
||||
settings: settings::Model,
|
||||
featured_image: Option<image::Model>
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
|
|
|
@ -87,6 +87,17 @@ pub(crate) async fn resolver(
|
|||
None => None,
|
||||
};
|
||||
|
||||
let featured_image: Option<image::Model> = 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()
|
||||
}
|
|
@ -33,6 +33,29 @@
|
|||
|
||||
<br />
|
||||
|
||||
{% match featured_image %}
|
||||
{% when Some with (featured_image) %}
|
||||
<div class="featured-image">
|
||||
<img class="featured-image"
|
||||
src="{{ featured_image.url }}"
|
||||
{% match featured_image.alt %}
|
||||
{% when Some with (alt) %}
|
||||
alt="{{ alt }}"
|
||||
{% when None %}
|
||||
{% endmatch %}
|
||||
/>
|
||||
|
||||
{% match featured_image.description %}
|
||||
{% when Some with (image_description) %}
|
||||
<p class="featured-image">{{ image_description }}</p>
|
||||
{% when None %}
|
||||
{% endmatch %}
|
||||
</div>
|
||||
|
||||
<br />
|
||||
{% when None %}
|
||||
{% endmatch %}
|
||||
|
||||
{% for content_block in content_blocks %}
|
||||
{% include "resolve_content.html" %}
|
||||
{% endfor %}
|
||||
|
|
Reference in a new issue