diff --git a/packages/backend/native-utils/crates/model/src/error.rs b/packages/backend/native-utils/crates/model/src/error.rs index 7686312ec4..c90f3f5a32 100644 --- a/packages/backend/native-utils/crates/model/src/error.rs +++ b/packages/backend/native-utils/crates/model/src/error.rs @@ -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, } diff --git a/packages/backend/native-utils/crates/model/src/repository/antenna.rs b/packages/backend/native-utils/crates/model/src/repository/antenna.rs index f9ef012a6e..d3ad6ececb 100644 --- a/packages/backend/native-utils/crates/model/src/repository/antenna.rs +++ b/packages/backend/native-utils/crates/model/src/repository/antenna.rs @@ -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 for antenna::Model { has_unread_note, }) } + + async fn pack_by_id(id: String) -> Result { + impl_pack_by_id!(antenna::Entity, id) + } } diff --git a/packages/backend/native-utils/crates/model/src/repository/mod.rs b/packages/backend/native-utils/crates/model/src/repository/mod.rs index 01e11412db..83573ab160 100644 --- a/packages/backend/native-utils/crates/model/src/repository/mod.rs +++ b/packages/backend/native-utils/crates/model/src/repository/mod.rs @@ -8,4 +8,18 @@ use crate::error::Error; #[async_trait] pub trait Repository { async fn pack(self) -> Result; + async fn pack_by_id(id: String) -> Result; +} + +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; } diff --git a/packages/backend/native-utils/crates/model/src/schema/mod.rs b/packages/backend/native-utils/crates/model/src/schema/mod.rs index cc495aac1b..a647510200 100644 --- a/packages/backend/native-utils/crates/model/src/schema/mod.rs +++ b/packages/backend/native-utils/crates/model/src/schema/mod.rs @@ -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 { +pub trait Schema { /// 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`]. diff --git a/packages/backend/native-utils/crates/model/tests/repository/antenna.rs b/packages/backend/native-utils/crates/model/tests/repository/antenna.rs index e07a16c072..193efcba2b 100644 --- a/packages/backend/native-utils/crates/model/tests/repository/antenna.rs +++ b/packages/backend/native-utils/crates/model/tests/repository/antenna.rs @@ -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; }