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 users;
pub use posts::{NewPost, PatchPost, Post};
pub use sections::{NewSection, Section};
pub use tokens::{NewRefreshToken, RefreshToken};
pub use users::{NewUser, User};

View File

@ -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,

View File

@ -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;

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?,
))
}