use crate::routes::{NewPost, Post}; use sqlx::{Row, SqlitePool}; // Gets all posts from the database pub async fn db_get_posts(pool: &SqlitePool) -> Vec { sqlx::query_as!(Post, "SELECT * FROM posts") .fetch_all(pool) .await .unwrap() } // Inserts a new post to the database pub async fn db_new_post(post: NewPost, pool: &SqlitePool) -> Option { let q2 = sqlx::query!("INSERT INTO posts (content) VALUES (?)", post.content) .execute(pool) .await; let q = sqlx::query_as!( Post, "SELECT * FROM posts WHERE id = (SELECT MAX(id) FROM posts)" ) .fetch_one(pool) .await .ok()?; Some(q) }