added base for openapi & swaggerui

This commit is contained in:
2025-02-18 18:42:04 +01:00
parent 466f8dd992
commit c29aae3207
6 changed files with 766 additions and 23 deletions

View File

@@ -1,11 +1,8 @@
use std::ops::Deref;
use std::sync::Arc;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{Json, Router};
use axum::routing::get;
use crate::base::controller::BaseController;
use axum::Router;
use super::model::User;
use super::service::UserService;
@@ -14,6 +11,14 @@ pub struct UserController {
base_controller: BaseController<User, UserService>,
}
impl Deref for UserController {
type Target = BaseController<User, UserService>;
fn deref(&self) -> &Self::Target {
&self.base_controller
}
}
impl UserController {
pub fn new(service: UserService) -> Self {
Self {
@@ -21,17 +26,19 @@ impl UserController {
}
}
pub fn routes(state: Arc<UserController>) -> Router {
Router::new()
.route("/users", get(Self::get_users))
.with_state(state)
pub fn routes(controller: Arc<Self>) -> Router {
BaseController::default_routes(controller, "/users")
}
/*
async fn get_users(
State(controller): State<Arc<UserController>>,
) -> Result<impl IntoResponse, (StatusCode, Json<String>)> {
controller.base_controller.get_all().await.map_err(|err| {
(StatusCode::INTERNAL_SERVER_ERROR, Json(err))
})
controller
.base_controller
.get_all()
.await
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, Json(err)))
}
*/
}

View File

@@ -1,9 +1,10 @@
use super::schema::users;
use crate::base::entity::BaseEntity;
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use crate::base::entity::BaseEntity;
use super::schema::users;
use utoipa::ToSchema;
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Identifiable)]
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Identifiable, ToSchema)]
#[diesel(table_name = users, primary_key(id))]
pub struct User {
pub id: i32,
@@ -11,6 +12,13 @@ pub struct User {
pub email: String,
}
#[derive(Debug, Serialize, ToSchema)]
pub struct UserResponse {
pub id: i32,
pub name: String,
pub email: String,
}
impl BaseEntity for User {
fn id(&self) -> i32 {
self.id
@@ -23,4 +31,5 @@ impl Identifiable for User {
fn id(self) -> Self::Id {
self.id
}
}
}