@@ -11,28 +11,32 @@ use mithril_common::{
11
11
WhereCondition ,
12
12
} ,
13
13
store:: adapter:: { AdapterError , StoreAdapter } ,
14
+ StdResult ,
14
15
} ;
15
16
16
17
use mithril_common:: StdError ;
17
18
use tokio:: sync:: Mutex ;
18
19
20
+ #[ cfg( test) ]
21
+ use mockall:: automock;
22
+
19
23
/// SignedEntity record is the representation of a stored signed_entity.
20
24
#[ derive( Debug , PartialEq , Clone ) ]
21
25
pub struct SignedEntityRecord {
22
26
/// Signed entity id.
23
- signed_entity_id : String ,
27
+ pub signed_entity_id : String ,
24
28
25
29
/// Signed entity type.
26
- signed_entity_type : SignedEntityType ,
30
+ pub signed_entity_type : SignedEntityType ,
27
31
28
32
/// Certificate id for this signed entity.
29
- certificate_id : String ,
33
+ pub certificate_id : String ,
30
34
31
35
/// Raw signed entity (in JSON format).
32
36
pub entity : String ,
33
37
34
38
/// Date and time when the signed_entity was created
35
- created_at : String ,
39
+ pub created_at : String ,
36
40
}
37
41
38
42
impl From < Snapshot > for SignedEntityRecord {
@@ -242,6 +246,20 @@ impl<'conn> Provider<'conn> for InsertSignedEntityRecordProvider<'conn> {
242
246
}
243
247
}
244
248
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
+
245
263
/// Service to deal with signed_entity (read & write).
246
264
pub struct SignedEntityStoreAdapter {
247
265
connection : Arc < Mutex < Connection > > ,
@@ -254,6 +272,31 @@ impl SignedEntityStoreAdapter {
254
272
}
255
273
}
256
274
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
+
257
300
#[ async_trait]
258
301
impl StoreAdapter for SignedEntityStoreAdapter {
259
302
type Key = String ;
0 commit comments