diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-17 23:27:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-17 23:27:19 +0100 |
commit | 53b34a6a994461260df18ee78c787bac86f5a31f (patch) | |
tree | b663cd9542dd420ef88c53745207eef38e11717b /src/branch/routes.rs | |
parent | 124048f2b7052c47816ce40d8a88a7e3f538fd84 (diff) | |
parent | 3dff10c09ef6db02360a2d2e914e8c2d96ef7a89 (diff) |
Merge pull request #17 from gico-net/feat/add-branch
Add CRUD for Branch model
Diffstat (limited to 'src/branch/routes.rs')
-rw-r--r-- | src/branch/routes.rs | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/branch/routes.rs b/src/branch/routes.rs new file mode 100644 index 0000000..e46d3ad --- /dev/null +++ b/src/branch/routes.rs @@ -0,0 +1,110 @@ +use crate::branch::models::Branch; +use crate::config::AppState; +use crate::errors::{AppError, AppErrorResponse, AppErrorType}; +use crate::helpers::uuid_from_string; + +use actix_web::http::header; +use actix_web::{web, HttpRequest, HttpResponse, Responder}; +use slog::info; +use std::env; +use uuid::Uuid; + +/// Endpoint used for getting all commits +async fn index(state: web::Data<AppState>) -> impl Responder { + info!(state.log, "GET /branch/"); + let result = Branch::find_all(state.pool.clone()).await; + + match result { + Ok(branches) => HttpResponse::Ok().json(branches), + _ => HttpResponse::BadRequest().json(AppErrorResponse { + detail: "Error trying to read all branches from database" + .to_string(), + }), + } +} + +// Endpoint used for getting branches of a repository +async fn get_repo_branch( + state: web::Data<AppState>, + repo: web::Path<(String,)>, +) -> impl Responder { + let uuid: Uuid = uuid_from_string(&repo.0); + info!(state.log, "GET /branch/repo/{}/", &uuid); + + let result = Branch::find_by_repo(state.pool.clone(), &uuid).await; + + result + .map(|branches| HttpResponse::Ok().json(branches)) + .map_err(|e| e) +} + +/// Endpoint used for retrieve a repository that matches with an `id`. +/// It is a String, casted in an Uuid format. +async fn get_branch( + state: web::Data<AppState>, + id: web::Path<(String,)>, +) -> impl Responder { + let uuid: Uuid = uuid_from_string(&id.0); + + let result = Branch::find(state.pool.clone(), &uuid).await; + info!(state.log, "GET /branch/{}/", id.0); + + // `map_err` is also used when repo is not found + result + .map(|repo| HttpResponse::Ok().json(repo)) + .map_err(|e| e) +} + +/// Endpoint used for delete branch. +/// It uses a SECRET_KEY used like an API key +async fn delete_branch( + req: HttpRequest, + state: web::Data<AppState>, + id: web::Path<(String,)>, +) -> impl Responder { + let uuid: Uuid = uuid_from_string(&id.0); + 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 /branch/{}/ 401", id.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 /branch/{}/ 400", id.0); + return Ok(HttpResponse::BadRequest().body("")); + } + }; + + let result = Branch::delete(state.pool.clone(), &uuid).await; + info!(state.log, "DELETE /branch/{}/", id.0); + + result + .map(|_| HttpResponse::NoContent().body("")) + .map_err(|e| e) +} + +/// Routes for branches +pub fn config(cfg: &mut web::ServiceConfig) { + cfg.service( + web::scope("/branch") + .service(web::resource("{_:/?}").route(web::get().to(index))) + .service( + web::resource("/repo/{repo_id}{_:/?}") + .route(web::get().to(get_repo_branch)), + ) + .service( + web::resource("/{id}{_:/?}") + .route(web::get().to(get_branch)) + .route(web::delete().to(delete_branch)), + ), + ); +} |