containers-read #7

Merged
JakeHillion merged 1 commits from containers-read into main 2022-07-25 14:11:10 +01:00
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;