add pack_by_id
This commit is contained in:
parent
c9f31aef9b
commit
d0734ef4c9
@ -5,5 +5,7 @@ pub enum Error {
|
|||||||
#[error("Failed to get database connection")]
|
#[error("Failed to get database connection")]
|
||||||
DbConnError(#[from] database::error::Error),
|
DbConnError(#[from] database::error::Error),
|
||||||
#[error("Database operation error: {0}")]
|
#[error("Database operation error: {0}")]
|
||||||
DatabaseOperationError(#[from] sea_orm::DbErr),
|
DbOperationError(#[from] sea_orm::DbErr),
|
||||||
|
#[error("Requested entity not found")]
|
||||||
|
NotFound,
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ use crate::entity::{antenna, antenna_note, user_group_joining};
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::schema::antenna::Antenna;
|
use crate::schema::antenna::Antenna;
|
||||||
|
|
||||||
|
use super::macros::impl_pack_by_id;
|
||||||
use super::Repository;
|
use super::Repository;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@ -44,4 +45,8 @@ impl Repository<Antenna> for antenna::Model {
|
|||||||
has_unread_note,
|
has_unread_note,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn pack_by_id(id: String) -> Result<Antenna, Error> {
|
||||||
|
impl_pack_by_id!(antenna::Entity, id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,18 @@ use crate::error::Error;
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Repository<T: JsonSchema> {
|
pub trait Repository<T: JsonSchema> {
|
||||||
async fn pack(self) -> Result<T, Error>;
|
async fn pack(self) -> Result<T, Error>;
|
||||||
|
async fn pack_by_id(id: String) -> Result<T, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
mod macros {
|
||||||
|
macro_rules! impl_pack_by_id {
|
||||||
|
($a:ty, $b:ident) => {
|
||||||
|
match <$a>::find_by_id($b).one(database::get_database()?).await? {
|
||||||
|
None => Err(Error::NotFound),
|
||||||
|
Some(m) => m.pack().await,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) use impl_pack_by_id;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ use schemars::{schema_for, JsonSchema};
|
|||||||
|
|
||||||
/// Structs of schema defitions implement this trait in order to
|
/// Structs of schema defitions implement this trait in order to
|
||||||
/// provide the JSON Schema validator [`jsonschema::JSONSchema`].
|
/// provide the JSON Schema validator [`jsonschema::JSONSchema`].
|
||||||
trait Schema<T: JsonSchema> {
|
pub trait Schema<T: JsonSchema> {
|
||||||
/// Returns the validator of [JSON Schema Draft
|
/// Returns the validator of [JSON Schema Draft
|
||||||
/// 7](https://json-schema.org/specification-links.html#draft-7) with the
|
/// 7](https://json-schema.org/specification-links.html#draft-7) with the
|
||||||
/// default settings of [`schemars::gen::SchemaSettings`].
|
/// default settings of [`schemars::gen::SchemaSettings`].
|
||||||
|
@ -29,20 +29,22 @@ mod int_test {
|
|||||||
.await
|
.await
|
||||||
.expect("Unable to pack");
|
.expect("Unable to pack");
|
||||||
|
|
||||||
assert_eq!(
|
let packed_by_id = antenna::Model::pack_by_id(alice_antenna.id.to_owned())
|
||||||
packed,
|
.await
|
||||||
schema::antenna::Antenna {
|
.expect("Unable to pack");
|
||||||
|
|
||||||
|
let result = schema::antenna::Antenna {
|
||||||
id: alice_antenna.id,
|
id: alice_antenna.id,
|
||||||
created_at: alice_antenna.created_at.into(),
|
created_at: alice_antenna.created_at.into(),
|
||||||
name: "Test Antenna".to_string(),
|
name: "Test Antenna".to_string(),
|
||||||
keywords: vec![
|
keywords: vec![
|
||||||
vec!["foo".to_string(), "bar".to_string()],
|
vec!["foo".to_string(), "bar".to_string()],
|
||||||
vec!["foobar".to_string()]
|
vec!["foobar".to_string()],
|
||||||
]
|
]
|
||||||
.into(),
|
.into(),
|
||||||
exclude_keywords: vec![
|
exclude_keywords: vec![
|
||||||
vec!["abc".to_string()],
|
vec!["abc".to_string()],
|
||||||
vec!["def".to_string(), "ghi".to_string()]
|
vec!["def".to_string(), "ghi".to_string()],
|
||||||
]
|
]
|
||||||
.into(),
|
.into(),
|
||||||
src: schema::antenna::AntennaSrc::All,
|
src: schema::antenna::AntennaSrc::All,
|
||||||
@ -55,8 +57,10 @@ mod int_test {
|
|||||||
with_replies: false,
|
with_replies: false,
|
||||||
with_file: false,
|
with_file: false,
|
||||||
has_unread_note: false,
|
has_unread_note: false,
|
||||||
}
|
};
|
||||||
);
|
|
||||||
|
assert_eq!(packed, result);
|
||||||
|
assert_eq!(packed_by_id, result);
|
||||||
|
|
||||||
cleanup().await;
|
cleanup().await;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user