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

Delete agreement by its Id #65

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions entity_api/src/agreement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ pub async fn update(db: &DatabaseConnection, id: Id, model: Model) -> Result<Mod
}
}

pub async fn delete_by_id(db: &DatabaseConnection, id: Id) -> Result<(), Error> {
let result = find_by_id(db, id).await?;

match result {
Some(agreement_model) => {
debug!(
"Existing Agreement model to be deleted: {:?}",
agreement_model
);

agreement_model.delete(db).await?;
Ok(())
}
None => Err(Error {
inner: None,
error_code: EntityApiErrorCode::RecordNotFound,
}),
}
}

pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Option<Model>, Error> {
match Entity::find_by_id(id).one(db).await {
Ok(Some(agreement)) => {
Expand Down
30 changes: 30 additions & 0 deletions web/src/controller/agreement_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use axum::response::IntoResponse;
use axum::Json;
use entity::{agreements::Model, Id};
use entity_api::agreement as AgreementApi;
use serde_json::json;
use service::config::ApiVersion;
use std::collections::HashMap;

Expand Down Expand Up @@ -149,3 +150,32 @@ pub async fn index(

Ok(Json(ApiResponse::new(StatusCode::OK.into(), agreements)))
}

/// DELETE an Agreement specified by its primary key.
#[utoipa::path(
delete,
path = "/agreements/{id}",
params(
ApiVersion,
("id" = i32, Path, description = "Agreement id to delete")
),
responses(
(status = 200, description = "Successfully deleted a certain Agreement by its id", body = [i32]),
(status = 401, description = "Unauthorized"),
(status = 404, description = "Agreement not found"),
(status = 405, description = "Method not allowed")
),
security(
("cookie_auth" = [])
)
)]
pub async fn delete(
CompareApiVersion(_v): CompareApiVersion,
State(app_state): State<AppState>,
Path(id): Path<Id>,
) -> Result<impl IntoResponse, Error> {
debug!("DELETE Agreement by id: {}", id);

AgreementApi::delete_by_id(app_state.db_conn_ref(), id).await?;
Ok(Json(json!({"id": id})))
}
2 changes: 2 additions & 0 deletions web/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use utoipa_rapidoc::RapiDoc;
agreement_controller::update,
agreement_controller::index,
agreement_controller::read,
agreement_controller::delete,
note_controller::create,
note_controller::update,
note_controller::index,
Expand Down Expand Up @@ -132,6 +133,7 @@ fn agreement_routes(app_state: AppState) -> Router {
.route("/agreements/:id", put(agreement_controller::update))
.route("/agreements", get(agreement_controller::index))
.route("/agreements/:id", get(agreement_controller::read))
.route("/agreements/:id", delete(agreement_controller::delete))
.route_layer(login_required!(Backend, login_url = "/login"))
.with_state(app_state)
}
Expand Down
Loading