From f2a04016010521c9110324b430221950757af74f Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 28 Sep 2021 10:16:20 +0200 Subject: [PATCH] Some route boilerplate for posts --- src/db/mod.rs | 1 + src/db/posts.rs | 4 ++-- src/main.rs | 1 + src/posts.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/posts.rs diff --git a/src/db/mod.rs b/src/db/mod.rs index 35e4995..055bf35 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -6,6 +6,7 @@ pub mod sections; pub mod tokens; pub mod users; +pub use posts::{NewPost, PatchPost, Post}; pub use sections::{NewSection, Section}; pub use tokens::{NewRefreshToken, RefreshToken}; pub use users::{NewUser, User}; diff --git a/src/db/posts.rs b/src/db/posts.rs index 677ed20..f435b9e 100644 --- a/src/db/posts.rs +++ b/src/db/posts.rs @@ -1,6 +1,6 @@ use chrono::NaiveDate; use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::{ @@ -8,7 +8,7 @@ use crate::{ schema::{posts, posts::dsl::*}, }; -#[derive(Queryable)] +#[derive(Queryable, Serialize)] pub struct Post { pub id: Uuid, diff --git a/src/main.rs b/src/main.rs index 3c803f0..f9e6217 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ pub mod auth; pub mod db; pub mod errors; pub mod guards; +pub mod posts; pub(crate) mod schema; pub mod sections; diff --git a/src/posts.rs b/src/posts.rs new file mode 100644 index 0000000..35df7bb --- /dev/null +++ b/src/posts.rs @@ -0,0 +1,26 @@ +use rocket::serde::json::Json; + +use crate::{ + db, + errors::RbResult, + guards::Admin, + RbDbConn, +}; + +#[get("/?&")] +pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult>> +{ + Ok(Json( + conn.run(move |c| db::posts::get(c, offset, limit)).await?, + )) +} + +#[post("/", data = "")] +pub async fn create(_admin: Admin, conn: RbDbConn, new_post: Json) + -> RbResult> +{ + Ok(Json( + conn.run(move |c| db::posts::create(c, &new_post.into_inner())) + .await?, + )) +}