containers read endpoint
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Jake Hillion 2022-07-24 21:58:25 +01:00
parent 3a44c299f3
commit bf92a2ea6d
2 changed files with 22 additions and 2 deletions

View File

@ -1,4 +1,4 @@
use super::{ApiEndpoint, ApiResult, ApiSuccess, State};
use super::{ApiEndpoint, ApiError, ApiResult, ApiSuccess, State};
use crate::db;
use std::sync::Arc;
@ -9,6 +9,7 @@ use uuid::Uuid;
pub fn setup_routes(mut route: tide::Route<Arc<State>>) {
route.at("/").get(ApiEndpoint(index));
route.at("/:id/").get(ApiEndpoint(read));
}
#[derive(Queryable, Serialize)]
@ -28,3 +29,22 @@ async fn index(req: tide::Request<Arc<State>>) -> ApiResult<Vec<Container>> {
Ok(ApiSuccess::Ok(items))
}
async fn read(req: tide::Request<Arc<State>>) -> ApiResult<Container> {
use db::schema::containers::dsl::*;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
let req_id: Uuid = req
.param("id")
.unwrap()
.try_into()
.map_err(|_| ApiError::NotFound)?;
let mut connection = req.state().db.get().unwrap();
let item: Container = containers
.filter(id.eq(req_id))
.first::<Container>(&mut connection)?;
Ok(ApiSuccess::Ok(item))
}

View File

@ -1,7 +1,7 @@
mod containers;
mod error;
use error::{ApiEndpoint, ApiResult, ApiSuccess};
use error::{ApiEndpoint, ApiError, ApiResult, ApiSuccess};
use std::sync::Arc;