Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[server] Add remove database endpoint #803 #805

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions agdb_server/openapi/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
"version": "0.1.0"
},
"paths": {
"/create_db": {
"/add_db": {
"post": {
"tags": [
"crate::app"
],
"operationId": "create_db",
"operationId": "add_db",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateUser"
"$ref": "#/components/schemas/ServerDatabase"
}
}
},
"required": true
},
"responses": {
"201": {
"description": "Database created"
"description": "Database added"
},
"403": {
"description": "Database exists"
"description": "Database already exists"
},
"461": {
"description": "Invalid database name"
Expand Down Expand Up @@ -80,7 +80,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/DeleteServerDatabase"
"$ref": "#/components/schemas/ServerDatabaseName"
}
}
},
Expand Down Expand Up @@ -154,6 +154,32 @@
}
}
}
},
"/remove_db": {
"post": {
"tags": [
"crate::app"
],
"operationId": "remove_db",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/DeleteServerDatabase"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Database removed"
},
"403": {
"description": "Database not found for user"
}
}
}
}
},
"components": {
Expand All @@ -166,27 +192,27 @@
"memory"
]
},
"DeleteServerDatabase": {
"ServerDatabase": {
"type": "object",
"required": [
"name"
"name",
"db_type"
],
"properties": {
"db_type": {
"$ref": "#/components/schemas/DbType"
},
"name": {
"type": "string"
}
}
},
"ServerDatabase": {
"ServerDatabaseName": {
"type": "object",
"required": [
"name",
"db_type"
"name"
],
"properties": {
"db_type": {
"$ref": "#/components/schemas/DbType"
},
"name": {
"type": "string"
}
Expand Down
7 changes: 4 additions & 3 deletions agdb_server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use utoipa::OpenApi;
#[derive(OpenApi)]
#[openapi(
paths(
crate::app::create_db,
crate::app::add_db,
crate::app::create_user,
crate::app::delete_db,
crate::app::list,
crate::app::login
crate::app::login,
crate::app::remove_db
),
components(schemas(
crate::app::ServerDatabase,
crate::app::DbType,
crate::app::DeleteServerDatabase,
crate::app::ServerDatabaseName,
crate::app::UserCredentials,
crate::app::UserToken
))
Expand Down
46 changes: 36 additions & 10 deletions agdb_server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) struct ServerDatabase {
}

#[derive(Deserialize, ToSchema)]
pub(crate) struct DeleteServerDatabase {
pub(crate) struct ServerDatabaseName {
pub(crate) name: String,
}

Expand Down Expand Up @@ -137,25 +137,26 @@ pub(crate) fn app(shutdown_sender: Sender<()>, db_pool: DbPool) -> Router {
.merge(SwaggerUi::new("/openapi").url("/openapi/openapi.json", Api::openapi()))
.route("/shutdown", routing::get(shutdown))
.route("/error", routing::get(test_error))
.route("/create_db", routing::post(create_db))
.route("/add_db", routing::post(add_db))
.route("/create_user", routing::post(create_user))
.route("/delete_db", routing::post(delete_db))
.route("/list", routing::get(list))
.route("/login", routing::post(login))
.route("/remove_db", routing::post(remove_db))
.layer(logger)
.with_state(state)
}

#[utoipa::path(post,
path = "/create_db",
request_body = CreateUser,
path = "/add_db",
request_body = ServerDatabase,
responses(
(status = 201, description = "Database created"),
(status = 403, description = "Database exists"),
(status = 201, description = "Database added"),
(status = 403, description = "Database already exists"),
(status = 461, description = "Invalid database name"),
)
)]
pub(crate) async fn create_db(
pub(crate) async fn add_db(
user: UserId,
State(db_pool): State<DbPool>,
Json(request): Json<ServerDatabase>,
Expand All @@ -165,7 +166,7 @@ pub(crate) async fn create_db(
}

db_pool
.create_database(
.add_database(
user.0,
Database {
db_id: None,
Expand Down Expand Up @@ -222,7 +223,7 @@ pub(crate) async fn create_user(

#[utoipa::path(post,
path = "/delete_db",
request_body = DeleteServerDatabase,
request_body = ServerDatabaseName,
responses(
(status = 200, description = "Database deleted"),
(status = 403, description = "Database not found for user"),
Expand All @@ -231,7 +232,7 @@ pub(crate) async fn create_user(
pub(crate) async fn delete_db(
user: UserId,
State(db_pool): State<DbPool>,
Json(request): Json<DeleteServerDatabase>,
Json(request): Json<ServerDatabaseName>,
) -> Result<StatusCode, ServerError> {
let db = db_pool
.find_user_database(user.0, &request.name)
Expand Down Expand Up @@ -296,6 +297,31 @@ pub(crate) async fn login(
Ok((StatusCode::OK, Json(UserToken(token))))
}

#[utoipa::path(post,
path = "/remove_db",
request_body = DeleteServerDatabase,
responses(
(status = 200, description = "Database removed"),
(status = 403, description = "Database not found for user"),
)
)]
pub(crate) async fn remove_db(
user: UserId,
State(db_pool): State<DbPool>,
Json(request): Json<ServerDatabaseName>,
) -> Result<StatusCode, ServerError> {
let db = db_pool
.find_user_database(user.0, &request.name)
.map_err(|_| ServerError {
status: StatusCode::FORBIDDEN,
error: anyhow!("Database not found for user"),
})?;

db_pool.remove_database(db)?;

Ok(StatusCode::OK)
}

async fn test_error() -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
Expand Down
19 changes: 11 additions & 8 deletions agdb_server/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl DbPool {
Ok(db_pool)
}

pub(crate) fn create_database(&self, user: DbId, database: Database) -> anyhow::Result<()> {
pub(crate) fn add_database(&self, user: DbId, database: Database) -> anyhow::Result<()> {
let db = ServerDb::new(&format!("{}:{}", database.db_type, database.name))?;
self.get_pool_mut()?.insert(database.name.clone(), db);

Expand Down Expand Up @@ -186,13 +186,7 @@ impl DbPool {
}

pub(crate) fn delete_database(&self, db: Database) -> anyhow::Result<()> {
self.0
.server_db
.get_mut()?
.exec_mut(&QueryBuilder::remove().ids(db.db_id.unwrap()).query())?;

let delete_db = self.get_pool_mut()?.remove(&db.name).unwrap();
let filename = delete_db.get()?.filename().to_string();
let filename = self.remove_database(db)?.get()?.filename().to_string();
let path = Path::new(&filename);

if path.exists() {
Expand Down Expand Up @@ -326,6 +320,15 @@ impl DbPool {
Ok(())
}

pub(crate) fn remove_database(&self, db: Database) -> anyhow::Result<ServerDb> {
self.0
.server_db
.get_mut()?
.exec_mut(&QueryBuilder::remove().ids(db.db_id.unwrap()).query())?;

Ok(self.get_pool_mut()?.remove(&db.name).unwrap())
}

// fn get_pool(&self) -> anyhow::Result<RwLockReadGuard<HashMap<String, ServerDb>>> {
// self.0.pool.read().map_err(map_error)
// }
Expand Down
Loading