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
51
52
53
54
55
use std::fmt::Debug;

use crate::document::document::BaseDocument;
use crate::document::pageable::{PageableRequest, PageableResponse};
use async_trait::async_trait;
use bson::oid::ObjectId;
use bson::Document;
use mongodb::error::Error;
use mongodb::options::{FindOneOptions, FindOptions};
use mongodb::Database;
use opentelemetry::trace::{Span, Tracer};
use opentelemetry::{global, Context, KeyValue};

#[async_trait]
pub trait Repository<T: BaseDocument + Debug + serde::ser::Serialize  + for<'de> serde::Deserialize<'de>> {
    async fn save(&self, entity: T, ctx: Option<&Context>) -> Result<ObjectId, Error>;
    async fn save_many(
        &self,
        entities: Vec<T>,
        ctx: Option<&Context>,
    ) -> Result<Vec<ObjectId>, Error>;
    async fn get_all_pageable(
        &self,
        request: PageableRequest,
        ctx: Option<&Context>,
    ) -> Result<PageableResponse<T>, Error>;

    async fn count_documents_in_collection(&self, ctx: Option<&Context>) -> Result<u64, Error>;
    async fn find_by_id(&self, id: &str, ctx: Option<&Context>) -> Result<T, Error>;
    async fn find_by_raw_id(&self, id: ObjectId, ctx: Option<&Context>) -> Result<T, Error>;
    async fn find_by_ids(&self, ids: Vec<String>, ctx: Option<&Context>) -> Result<Vec<T>, Error>;

    async fn find_by_raw_ids(
        &self,
        pids: Vec<ObjectId>,
        ctx: Option<&Context>,
    ) -> Result<Vec<T>, Error>;

    async fn find_by_filter_and_options(
        &self,
        filter: Document,
        options: Option<FindOneOptions>,
        ctx: Option<&Context>,
    ) -> Result<T, Error>;

    async fn find_all_by_filter_and_options(
        &self,
        filter: Document,
        options: Option<FindOptions>,
        ctx: Option<&Context>,
    ) -> Result<Vec<T>, Error>;

    async fn update(&self, entity: &T, ctx: Option<&Context>) -> Result<ObjectId, Error>;
    async fn delete(&self, pid: String, ctx: Option<&Context>) -> Result<(), Error>;
}