list view gives us a table

This commit is contained in:
Christoph J. Scherr 2024-06-27 12:12:34 +02:00
parent 732f73b637
commit 1bca475b3a
2 changed files with 33 additions and 13 deletions

View File

@ -1,6 +1,8 @@
pub mod models; pub mod models;
pub mod schema; pub mod schema;
use self::schema::posts::dsl::*;
use std::io::Write; use std::io::Write;
use std::{env, io}; use std::{env, io};
@ -11,9 +13,6 @@ use dotenvy::dotenv;
use libpt::log::{error, info, warn}; use libpt::log::{error, info, warn};
use self::models::*;
use self::schema::posts::dsl::*;
pub fn establish_connection() -> anyhow::Result<SqliteConnection> { pub fn establish_connection() -> anyhow::Result<SqliteConnection> {
dotenv()?; dotenv()?;
@ -22,21 +21,41 @@ pub fn establish_connection() -> anyhow::Result<SqliteConnection> {
.inspect_err(|e| error!("Error connecting to {}:\n{e:#?}", database_url))?) .inspect_err(|e| error!("Error connecting to {}:\n{e:#?}", database_url))?)
} }
pub fn load_posts(conn: &mut SqliteConnection) -> anyhow::Result<Vec<models::Post>> { pub fn load_all_posts(conn: &mut SqliteConnection) -> anyhow::Result<Vec<models::Post>> {
Ok(posts.select(models::Post::as_select()).load(conn)?)
}
pub fn load_relevant_posts(conn: &mut SqliteConnection) -> anyhow::Result<Vec<models::Post>> {
Ok(posts Ok(posts
.filter(published.eq(true)) .filter(schema::posts::published.eq(true))
.limit(5) .limit(5)
.select(Post::as_select()) .select(models::Post::as_select())
.load(conn)?) .load(conn)?)
} }
pub fn print_posts(posts_to_print: &Vec<Post>) { // NOTE: formatting breaks when you use japanese fullwidth (or probably other longer chars too)
// characters. Works well for the regular alphabet
pub fn print_posts(posts_to_print: &Vec<models::Post>) {
if !posts_to_print.is_empty() { if !posts_to_print.is_empty() {
info!("Displaying {} posts", posts_to_print.len()); info!("{} posts are in the database", posts_to_print.len());
println!(
"{: <12}| {: <30} | {: <40}[...] | {: <12} | {: <5}",
"id", "title", "body (truncated)", "body len", "is published?"
);
println!("{:=^140}", "");
for post in posts_to_print { for post in posts_to_print {
println!("{}", post.title); let mut short_title = post.body.clone();
println!("-----------\n"); short_title.truncate(30);
println!("{}", post.body); let mut short_body = post.body.clone();
short_body.truncate(40);
println!(
"{: <12}| {: <30} | {: <40}[...] | {: <12} | {: <5}",
post.id,
short_title,
short_body,
post.body.len(),
post.published
);
} }
} else { } else {
warn!("Tried to display posts, but there are no posts stored in the database"); warn!("Tried to display posts, but there are no posts stored in the database");

View File

@ -6,7 +6,7 @@ use diesel_demo as lib;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let _logger = log::Logger::builder() let _logger = log::Logger::builder()
.max_level(log::Level::TRACE) .max_level(log::Level::DEBUG)
.show_time(false) .show_time(false)
.build(); .build();
debug!("logger initialized"); debug!("logger initialized");
@ -38,7 +38,8 @@ fn repl(conn: &mut SqliteConnection) -> anyhow::Result<()> {
} else if buf.starts_with("EXIT") { } else if buf.starts_with("EXIT") {
break; break;
} else if buf.starts_with("LIST") { } else if buf.starts_with("LIST") {
let posts = lib::load_posts(conn)?; let posts = lib::load_all_posts(conn)?;
trace!("loaded posts for display: {posts:#?}");
lib::print_posts(&posts); lib::print_posts(&posts);
} else if buf.starts_with("NEW") { } else if buf.starts_with("NEW") {
let post = PostDraft::interactive_create()?; let post = PostDraft::interactive_create()?;