Skip to content

Commit

Permalink
Delete /media endpoint (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer authored Nov 22, 2024
1 parent d807c0d commit 6fe3f66
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions wp_api/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,12 @@ pub struct MediaUpdateParams {
// meta field is omitted for now: https://github.com/Automattic/wordpress-rs/issues/381
}

#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
pub struct MediaDeleteResponse {
pub deleted: bool,
pub previous: MediaWithEditContext,
}

#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
pub struct SparseMedia {
#[WpContext(edit, embed, view)]
Expand Down
16 changes: 16 additions & 0 deletions wp_api/src/request/endpoint/media_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@ enum MediaRequest {
List,
#[contextual_get(url = "/media/<media_id>", output = crate::media::SparseMedia, filter_by = crate::media::SparseMediaField)]
Retrieve,
#[delete(url = "/media/<media_id>", output = crate::media::MediaDeleteResponse)]
Delete,
#[post(url = "/media/<media_id>", params = &MediaUpdateParams, output = MediaWithEditContext)]
Update,
}

impl DerivedRequest for MediaRequest {
fn additional_query_pairs(&self) -> Vec<(&str, String)> {
match self {
// The server always returns an error when `force=false`, so a separate `Trash` action
// is not implemented.
MediaRequest::Delete => vec![("force", true.to_string())],
_ => vec![],
}
}

fn namespace() -> impl AsNamespace {
WpNamespace::WpV2
}
Expand Down Expand Up @@ -70,6 +81,11 @@ mod tests {
use rstest::*;
use std::sync::Arc;

#[rstest]
fn delete_media(endpoint: MediaRequestEndpoint) {
validate_wp_v2_endpoint(endpoint.delete(&MediaId(54)), "/media/54?force=true");
}

#[rstest]
#[case(MediaListParams::default(), "")]
#[case(generate!(MediaListParams, (page, Some(2))), "page=2")]
Expand Down
15 changes: 15 additions & 0 deletions wp_api_integration_tests/tests/test_media_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ use wp_api_integration_tests::{
api_client, backend::RestoreServer, AssertResponse, FIRST_POST_ID, MEDIA_ID_611,
};

#[tokio::test]
#[serial]
async fn delete_media() {
// Delete the media using the API and ensure it's successful
let media_delete_response = api_client().media().delete(&MEDIA_ID_611).await;
assert!(
media_delete_response.is_ok(),
"{:#?}",
media_delete_response
);
assert!(media_delete_response.unwrap().data.deleted);

RestoreServer::db().await;
}

generate_update_test!(update_date, date, "2024-09-09T12:00:00".to_string());

generate_update_test!(update_date_gmt, date_gmt, "2024-09-09T12:00:00".to_string());
Expand Down
1 change: 1 addition & 0 deletions wp_api_integration_tests/tests/test_posts_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async fn delete_post() {
// Delete the post using the API and ensure it's successful
let post_delete_response = api_client().posts().delete(&FIRST_POST_ID).await;
assert!(post_delete_response.is_ok(), "{:#?}", post_delete_response);
assert!(post_delete_response.unwrap().data.deleted);

// Assert that the post was deleted
assert!(
Expand Down

0 comments on commit 6fe3f66

Please sign in to comment.