|
| 1 | +//! Functionality related to deleting a crate. |
| 2 | +
|
| 3 | +use chrono::{Duration, Utc}; |
| 4 | + |
| 5 | +use crate::controllers::cargo_prelude::*; |
| 6 | +use crate::models::Crate; |
| 7 | + |
| 8 | +use crate::schema::*; |
| 9 | +use crate::util::errors::internal; |
| 10 | +use crate::views::{EncodableCrate, GoodCrate, PublishWarnings}; |
| 11 | + |
| 12 | +/// Handles the `DELETE /crates/:crate_id` route. |
| 13 | +/// |
| 14 | +/// Actually deletion is allowed only in the first 24 hours from creation |
| 15 | +pub async fn delete(app: AppState, Path(crate_name): Path<String>) -> AppResult<Json<GoodCrate>> { |
| 16 | + let conn = app.primary_database.get()?; |
| 17 | + let krate: Crate = Crate::by_name(&crate_name).first(&*conn)?; |
| 18 | + |
| 19 | + if Utc::now() |
| 20 | + .naive_utc() |
| 21 | + .signed_duration_since(krate.created_at) |
| 22 | + > Duration::hours(24) |
| 23 | + { |
| 24 | + return Err(cargo_err( |
| 25 | + "Crate deletion is allowed only in the first 24 hours from creation", |
| 26 | + )); |
| 27 | + } |
| 28 | + |
| 29 | + // Create a transaction on the database, if there are no errors, |
| 30 | + // commit the transactions to delete the crate. |
| 31 | + conn.transaction(|| { |
| 32 | + diesel::delete(crates::table.find(krate.id)).execute(&*conn)?; |
| 33 | + |
| 34 | + let uploader = app.config.uploader(); |
| 35 | + uploader |
| 36 | + .delete_index(app.http_client(), &krate.name) |
| 37 | + .map_err(|e| internal(format_args!("failed to delete crate: {e}")))?; |
| 38 | + |
| 39 | + Ok(Json(GoodCrate { |
| 40 | + krate: EncodableCrate::from_minimal(krate, None, None, true, None), |
| 41 | + warnings: PublishWarnings { |
| 42 | + invalid_categories: vec![], |
| 43 | + invalid_badges: vec![], |
| 44 | + other: vec![], |
| 45 | + }, |
| 46 | + })) |
| 47 | + }) |
| 48 | +} |
0 commit comments