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")]
DbConnError(#[from] database::error::Error),
#[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::schema::antenna::Antenna;
use super::macros::impl_pack_by_id;
use super::Repository;
#[async_trait]
@ -44,4 +45,8 @@ impl Repository<Antenna> for antenna::Model {
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]
pub trait Repository<T: JsonSchema> {
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
/// provide the JSON Schema validator [`jsonschema::JSONSchema`].
trait Schema<T: JsonSchema> {
pub trait Schema<T: JsonSchema> {
/// Returns the validator of [JSON Schema Draft
/// 7](https://json-schema.org/specification-links.html#draft-7) with the
/// default settings of [`schemars::gen::SchemaSettings`].

View File

@ -29,34 +29,38 @@ mod int_test {
.await
.expect("Unable to pack");
assert_eq!(
packed,
schema::antenna::Antenna {
id: alice_antenna.id,
created_at: alice_antenna.created_at.into(),
name: "Test Antenna".to_string(),
keywords: vec![
vec!["foo".to_string(), "bar".to_string()],
vec!["foobar".to_string()]
]
.into(),
exclude_keywords: vec![
vec!["abc".to_string()],
vec!["def".to_string(), "ghi".to_string()]
]
.into(),
src: schema::antenna::AntennaSrc::All,
user_list_id: None,
user_group_id: None,
users: vec![].into(),
instances: vec![].into(),
case_sensitive: true,
notify: true,
with_replies: false,
with_file: false,
has_unread_note: false,
}
);
let packed_by_id = antenna::Model::pack_by_id(alice_antenna.id.to_owned())
.await
.expect("Unable to pack");
let result = schema::antenna::Antenna {
id: alice_antenna.id,
created_at: alice_antenna.created_at.into(),
name: "Test Antenna".to_string(),
keywords: vec![
vec!["foo".to_string(), "bar".to_string()],
vec!["foobar".to_string()],
]
.into(),
exclude_keywords: vec![
vec!["abc".to_string()],
vec!["def".to_string(), "ghi".to_string()],
]
.into(),
src: schema::antenna::AntennaSrc::All,
user_list_id: None,
user_group_id: None,
users: vec![].into(),
instances: vec![].into(),
case_sensitive: true,
notify: true,
with_replies: false,
with_file: false,
has_unread_note: false,
};
assert_eq!(packed, result);
assert_eq!(packed_by_id, result);
cleanup().await;
}