1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use super::utils::{get_key, Element};
use crate::commons::models::validation::ValidationProof;
use crate::utils::{deserialize, serialize};
use crate::DbError;
use crate::{DatabaseCollection, DatabaseManager, Derivable, DigestIdentifier};
use std::sync::Arc;

pub(crate) struct ValidationDb<C: DatabaseCollection> {
    collection: C,
    prefix: String,
}

impl<C: DatabaseCollection> ValidationDb<C> {
    pub fn new<M: DatabaseManager<C>>(manager: &Arc<M>) -> Self {
        Self {
            collection: manager.create_collection("validation"),
            prefix: "validation".to_string(),
        }
    }

    pub fn get_validation_register(
        &self,
        subject_id: &DigestIdentifier,
    ) -> Result<ValidationProof, DbError> {
        let key_elements: Vec<Element> = vec![
            Element::S(self.prefix.clone()),
            Element::S(subject_id.to_str()),
        ];
        let key = get_key(key_elements)?;
        let validation_register = self.collection.get(&key)?;
        Ok(deserialize::<ValidationProof>(&validation_register)
            .map_err(|_| DbError::DeserializeError)?)
    }

    pub fn set_validation_register(
        &self,
        subject_id: &DigestIdentifier,
        validation_proof: &ValidationProof,
    ) -> Result<(), DbError> {
        let key_elements: Vec<Element> = vec![
            Element::S(self.prefix.clone()),
            Element::S(subject_id.to_str()),
        ];
        let key = get_key(key_elements)?;
        let Ok(data) = serialize::<ValidationProof>(validation_proof) else {
            return Err(DbError::SerializeError);
        };
        self.collection.put(&key, data)
    }
}