actix test
This commit is contained in:
24
src/routes/crud.rs
Normal file
24
src/routes/crud.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use actix_web::{dev::{ServiceFactory, ServiceRequest}, web, App};
|
||||
use crate::crud::{handlers, r#trait::CrudEntity};
|
||||
|
||||
pub fn add_crud_routes<T: CrudEntity + 'static>(mut app: App<T>, path: &str) -> App<T>
|
||||
where
|
||||
T::Id: serde::de::DeserializeOwned + serde::Serialize + 'static,
|
||||
T::CreateInput: serde::de::DeserializeOwned + 'static,
|
||||
T::UpdateInput: serde::de::DeserializeOwned + 'static,
|
||||
|
||||
|
||||
T: ServiceFactory<ServiceRequest, Config = (), Error = actix_web::Error, InitError = ()>
|
||||
{
|
||||
app = app.service(
|
||||
web::resource(path)
|
||||
.route(web::get().to(handlers::get_all::<T>))
|
||||
.route(web::post().to(handlers::create::<T>)),
|
||||
);
|
||||
app.service(
|
||||
web::resource(&format!("{}/{{id}}", path))
|
||||
.route(web::get().to(handlers::get_one::<T>))
|
||||
.route(web::put().to(handlers::update::<T>))
|
||||
.route(web::delete().to(handlers::delete::<T>)),
|
||||
)
|
||||
}
|
@@ -1 +1 @@
|
||||
pub mod user;
|
||||
pub mod crud;
|
@@ -1,44 +0,0 @@
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::base::controller::BaseController;
|
||||
use axum::Router;
|
||||
|
||||
use super::model::User;
|
||||
use super::service::UserService;
|
||||
|
||||
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 {
|
||||
base_controller: BaseController::new(service),
|
||||
}
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
*/
|
||||
}
|
@@ -1,4 +0,0 @@
|
||||
pub mod model;
|
||||
pub mod schema;
|
||||
pub mod service;
|
||||
pub mod controller;
|
@@ -1,35 +0,0 @@
|
||||
use super::schema::users;
|
||||
use crate::base::entity::BaseEntity;
|
||||
use diesel::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use utoipa::ToSchema;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Identifiable, ToSchema)]
|
||||
#[diesel(table_name = users, primary_key(id))]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
impl Identifiable for User {
|
||||
type Id = i32;
|
||||
|
||||
fn id(self) -> Self::Id {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +0,0 @@
|
||||
use diesel::table;
|
||||
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Integer,
|
||||
name -> Varchar,
|
||||
email -> Varchar,
|
||||
}
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
use crate::base::service::BaseService;
|
||||
use super::model::User;
|
||||
use super::schema::users;
|
||||
|
||||
pub type UserService = BaseService<User, users::table>;
|
Reference in New Issue
Block a user