• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 4020902227

pending completion
4020902227

Pull #743

github

GitHub
Merge 57279c6b6 into a12da35a5
Pull Request #743: Chore clippy fix

165 of 165 new or added lines in 60 files covered. (100.0%)

23638 of 35485 relevant lines covered (66.61%)

38417.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/dozer-admin/src/db/api_config.rs
1
use super::{
2
    application::Application,
3
    constants,
4
    persistable::Persistable,
5
    schema::{
6
        self,
7
        configs::{self, app_id},
8
    },
9
};
10
use crate::db::schema::apps::dsl::apps;
11
use crate::diesel::ExpressionMethods;
12
use crate::server::dozer_admin_grpc::Pagination;
13
use diesel::{insert_into, AsChangeset, Insertable, QueryDsl, Queryable, RunQueryDsl};
14
use diesel::{query_dsl::methods::FilterDsl, *};
15
use dozer_types::models::{
16
    api_config::{
17
        default_api_config, ApiConfig, ApiGrpc, ApiInternal, ApiPipelineInternal, ApiRest,
18
    },
19
    api_security::ApiSecurity,
20
};
21
use schema::configs::dsl::*;
22
use serde::{Deserialize, Serialize};
23
use std::error::Error;
24

25
#[derive(Identifiable, Queryable, PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Default)]
×
26
#[diesel(table_name = configs)]
27
pub struct DBApiConfig {
28
    pub(crate) id: String,
29
    pub(crate) app_id: String,
30
    pub(crate) api_security: Option<String>,
31
    pub(crate) rest: Option<String>,
32
    pub(crate) grpc: Option<String>,
33
    pub(crate) auth: Option<bool>,
34
    pub(crate) api_internal: Option<String>,
35
    pub(crate) pipeline_internal: Option<String>,
36
    pub(crate) created_at: String,
37
    pub(crate) updated_at: String,
38
}
39
#[derive(Insertable, AsChangeset, PartialEq, Debug, Serialize, Deserialize)]
×
40
#[diesel(table_name = configs)]
41
struct NewApiConfig {
42
    pub(crate) id: String,
43
    pub(crate) app_id: String,
44
    pub(crate) api_security: Option<String>,
45
    pub(crate) rest: Option<String>,
46
    pub(crate) grpc: Option<String>,
47
    pub(crate) api_internal: Option<String>,
48
    pub(crate) pipeline_internal: Option<String>,
49
    pub(crate) auth: Option<bool>,
50
}
51
impl TryFrom<DBApiConfig> for ApiConfig {
52
    type Error = Box<dyn Error>;
53
    fn try_from(item: DBApiConfig) -> Result<Self, Self::Error> {
×
54
        let default_api_config = default_api_config();
×
55
        let rest_value = item.rest.map_or_else(
×
56
            || default_api_config.rest,
×
57
            |str| serde_json::from_str::<ApiRest>(&str).ok(),
×
58
        );
×
59
        let grpc_value = item.grpc.map_or_else(
×
60
            || default_api_config.grpc,
×
61
            |str| serde_json::from_str::<ApiGrpc>(&str).ok(),
×
62
        );
×
63
        let internal_api = item.api_internal.map_or_else(
×
64
            || default_api_config.api_internal,
×
65
            |str| serde_json::from_str::<ApiInternal>(&str).ok(),
×
66
        );
×
67
        let internal_pipeline = item.pipeline_internal.map_or_else(
×
68
            || default_api_config.pipeline_internal,
×
69
            |str| serde_json::from_str::<ApiPipelineInternal>(&str).ok(),
×
70
        );
×
71
        let security_jwt = item.api_security.map_or_else(
×
72
            || default_api_config.api_security,
×
73
            |str| serde_json::from_str::<ApiSecurity>(&str).ok(),
×
74
        );
×
75
        Ok(ApiConfig {
×
76
            rest: rest_value,
×
77
            grpc: grpc_value,
×
78
            api_security: security_jwt,
×
79
            api_internal: internal_api,
×
80
            pipeline_internal: internal_pipeline,
×
81
            auth: item.auth.unwrap_or_default(),
×
82
            app_id: Some(item.app_id),
×
83
            id: Some(item.id),
×
84
        })
×
85
    }
×
86
}
87

