34 lines
901 B
Rust
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)))
|
|
}
|
|
}
|