forked from Chewing_Bever/rusty-bever
Some route boilerplate for posts
parent
769d7a32de
commit
f2a0401601
|
@ -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};
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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?,
|
||||
))
|
||||
}
|
Loading…
Reference in New Issue