Some route boilerplate for posts

develop
Jef Roosens 2021-09-28 10:16:20 +02:00
parent 769d7a32de
commit f2a0401601
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
4 changed files with 30 additions and 2 deletions

View File

@ -6,6 +6,7 @@ pub mod sections;
pub mod tokens; pub mod tokens;
pub mod users; pub mod users;
pub use posts::{NewPost, PatchPost, Post};
pub use sections::{NewSection, Section}; pub use sections::{NewSection, Section};
pub use tokens::{NewRefreshToken, RefreshToken}; pub use tokens::{NewRefreshToken, RefreshToken};
pub use users::{NewUser, User}; pub use users::{NewUser, User};

View File

@ -1,6 +1,6 @@
use chrono::NaiveDate; use chrono::NaiveDate;
use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable}; use diesel::{insert_into, prelude::*, Insertable, PgConnection, Queryable};
use serde::Deserialize; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
@ -8,7 +8,7 @@ use crate::{
schema::{posts, posts::dsl::*}, schema::{posts, posts::dsl::*},
}; };
#[derive(Queryable)] #[derive(Queryable, Serialize)]
pub struct Post pub struct Post
{ {
pub id: Uuid, pub id: Uuid,

View File

@ -26,6 +26,7 @@ pub mod auth;
pub mod db; pub mod db;
pub mod errors; pub mod errors;
pub mod guards; pub mod guards;
pub mod posts;
pub(crate) mod schema; pub(crate) mod schema;
pub mod sections; pub mod sections;

26
src/posts.rs 100644
View File

@ -0,0 +1,26 @@
use rocket::serde::json::Json;
use crate::{
db,
errors::RbResult,
guards::Admin,
RbDbConn,
};
#[get("/?<offset>&<limit>")]
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Post>>>
{
Ok(Json(
conn.run(move |c| db::posts::get(c, offset, limit)).await?,
))
}
#[post("/", data = "<new_post>")]
pub async fn create(_admin: Admin, conn: RbDbConn, new_post: Json<db::NewPost>)
-> RbResult<Json<db::Post>>
{
Ok(Json(
conn.run(move |c| db::posts::create(c, &new_post.into_inner()))
.await?,
))
}