Skip to content

Commit b563520

Browse files
committed
Add SignedEntityStorer trait
1 parent 6bcbead commit b563520

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

mithril-aggregator/src/database/provider/signed_entity.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,32 @@ use mithril_common::{
1111
WhereCondition,
1212
},
1313
store::adapter::{AdapterError, StoreAdapter},
14+
StdResult,
1415
};
1516

1617
use mithril_common::StdError;
1718
use tokio::sync::Mutex;
1819

20+
#[cfg(test)]
21+
use mockall::automock;
22+
1923
/// SignedEntity record is the representation of a stored signed_entity.
2024
#[derive(Debug, PartialEq, Clone)]
2125
pub struct SignedEntityRecord {
2226
/// Signed entity id.
23-
signed_entity_id: String,
27+
pub signed_entity_id: String,
2428

2529
/// Signed entity type.
26-
signed_entity_type: SignedEntityType,
30+
pub signed_entity_type: SignedEntityType,
2731

2832
/// Certificate id for this signed entity.
29-
certificate_id: String,
33+
pub certificate_id: String,
3034

3135
/// Raw signed entity (in JSON format).
3236
pub entity: String,
3337

3438
/// Date and time when the signed_entity was created
35-
created_at: String,
39+
pub created_at: String,
3640
}
3741

3842
impl From<Snapshot> for SignedEntityRecord {
@@ -242,6 +246,20 @@ impl<'conn> Provider<'conn> for InsertSignedEntityRecordProvider<'conn> {
242246
}
243247
}
244248

249+
/// Signed entity storer trait
250+
#[cfg_attr(test, automock)]
251+
#[async_trait]
252+
pub trait SignedEntityStorer: Sync + Send {
253+
/// Store a signed entity
254+
async fn store_signed_entity(&self, signed_entity: &SignedEntityRecord) -> StdResult<()>;
255+
256+
/// Get signed entity type
257+
async fn get_signed_entity(
258+
&self,
259+
signed_entity_id: String,
260+
) -> StdResult<Option<SignedEntityRecord>>;
261+
}
262+
245263
/// Service to deal with signed_entity (read & write).
246264
pub struct SignedEntityStoreAdapter {
247265
connection: Arc<Mutex<Connection>>,
@@ -254,6 +272,31 @@ impl SignedEntityStoreAdapter {
254272
}
255273
}
256274

275+
#[async_trait]
276+
impl SignedEntityStorer for SignedEntityStoreAdapter {
277+
async fn store_signed_entity(&self, signed_entity: &SignedEntityRecord) -> StdResult<()> {
278+
let connection = &*self.connection.lock().await;
279+
let provider = InsertSignedEntityRecordProvider::new(connection);
280+
let _signed_entity_record = provider.persist(signed_entity.to_owned())?;
281+
282+
Ok(())
283+
}
284+
285+
async fn get_signed_entity(
286+
&self,
287+
signed_entity_id: String,
288+
) -> StdResult<Option<SignedEntityRecord>> {
289+
let connection = &*self.connection.lock().await;
290+
let provider = SignedEntityRecordProvider::new(connection);
291+
let mut cursor = provider
292+
.get_by_signed_entity_id(signed_entity_id)
293+
.map_err(|e| AdapterError::GeneralError(format!("{e}")))?;
294+
let signed_entity = cursor.next();
295+
296+
Ok(signed_entity)
297+
}
298+
}
299+
257300
#[async_trait]
258301
impl StoreAdapter for SignedEntityStoreAdapter {
259302
type Key = String;

0 commit comments

Comments
 (0)