88
impl TryFrom<ApiConfig> for NewApiConfig {
89
    type Error = Box<dyn Error>;
90
    fn try_from(item: ApiConfig) -> Result<Self, Self::Error> {
×
91
        let rest_value = item.rest.map_or_else(
×
92
            || None,
×
93
            |rest_config| serde_json::to_string(&rest_config).ok(),
×
94
        );
×
95
        let grpc_value = item.grpc.map_or_else(
×
96
            || None,
×
97
            |grpc_config| serde_json::to_string(&grpc_config).ok(),
×
98
        );
×
99
        let internal_api = item.api_internal.map_or_else(
×
100
            || None,
×
101
            |api_internal_config| serde_json::to_string(&api_internal_config).ok(),
×
102
        );
×
103
        let internal_pipeline = item.pipeline_internal.map_or_else(
×
104
            || None,
×
105
            |internal_pipeline_config| serde_json::to_string(&internal_pipeline_config).ok(),
×
106
        );
×
107
        let api_security_config = item.api_security.map_or_else(
×
108
            || None,
×
109
            |iapi_security_config| serde_json::to_string(&iapi_security_config).ok(),
×
110
        );
×
111

×
112
        let generated_id = uuid::Uuid::new_v4().to_string();
×
113
        let id_str = item.id.unwrap_or(generated_id);
×
114
        Ok(NewApiConfig {
×
115
            id: id_str,
×
116
            app_id: item.app_id.unwrap(),
×
117
            rest: rest_value,
×
118
            grpc: grpc_value,
×
119
            api_internal: internal_api,
×
120
            pipeline_internal: internal_pipeline,
×
121
            auth: Some(item.auth),
×
122
            api_security: api_security_config,
×
123
        })
×
124
    }
×
125
}
126
impl Persistable<'_, ApiConfig> for ApiConfig {
127
    fn save(&mut self, pool: super::pool::DbPool) -> Result<&mut ApiConfig, Box<dyn Error>> {
×
128
        self.upsert(pool)
×
129
    }
×
130

131
    fn by_id(
×
132
        pool: super::pool::DbPool,
×
133
        _input_id: String,
×
134
        application_id: String,
×
135
    ) -> Result<ApiConfig, Box<dyn Error>> {
×
136
        let mut db = pool.get()?;
×
137
        let result: DBApiConfig =
×
138
            FilterDsl::filter(configs, app_id.eq(application_id)).first(&mut db)?;
×
139
        let result = ApiConfig::try_from(result)?;
×
140
        Ok(result)
×
141
    }
×
142

143
    fn list(
×
144
        pool: super::pool::DbPool,
×
145
        application_id: String,
×
146
        limit: Option<u32>,
×
147
        offset: Option<u32>,
×
148
    ) -> Result<(Vec<ApiConfig>, Pagination), Box<dyn Error>> {
×
149
        let mut db = pool.get()?;
×
150
        let offset = offset.unwrap_or(constants::OFFSET);
×
151
        let limit = limit.unwrap_or(constants::LIMIT);
×
152
        let filter_dsl = FilterDsl::filter(configs, app_id.eq(application_id));
×
153
        let results: Vec<DBApiConfig> = filter_dsl
×
154
            .to_owned()
×
155
            .offset(offset.into())
×
156
            .order_by(configs::id.asc())
×
157
            .limit(limit.into())
×
158
            .load(&mut db)?;
×
159
        let total: i64 = filter_dsl.count().get_result(&mut db)?;
×
160
        let config_lst: Vec<ApiConfig> = results
×
161
            .iter()
×
162
            .map(|result| ApiConfig::try_from(result.clone()).unwrap())
×
163
            .collect();
×
164
        Ok((
×
165
            config_lst,
×
166
            Pagination {
×
167
                limit,
×
168
                total: total.try_into().unwrap_or(0),
×
169
                offset,
×
170
            },
×
171
        ))
×
172
    }
×
173

174
    fn upsert(&mut self, pool: super::pool::DbPool) -> Result<&mut ApiConfig, Box<dyn Error>> {
×
175
        let new_config = NewApiConfig::try_from(self.to_owned())?;
×
176
        let mut db = pool.get()?;
×
177
        db.transaction::<(), _, _>(|conn| -> Result<(), Box<dyn Error>> {
×
178
            let _ = apps
×
179
                .find(new_config.app_id.to_owned())
×
180
                .first::<Application>(conn)
×
181
                .map_err(|err| format!("App_id: {err:}"))?;
×
182
            let _inserted = insert_into(configs)
×
183
                .values(&new_config)
×
184
                .on_conflict(configs::id)
×
185
                .do_update()
×
186
                .set(&new_config)
×
187
                .execute(conn);
×
188
            self.id = Some(new_config.id);
×
189
            Ok(())
×
190
        })?;
×
191

192
        Ok(self)
×
193
    }
×
194

195
    fn delete(
×
196
        _pool: super::pool::DbPool,
×
197
        _input_id: String,
×
198
        _application_id: String,
×
199
    ) -> Result<bool, Box<dyn Error>> {
×
200
        todo!()
×
201
    }
×
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc