summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-17 14:14:46 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-17 14:14:46 +0100
commit904c7e40a2d092eea87ca6d9ea80edce8a2aad2a (patch)
tree9749fbf7ff0b9c887df5439aef793bc31c7f202e
parentc3522a8d3b0236b3c9358db4160182cc211f8069 (diff)
feat: add delete commit endpoint
-rw-r--r--src/commit/models.rs32
-rw-r--r--src/commit/routes.rs46
2 files changed, 74 insertions, 4 deletions
diff --git a/src/commit/models.rs b/src/commit/models.rs
index c70ecf2..8b81a15 100644
--- a/src/commit/models.rs
+++ b/src/commit/models.rs
@@ -61,4 +61,36 @@ impl Commit {
}),
}
}
+
+ /// Find a commit and delete it, but before check if "Authorization"
+ /// matches with SECRET_KEY
+ pub async fn delete(
+ pool: Pool,
+ hash: &String,
+ ) -> Result<Commit, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client
+ .prepare(
+ "
+ DELETE FROM commit
+ WHERE hash=$1
+ RETURNING *
+ ",
+ )
+ .await?;
+
+ let commit = client
+ .query_opt(&statement, &[&hash])
+ .await?
+ .map(|row| Commit::from_row_ref(&row).unwrap());
+
+ match commit {
+ Some(commit) => Ok(commit),
+ None => Err(AppError {
+ error_type: AppErrorType::NotFoundError,
+ cause: None,
+ message: Some("Commit not found".to_string()),
+ }),
+ }
+ }
}
diff --git a/src/commit/routes.rs b/src/commit/routes.rs
index 1b73c4f..e49f698 100644
--- a/src/commit/routes.rs
+++ b/src/commit/routes.rs
@@ -1,9 +1,10 @@
use crate::commit::models::Commit;
use crate::config::AppState;
-use crate::errors::AppErrorResponse;
-
-use actix_web::{web, HttpResponse, Responder};
+use crate::errors::{AppError, AppErrorResponse, AppErrorType};
+use actix_web::http::header;
+use actix_web::{web, HttpRequest, HttpResponse, Responder};
use slog::info;
+use std::env;
/// Endpoint used for getting all commits
async fn index(state: web::Data<AppState>) -> impl Responder {
@@ -33,6 +34,42 @@ async fn get_commit(
.map_err(|e| e)
}
+/// Endpoint used for delete commitsitory.
+/// It uses a SECRET_KEY used like an API key
+async fn delete_commit(
+ req: HttpRequest,
+ state: web::Data<AppState>,
+ hash: web::Path<(String,)>,
+) -> impl Responder {
+ match req.headers().get(header::AUTHORIZATION) {
+ Some(x)
+ if x.to_str().unwrap()
+ != env::var("SECRET_KEY").unwrap_or("".to_string()) =>
+ {
+ info!(state.log, "DELETE /commit/{}/ 401", &hash.0);
+ return Err(AppError {
+ error_type: AppErrorType::AuthorizationError,
+ message: Some(
+ "You must provide a valid Authorization".to_string(),
+ ),
+ cause: None,
+ });
+ }
+ Some(_) => {}
+ None => {
+ info!(state.log, "DELETE /commit/{}/ 400", &hash.0);
+ return Ok(HttpResponse::BadRequest().body(""));
+ }
+ };
+
+ let result = Commit::delete(state.pool.clone(), &hash.0).await;
+ info!(state.log, "DELETE /commit/{}/", &hash.0);
+
+ result
+ .map(|_| HttpResponse::NoContent().body(""))
+ .map_err(|e| e)
+}
+
/// Routes for commits
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
@@ -40,7 +77,8 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.service(web::resource("{_:/?}").route(web::get().to(index)))
.service(
web::resource("/{hash}{_:/?}")
- .route(web::get().to(get_commit)),
+ .route(web::get().to(get_commit))
+ .route(web::delete().to(delete_commit)),
),
);
}