forked from Chewing_Bever/rusty-bever
Smoothly wrote post find function
parent
d013bd60bd
commit
a107d9e283
|
@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
errors::{RbError, RbResult},
|
errors::{RbError, RbResult, RbOption},
|
||||||
schema::{posts, posts::dsl::*},
|
schema::{posts, posts::dsl::*},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -48,6 +48,15 @@ pub fn get(conn: &PgConnection, offset_: u32, limit_: u32) -> RbResult<Vec<Post>
|
||||||
.map_err(|_| RbError::DbError("Couldn't query posts."))?)
|
.map_err(|_| RbError::DbError("Couldn't query posts."))?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find(conn: &PgConnection, id_: &Uuid) -> RbOption<Post>
|
||||||
|
{
|
||||||
|
match posts.find(id_).first(conn) {
|
||||||
|
Ok(val) => Ok(Some(val)),
|
||||||
|
Err(diesel::NotFound) => Ok(None),
|
||||||
|
_ => Err(RbError::DbError("Couldn't find post.")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
|
pub fn create(conn: &PgConnection, new_post: &NewPost) -> RbResult<Post>
|
||||||
{
|
{
|
||||||
Ok(insert_into(posts)
|
Ok(insert_into(posts)
|
||||||
|
|
|
@ -12,6 +12,7 @@ pub struct Section
|
||||||
{
|
{
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
pub shortname: String,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub is_default: bool,
|
pub is_default: bool,
|
||||||
pub has_titles: bool,
|
pub has_titles: bool,
|
||||||
|
@ -23,6 +24,7 @@ pub struct Section
|
||||||
pub struct NewSection
|
pub struct NewSection
|
||||||
{
|
{
|
||||||
title: String,
|
title: String,
|
||||||
|
pub shortname: String,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
is_default: Option<bool>,
|
is_default: Option<bool>,
|
||||||
has_titles: Option<bool>,
|
has_titles: Option<bool>,
|
||||||
|
@ -34,6 +36,7 @@ pub struct NewSection
|
||||||
pub struct PatchSection
|
pub struct PatchSection
|
||||||
{
|
{
|
||||||
title: Option<String>,
|
title: Option<String>,
|
||||||
|
shortname: Option<String>,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
is_default: Option<bool>,
|
is_default: Option<bool>,
|
||||||
has_titles: Option<bool>,
|
has_titles: Option<bool>,
|
||||||
|
|
|
@ -87,4 +87,8 @@ impl<'r> Responder<'r, 'static> for RbError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Type alias for results that can return an RbError
|
||||||
pub type RbResult<T> = std::result::Result<T, RbError>;
|
pub type RbResult<T> = std::result::Result<T, RbError>;
|
||||||
|
|
||||||
|
/// Type alias for optional results that can fail & return an RbError
|
||||||
|
pub type RbOption<T> = RbResult<Option<T>>;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
use crate::{db, errors::RbResult, guards::Admin, RbDbConn};
|
use crate::{db, errors::RbResult, errors::RbOption, guards::Admin, RbDbConn};
|
||||||
|
|
||||||
#[get("/?<offset>&<limit>")]
|
#[get("/?<offset>&<limit>")]
|
||||||
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Post>>>
|
pub async fn get(conn: RbDbConn, offset: u32, limit: u32) -> RbResult<Json<Vec<db::Post>>>
|
||||||
|
@ -23,6 +23,12 @@ pub async fn create(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/<id>")]
|
||||||
|
pub async fn find(conn: RbDbConn, id: uuid::Uuid) -> RbOption<Json<db::Post>>
|
||||||
|
{
|
||||||
|
Ok(conn.run(move |c| db::posts::find(c, &id)).await?.and_then(|p| Some(Json(p))))
|
||||||
|
}
|
||||||
|
|
||||||
#[patch("/<id>", data = "<patch_post>")]
|
#[patch("/<id>", data = "<patch_post>")]
|
||||||
pub async fn patch(
|
pub async fn patch(
|
||||||
_admin: Admin,
|
_admin: Admin,
|
||||||
|
|
|
@ -21,6 +21,7 @@ table! {
|
||||||
sections (id) {
|
sections (id) {
|
||||||
id -> Uuid,
|
id -> Uuid,
|
||||||
title -> Varchar,
|
title -> Varchar,
|
||||||
|
shortname -> Varchar,
|
||||||
description -> Nullable<Text>,
|
description -> Nullable<Text>,
|
||||||
is_default -> Bool,
|
is_default -> Bool,
|
||||||
has_titles -> Bool,
|
has_titles -> Bool,
|
||||||
|
@ -40,4 +41,9 @@ table! {
|
||||||
joinable!(posts -> sections (section_id));
|
joinable!(posts -> sections (section_id));
|
||||||
joinable!(refresh_tokens -> users (user_id));
|
joinable!(refresh_tokens -> users (user_id));
|
||||||
|
|
||||||
allow_tables_to_appear_in_same_query!(posts, refresh_tokens, sections, users,);
|
allow_tables_to_appear_in_same_query!(
|
||||||
|
posts,
|
||||||
|
refresh_tokens,
|
||||||
|
sections,
|
||||||
|
users,
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in New Issue