did a reset

This commit is contained in:
Nicolai Van der Storm 2025-02-06 14:34:56 +01:00
parent a577a5aa48
commit cea215f0f8
15 changed files with 3087 additions and 2 deletions

7
.gitignore vendored
View File

@ -1 +1,8 @@
/target /target
# Added by cargo
#
# already existing elements were commented out
#/target

2931
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,3 +4,10 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
async-trait = "0.1.86"
axum = "0.8.1"
sea-orm = { version = "1.1.4", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
thiserror = "2.0.11"
tokio = { version = "1.43.0", features = ["full"] }

49
src/base/controller.rs Normal file
View File

@ -0,0 +1,49 @@
use axum::{
extract::{Path, State},
response::Json,
routing::{get, post, delete, put},
Router,
};
use sea_orm::DatabaseConnection;
use serde_json::{json, Value};
use crate::error::ApiError;
use super::service::BaseService;
pub struct BaseController<S: BaseService> {
service: S,
}
impl<S: BaseService + Send + Sync + 'static> BaseController<S> {
pub fn new(service: S) -> Self {
Self { service }
}
pub fn routes(&self) -> Router<DatabaseConnection> {
Router::new()
.route("/", get(Self::find_all))
.route("/", post(Self::create))
.route("/:id", get(Self::find_by_id))
.route("/:id", put(Self::update))
.route("/:id", delete(Self::delete))
}
async fn find_all(
State(db): State<DatabaseConnection>,
service: S,
) -> Result<Json<Value>, ApiError> {
let items = service.find_all(&db).await?;
Ok(Json(json!({ "data": items })))
}
async fn find_by_id(
State(db): State<DatabaseConnection>,
Path(id): Path<i32>,
service: S,
) -> Result<Json<Value>, ApiError> {
let item = service.find_by_id(&db, id).await?;
Ok(Json(json!({ "data": item })))
}
// Additional CRUD methods implementation...
}

8
src/base/entity.rs Normal file
View File

@ -0,0 +1,8 @@
use sea_orm::EntityTrait;
use serde::{Deserialize, Serialize};
pub trait BaseEntity: EntityTrait {
type CreateDto: for<'de> Deserialize<'de> + Send + Sync;
type UpdateDto: for<'de> Deserialize<'de> + Send + Sync;
type ResponseDto: Serialize + Send + Sync;
}

0
src/base/mod.rs Normal file
View File

15
src/base/service.rs Normal file
View File

@ -0,0 +1,15 @@
use async_trait::async_trait;
use sea_orm::{DatabaseConnection, EntityTrait};
use crate::error::ApiError;
#[async_trait]
pub trait BaseService {
type Entity: EntityTrait;
async fn find_all(&self, db: &DatabaseConnection) -> Result<Vec<Self::Entity::Model>, ApiError>;
async fn find_by_id(&self, db: &DatabaseConnection, id: i32) -> Result<Vec<Self::Entity::Model>, ApiError>;
async fn create(&self, db: &DatabaseConnection, data: <Self::Entity as EntityTrait>::Model) -> Result<Self::Entity::Model, ApiError>;
async fn update(&self, db: &DatabaseConnection, id: i32, data: <Self::Entity as EntityTrait>::Model) -> Result<Self::Entity::Model, ApiError>;
async fn delete(&self, db: &DatabaseConnection, id: i32) -> Result<(), ApiError>;
}

1
src/entities/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod user;

37
src/entities/user.rs Normal file
View File

@ -0,0 +1,37 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use crate::base::entity::BaseEntity;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "users")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub email: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Deserialize)]
pub struct CreateUserDto {
pub name: String,
pub email: String,
}
#[derive(Deserialize)]
pub struct UpdateUserDto {
pub name: Option<String>,
pub email: Option<String>,
}
impl BaseEntity for Model {
type CreateDto = CreateUserDto;
type UpdateDto = UpdateUserDto;
type ResponseDto = Model;
}

0
src/error.rs Normal file
View File

5
src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod base;
pub mod entities;
pub mod error;
pub mod routes;
pub mod services;

View File

@ -1,3 +1,16 @@
fn main() { use axum::{Server, Router};
println!("Hello, world!"); use sea_orm::Database;
#[tokio::main]
async fn main() {
let db = Database::connect("postgres://user:pass@localhost/dbname").await.unwrap();
let app = Router::new()
.nest("/user", UserController::new(UserService).routes())
.with_state(db);
Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
} }

0
src/routes/mod.rs Normal file
View File

1
src/services/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod user_service;

View File

@ -0,0 +1,11 @@
use crate::base::service::BaseService;
use crate::entities::user::{Entity as User, Model as UserModel};
pub struct UserService;
#[async_trait]
impl BaseService for UserService {
type Entity = User;
// Implement the required methods...
}