Add primary menu

This commit is contained in:
Evie Viau-Chow-Stuart 2023-02-04 21:21:52 -08:00
parent a93b5b7cf0
commit c478ef03fb
Signed by: evie
GPG key ID: 928652CDFCEC8099
14 changed files with 200 additions and 13 deletions

View file

@ -4,6 +4,8 @@ mod m20230204_001022_create_pages;
mod m20230204_011034_create_posts;
mod m20230204_014834_create_post_blocks;
mod m20230204_014838_create_page_blocks;
mod m20230204_194846_create_menus;
mod m20230204_201059_create_menu_entries;
pub struct Migrator;
#[async_trait::async_trait]
@ -14,6 +16,8 @@ impl MigratorTrait for Migrator {
Box::new(m20230204_011034_create_posts::Migration),
Box::new(m20230204_014834_create_post_blocks::Migration),
Box::new(m20230204_014838_create_page_blocks::Migration),
Box::new(m20230204_194846_create_menus::Migration),
Box::new(m20230204_201059_create_menu_entries::Migration),
]
}
}

View file

@ -11,8 +11,7 @@ impl MigrationTrait for Migration {
Table::create()
.table(Page::Table)
.if_not_exists()
.col(
ColumnDef::new(Page::Id)
.col(ColumnDef::new(Page::Id)
.string()
.not_null()
.primary_key(),

View file

@ -11,8 +11,7 @@ impl MigrationTrait for Migration {
Table::create()
.table(Post::Table)
.if_not_exists()
.col(
ColumnDef::new(Post::Id)
.col(ColumnDef::new(Post::Id)
.string()
.not_null()
.primary_key(),

View file

@ -13,8 +13,7 @@ impl MigrationTrait for Migration {
Table::create()
.table(PostBlock::Table)
.if_not_exists()
.col(
ColumnDef::new(PostBlock::Id)
.col(ColumnDef::new(PostBlock::Id)
.string()
.not_null()
.primary_key(),

View file

@ -13,8 +13,7 @@ impl MigrationTrait for Migration {
Table::create()
.table(PageBlock::Table)
.if_not_exists()
.col(
ColumnDef::new(PageBlock::Id)
.col(ColumnDef::new(PageBlock::Id)
.string()
.not_null()
.primary_key(),

View file

@ -0,0 +1,38 @@
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(Menu::Table)
.if_not_exists()
.col(ColumnDef::new(Menu::Id)
.string()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Menu::Name).string().not_null())
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Menu::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
pub enum Menu {
Table,
Id,
Name,
}

View file

@ -0,0 +1,51 @@
use sea_orm_migration::prelude::*;
use crate::m20230204_194846_create_menus::Menu;
#[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(MenuEntry::Table)
.if_not_exists()
.col(ColumnDef::new(MenuEntry::Id)
.string()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(MenuEntry::Owner).string().not_null())
.col(ColumnDef::new(MenuEntry::Order).integer().not_null())
.col(ColumnDef::new(MenuEntry::Text).string().not_null())
.col(ColumnDef::new(MenuEntry::ReferringSlug).string().not_null())
.foreign_key(ForeignKey::create()
.name("fk-menu_entries-menus")
.from(MenuEntry::Table, MenuEntry::Owner)
.to(Menu::Table, Menu::Id)
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(MenuEntry::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum MenuEntry {
Table,
Id,
Owner,
Order,
Text,
ReferringSlug,
}

25
src/entities/menu.rs Normal file
View file

@ -0,0 +1,25 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "menu")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: String,
pub name: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::menu_entry::Entity")]
MenuEntry,
}
impl Related<super::menu_entry::Entity> for Entity {
fn to() -> RelationDef {
Relation::MenuEntry.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -0,0 +1,34 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "menu_entry")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: String,
pub owner: String,
pub order: i32,
pub text: String,
pub referring_slug: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::menu::Entity",
from = "Column::Owner",
to = "super::menu::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Menu,
}
impl Related<super::menu::Entity> for Entity {
fn to() -> RelationDef {
Relation::Menu.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -2,6 +2,8 @@
pub mod prelude;
pub mod menu;
pub mod menu_entry;
pub mod page;
pub mod page_block;
pub mod post;

View file

@ -1,5 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.7
pub use super::menu::Entity as Menu;
pub use super::menu_entry::Entity as MenuEntry;
pub use super::page::Entity as Page;
pub use super::page_block::Entity as PageBlock;
pub use super::post::Entity as Post;

View file

@ -7,6 +7,7 @@ use crate::{block_types::BlockTypes, AppState};
#[derive(Template)]
#[template(path = "page.html")]
pub(crate) struct PageTemplate {
menu: Vec<menu_entry::Model>,
draft: bool,
title: String,
description: Option<String>,
@ -18,6 +19,7 @@ pub(crate) struct PageTemplate {
#[derive(Template)]
#[template(path = "post.html")]
pub(crate) struct PostTemplate {
menu: Vec<menu_entry::Model>,
draft: bool,
title: String,
summary: Option<String>,
@ -30,6 +32,7 @@ pub(crate) struct PostTemplate {
#[derive(Template)]
#[template(path = "notfound.html")]
pub(crate) struct NotFoundTemplate {
menu: Vec<menu_entry::Model>,
slug: String,
year: String
}
@ -44,6 +47,7 @@ use sea_orm::*;
#[derive(Template)]
#[template(path = "page.html")]
pub(crate) struct HomeSpecialTemplate {
menu: Vec<menu_entry::Model>,
draft: bool,
title: String,
description: Option<String>,
@ -86,9 +90,20 @@ pub(crate) async fn root(
BlockTypes::UNSUPPORTED
}
}
).collect();
let menu: Vec<menu_entry::Model> = Menu::find()
.filter(menu::Column::Name.eq("primary"))
.one(&state.db_conn)
.await
.expect("Failed to get primary menu! HALTING")
.unwrap()
.find_related(MenuEntry)
.order_by_asc(menu_entry::Column::Order)
.all(&state.db_conn)
.await
.expect("Failed to get primary menu items!");
HomeSpecialTemplate {
title: page_meta.clone().title,
description: page_meta.clone().description,
@ -96,5 +111,6 @@ pub(crate) async fn root(
content_blocks,
year: "2023".to_string(),
draft: false,
menu
}
}

View file

@ -14,6 +14,19 @@ pub(crate) async fn resolver(
Path(slug): Path<String>,
state: State<AppState>
) -> impl IntoResponse {
let menu: Vec<menu_entry::Model> = Menu::find()
.filter(menu::Column::Name.eq("primary"))
.one(&state.db_conn)
.await
.expect("Failed to get primary menu! HALTING")
.unwrap()
.find_related(MenuEntry)
.order_by_asc(menu_entry::Column::Order)
.all(&state.db_conn)
.await
.expect("Failed to get primary menu items!");
let page_search: Result<Option<page::Model>, DbErr> = Page::find()
.filter(page::Column::Slug.eq(&slug))
.one(&state.db_conn)
@ -23,7 +36,7 @@ pub(crate) async fn resolver(
Ok(page_result) => {
match page_result {
Some(page_result) => page_result,
None => return (StatusCode::NOT_FOUND, NotFoundTemplate { slug, year: "2023".to_owned() }).into_response(),
None => return (StatusCode::NOT_FOUND, NotFoundTemplate { slug, year: "2023".to_owned(), menu }).into_response(),
}
},
Err(e) => {
@ -63,6 +76,7 @@ pub(crate) async fn resolver(
description: page_meta.clone().description,
show_title: page_meta.clone().show_title,
content_blocks,
year: "2023".to_string()
year: "2023".to_string(),
menu
}).into_response()
}

View file

@ -5,13 +5,18 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Evie / eviee / uwueviee - {% block title %}{{ title }}{% endblock %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="static/css/sakura-vader.css" type="text/css">
<link rel="stylesheet" href="static/css/sakura-pink.css" media="screen" type="text/css">
<link rel="stylesheet" href="static/css/sakura-vader.css" media="screen and (prefers-color-scheme: dark)" type="text/css">
<link rel="stylesheet" href="static/css/meow.css" type="text/css">
</head>
<body>
<h1>🐈 Evie / eviee / uwueviee</h1>
{% block menus %}{% endblock %}
<ul class="primary-menu">
{% for entry in menu %}
<li><a href="/{{ entry.referring_slug }}">{{ entry.text }}</a></li>
{% endfor %}
</ul>
<hr />