OpenLayerHub/src/base/controller.rs
2025-02-11 19:59:00 +01:00

34 lines
901 B
Rust

use anyhow::Result;
use axum::{http::StatusCode, response::IntoResponse, Json};
use diesel::Identifiable;
use serde::Serialize;
use std::marker::PhantomData;
use crate::base::{entity::BaseEntity, i_service::IBaseService};
pub struct BaseController<T, S>
where
T: BaseEntity + Identifiable<Id = i32> + Serialize + Send + Sync + 'static,
S: IBaseService<T> + Send + Sync + 'static,
{
service: S,
_marker: PhantomData<T>,
}
impl<T, S> BaseController<T, S>
where
T: BaseEntity + Identifiable<Id = i32> + Serialize + Send + Sync + 'static,
S: IBaseService<T> + Send + Sync + 'static,
{
pub fn new(service: S) -> Self {
Self {
service,
_marker: PhantomData,
}
}
pub async fn get_all(&self) -> Result<impl IntoResponse> {
let entities = self.service.get_all().await?;
Ok((StatusCode::OK, Json(entities)))
}
}