summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-14 00:39:37 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-14 00:39:37 +0100
commitf7932c366bab2f5d0726f4d1cdf5b9dbeddf92eb (patch)
tree7913f9f79b01323464285372508fee8da261f285 /src
parent3232579f29d1c91e70e7dc76305d6589c4acbaae (diff)
feat: find repo by id
Diffstat (limited to 'src')
-rw-r--r--src/repository/models.rs23
-rw-r--r--src/repository/routes.rs15
2 files changed, 37 insertions, 1 deletions
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<Repository, AppError> {
+ 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<AppState>) -> impl Responder {
let result = Repository::find_all(state.pool.clone()).await;
@@ -16,6 +17,20 @@ async fn index(state: web::Data<AppState>) -> impl Responder {
}
}
+async fn get_repo(
+ state: web::Data<AppState>,
+ 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)),
+ );
}