actix test

This commit is contained in:
Nicolai Van der Storm
2025-02-21 09:10:11 +01:00
parent 598125abae
commit fbf2dd89dc
25 changed files with 765 additions and 1290 deletions

24
src/routes/crud.rs Normal file
View 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>)),
)
}

View File

@@ -1 +1 @@
pub mod user;
pub mod crud;

View File

@@ -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)))
}
*/
}

View File

@@ -1,4 +0,0 @@
pub mod model;
pub mod schema;
pub mod service;
pub mod controller;

View File

@@ -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
}
}

View File

@@ -1,9 +0,0 @@
use diesel::table;
table! {
users (id) {
id -> Integer,
name -> Varchar,
email -> Varchar,
}
}

View File

@@ -1,5 +0,0 @@
use crate::base::service::BaseService;
use super::model::User;
use super::schema::users;
pub type UserService = BaseService<User, users::table>;