From f7932c366bab2f5d0726f4d1cdf5b9dbeddf92eb Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 14 Mar 2021 00:39:37 +0100 Subject: feat: find repo by id --- src/repository/models.rs | 23 ++++++++++++++++++++++- src/repository/routes.rs | 15 +++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/repository/models.rs b/src/repository/models.rs index ec5559b..54a93d7 100644 --- a/src/repository/models.rs +++ b/src/repository/models.rs @@ -1,5 +1,5 @@ use crate::db::get_client; -use crate::errors::AppError; +use crate::errors::{AppError, AppErrorType}; use chrono::NaiveDateTime; use deadpool_postgres::Pool; use serde::{Deserialize, Serialize}; @@ -33,4 +33,25 @@ impl Repository { Ok(repos) } + + pub async fn find(pool: Pool, id: &Uuid) -> Result { + let client = get_client(pool.clone()).await.unwrap(); + let statement = client + .prepare("SELECT * FROM repository WHERE id = $1") + .await?; + + let repo = client + .query_opt(&statement, &[&id]) + .await? + .map(|row| Repository::from_row_ref(&row).unwrap()); + + match repo { + Some(repo) => Ok(repo), + None => Err(AppError { + error_type: AppErrorType::NotFoundError, + cause: None, + message: Some("Repository not found".to_string()), + }), + } + } } diff --git a/src/repository/routes.rs b/src/repository/routes.rs index 6b3a942..634d210 100644 --- a/src/repository/routes.rs +++ b/src/repository/routes.rs @@ -3,6 +3,7 @@ use crate::errors::AppErrorResponse; use crate::repository::models::Repository; use actix_web::{web, HttpResponse, Responder}; use slog::info; +use uuid::Uuid; async fn index(state: web::Data) -> impl Responder { let result = Repository::find_all(state.pool.clone()).await; @@ -16,6 +17,20 @@ async fn index(state: web::Data) -> impl Responder { } } +async fn get_repo( + state: web::Data, + id: web::Path<(Uuid,)>, +) -> impl Responder { + let result = Repository::find(state.pool.clone(), &id.0).await; + info!(state.log, "GET /repo/{}/", id.0); + result + .map(|repo| HttpResponse::Ok().json(repo)) + .map_err(|e| e) +} + pub fn config(cfg: &mut web::ServiceConfig) { cfg.service(web::resource("/repo{_:/?}").route(web::get().to(index))) + .service( + web::resource("/repo/{id}{_:/?}").route(web::get().to(get_repo)), + ); } -- cgit v1.2.3-18-g5258