From 8111dee595ea14d8f35b25dca8ec1763c853ad37 Mon Sep 17 00:00:00 2001 From: core Date: Mon, 31 Jul 2023 00:33:00 -0400 Subject: [PATCH] roles:create error parity References: https://todo.e3t.cc/~core/trifid/1 --- trifid-api/src/routes/v1/roles.rs | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/trifid-api/src/routes/v1/roles.rs b/trifid-api/src/routes/v1/roles.rs index b1a5c0a..93d1936 100644 --- a/trifid-api/src/routes/v1/roles.rs +++ b/trifid-api/src/routes/v1/roles.rs @@ -216,6 +216,54 @@ pub async fn create_role_request( } }; + let role: Option = match role::Entity::find() + .filter(role::Column::Name.eq(&req.name)) + .one(&db.conn) + .await + { + Ok(r) => r, + Err(e) => { + error!("database error: {}", e); + return HttpResponse::InternalServerError().json(APIErrorsResponse { + errors: vec![ + APIError { + code: "ERR_DB_ERROR".to_string(), + message: "There was an error performing the database request, please try again later.".to_string(), + path: None, + } + ], + }); + } + }; + + if role.is_some() { + return HttpResponse::BadRequest().json(APIErrorsResponse { + errors: vec![ + APIError { + code: "ERR_DUPLICATE_VALUE".to_string(), + message: "value already exists".to_string(), + path: Some("name".to_string()) + } + ] + }) + } + + for (id, rule) in req.firewall_rules.iter().enumerate() { + if let Some(pr) = &rule.port_range { + if pr.from < pr.to { + return HttpResponse::BadRequest().json(APIErrorsResponse { + errors: vec![ + APIError { + code: "ERR_INVALID_VALUE".to_string(), + message: "from must be less than or equal to to".to_string(), + path: Some(format!("firewallRules[{}].portRange", id)) + } + ] + }); + } + } + } + let new_role_model = role::Model { id: random_id("role"), name: req.name.clone(),