|
|
@@ -1,6 +1,6 @@
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
use mongodb::Collection;
|
|
|
-use bson::oid::ObjectId;
|
|
|
+use bson::{doc, oid::ObjectId};
|
|
|
|
|
|
use crate::app_error::AppError;
|
|
|
use crate::dto::transaction::CreateInput;
|
|
|
@@ -26,8 +26,7 @@ impl Transaction {
|
|
|
pub async fn insert(
|
|
|
collection: &Collection<Transaction>,
|
|
|
input: CreateInput,
|
|
|
- account: ObjectId
|
|
|
- ) -> Result<(), AppError> {
|
|
|
+ ) -> Result<ObjectId, AppError> {
|
|
|
let transaction = Transaction {
|
|
|
id: ObjectId::new(),
|
|
|
account: ObjectId::parse_str(input.account)?,
|
|
|
@@ -35,7 +34,24 @@ impl Transaction {
|
|
|
date: input.date
|
|
|
};
|
|
|
|
|
|
- collection.insert_one(transaction, None).await?;
|
|
|
- Ok(())
|
|
|
+ let insert_one_result = collection.insert_one(transaction, None).await?;
|
|
|
+ Ok(insert_one_result.inserted_id.as_object_id().unwrap())
|
|
|
+ }
|
|
|
+
|
|
|
+ pub async fn find_one(collection: &Collection<Transaction>, transaction_id: ObjectId) -> Result<Transaction, AppError> {
|
|
|
+ match collection.find_one(doc!{"_id": transaction_id}, None).await {
|
|
|
+ Ok(Some(a)) => Ok(a),
|
|
|
+ Ok(None) => Err(AppError::invalid_input("No transaction with that ID")),
|
|
|
+ Err(e) => Err(AppError::Database(e.into()))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn response(self) -> ResponseTransaction {
|
|
|
+ ResponseTransaction {
|
|
|
+ id: self.id.to_hex(),
|
|
|
+ account: self.account.to_hex(),
|
|
|
+ data: self.data.clone(),
|
|
|
+ date: self.date.clone()
|
|
|
+ }
|
|
|
}
|
|
|
}
|