Skip to content

Commit

Permalink
Initial update_model implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sabotack committed Nov 28, 2023
1 parent d5fc74a commit 25940ef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
60 changes: 45 additions & 15 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::database::{
model_context::ModelContextTrait, query_context::QueryContextTrait,
session_context::SessionContextTrait, user_context::UserContextTrait,
};
use crate::entities::access::Model;

Check warning on line 15 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused import: `crate::entities::access::Model`

warning: unused import: `crate::entities::access::Model` --> src/api/ecdar_api.rs:15:5 | 15 | use crate::entities::access::Model; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

Check warning on line 15 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused import: `crate::entities::access::Model`

warning: unused import: `crate::entities::access::Model` --> src/api/ecdar_api.rs:15:5 | 15 | use crate::entities::access::Model; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

use super::server::server::UpdateModelRequest;
use super::server::server::{
Expand Down Expand Up @@ -174,21 +175,50 @@ impl EcdarApi for ConcreteEcdarApi {
Err(error) => return Err(Status::internal(error.to_string())),
};

Check warning on line 176 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/ecdar_api.rs

// // Check if the user has access to the model
// let access = match self.access_context.get_by_id(message.id, uid).await {
// Ok(Some(access)) => access,
// Ok(None) => return Err(Status::permission_denied("No access to model")),
// Err(error) => return Err(Status::internal(error.to_string())),
// };

// let model = model::Model {
// id: message.id,
// name: message.name,
// components_info: serde_json::to_value(message.components_info).unwrap(),
// owner_id: Default::default(),
// };

match self.model_context.update(model).await {
// Check if the user has access to the model
let access = match self.access_context.get_access_by_uid_and_model_id(uid, model.id).await {

Check warning on line 179 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused variable: `access`

warning: unused variable: `access` --> src/api/ecdar_api.rs:179:13 | 179 | let access = match self.access_context.get_access_by_uid_and_model_id(uid, model.id).await { | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_access` | = note: `#[warn(unused_variables)]` on by default

Check warning on line 179 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

unused variable: `access`

warning: unused variable: `access` --> src/api/ecdar_api.rs:179:13 | 179 | let access = match self.access_context.get_access_by_uid_and_model_id(uid, model.id).await { | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_access` | = note: `#[warn(unused_variables)]` on by default
Ok(access) => {
let mut is_editor = false;
let access = match access {
Some(access) => {
if access.role == "Editor" {
is_editor = true;
} else {
is_editor = false;

Check warning on line 187 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/ecdar_api.rs
}

Check failure on line 188 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

this if-then-else expression assigns a bool literal

error: this if-then-else expression assigns a bool literal --> src/api/ecdar_api.rs:184:25 | 184 | / if access.role == "Editor" { 185 | | is_editor = true; 186 | | } else { 187 | | is_editor = false; 188 | | } | |_________________________^ help: you can reduce it to: `is_editor = access.role == "Editor";` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool_assign = note: `-D clippy::needless-bool-assign` implied by `-D clippy::complexity` = help: to override `-D clippy::complexity` add `#[allow(clippy::needless_bool_assign)]`

Check failure on line 188 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

this if-then-else expression assigns a bool literal

error: this if-then-else expression assigns a bool literal --> src/api/ecdar_api.rs:184:25 | 184 | / if access.role == "Editor" { 185 | | is_editor = true; 186 | | } else { 187 | | is_editor = false; 188 | | } | |_________________________^ help: you can reduce it to: `is_editor = access.role == "Editor";` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_bool_assign = note: `-D clippy::needless-bool-assign` implied by `-D clippy::complexity` = help: to override `-D clippy::complexity` add `#[allow(clippy::needless_bool_assign)]`
Some(access)
},
None => {
None
},
};

if !is_editor || access.is_none() {
return Err(Status::permission_denied("You do not have permission to update this model"))

Check warning on line 197 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/ecdar_api.rs
}

access.unwrap()
},

Check warning on line 201 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/ecdar_api.rs
Err(error) => return Err(Status::internal(error.to_string())),
};

let new_model = model::Model {

Check warning on line 205 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/Ecdar-API/Ecdar-API/src/api/ecdar_api.rs
id: Default::default(),
name: match message.clone().name {
Some(name) => name,
None => model.name,
},
components_info: match message.clone().components_info {
Some(components_info) => serde_json::to_value(components_info).unwrap(),
None => model.components_info,
},
owner_id: match message.clone().owner_id {
Some(owner_id) => owner_id,
None => model.owner_id,
},
};

match self.model_context.update(new_model).await {
Ok(_) => Ok(Response::new(())),
Err(error) => Err(Status::new(Code::Internal, error.to_string())),
}
Expand Down
4 changes: 3 additions & 1 deletion src/tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ mock! {
async fn delete(&self, entity_id: i32) -> Result<access::Model, DbErr>;
}
#[async_trait]
impl AccessContextTrait for AccessContext {}
impl AccessContextTrait for AccessContext {
async fn get_access_by_uid_and_model_id(&self, uid: i32, model_id: i32) -> Result<Option<access::Model>, DbErr>;
}
}

mock! {
Expand Down

0 comments on commit 25940ef

Please sign in to comment.