use crate::{db::permissions::PermissionRaw, prelude::*}; #[derive(Deserialize, Debug)] pub struct Params { folder_id: Uuid, permission_type: PermissionRaw, user_id: i32, } pub async fn set( claims: Claims, State(pool): State, Json(params): Json, ) -> GeneralResult { let root = db::folder::get_root(claims.user_id, &pool) .await .handle_internal("Error getting the root folder")?; if params.folder_id == root { return Err(GeneralError::message( StatusCode::BAD_REQUEST, "Cannot delete the root folder", )); } db::folder::get_permissions(params.folder_id, claims.user_id, &pool) .await .map_err(GeneralError::permissions)? .can_manage_guard()?; let folder_info = db::folder::get_by_id(params.folder_id, &pool) .await .handle_internal("Error getting folder info")? .ok_or_else(GeneralError::item_not_found)?; if folder_info.owner_id == params.user_id { return Err(GeneralError::message( StatusCode::BAD_REQUEST, "Cannot set permissions of the folder owner", )); } db::permissions::insert( params.user_id, params.folder_id, params.permission_type, &pool, ) .await .handle_internal("Error writing to the database")?; Ok(StatusCode::NO_CONTENT) }