add pack_by_id

This commit is contained in:
Namekuji 2023-06-02 04:34:49 -04:00
parent c9f31aef9b
commit d0734ef4c9
No known key found for this signature in database
GPG Key ID: B541BD6E646CABC7
5 changed files with 55 additions and 30 deletions

View File

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

View File

@ -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)
}
} }

View File

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

View File

@ -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`].

View File

@ -29,34 +29,38 @@ 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");
id: alice_antenna.id,
created_at: alice_antenna.created_at.into(), let result = schema::antenna::Antenna {
name: "Test Antenna".to_string(), id: alice_antenna.id,
keywords: vec![ created_at: alice_antenna.created_at.into(),
vec!["foo".to_string(), "bar".to_string()], name: "Test Antenna".to_string(),
vec!["foobar".to_string()] keywords: vec![
] vec!["foo".to_string(), "bar".to_string()],
.into(), vec!["foobar".to_string()],
exclude_keywords: vec![ ]
vec!["abc".to_string()], .into(),
vec!["def".to_string(), "ghi".to_string()] exclude_keywords: vec![
] vec!["abc".to_string()],
.into(), vec!["def".to_string(), "ghi".to_string()],
src: schema::antenna::AntennaSrc::All, ]
user_list_id: None, .into(),
user_group_id: None, src: schema::antenna::AntennaSrc::All,
users: vec![].into(), user_list_id: None,
instances: vec![].into(), user_group_id: None,
case_sensitive: true, users: vec![].into(),
notify: true, instances: vec![].into(),
with_replies: false, case_sensitive: true,
with_file: false, notify: true,
has_unread_note: false, with_replies: false,
} with_file: false,
); has_unread_note: false,
};
assert_eq!(packed, result);
assert_eq!(packed_by_id, result);
cleanup().await; cleanup().await;
} }