restarting form a clean slate
This commit is contained in:
parent
e55f4937e4
commit
85718f3aea
7
.gitignore
vendored
7
.gitignore
vendored
@ -1,6 +1,13 @@
|
||||
/target
|
||||
|
||||
|
||||
# Added by cargo
|
||||
#
|
||||
# already existing elements were commented out
|
||||
|
||||
#/target
|
||||
|
||||
|
||||
# Added by cargo
|
||||
#
|
||||
# already existing elements were commented out
|
||||
|
2931
Cargo.lock
generated
2931
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -4,10 +4,3 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[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"] }
|
||||
|
@ -1,49 +0,0 @@
|
||||
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...
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
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;
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
pub mod controller;
|
||||
pub mod entity;
|
||||
pub mod service;
|
@ -1,15 +0,0 @@
|
||||
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 +0,0 @@
|
||||
pub mod user;
|
@ -1,37 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
@ -1,5 +0,0 @@
|
||||
pub mod base;
|
||||
pub mod entities;
|
||||
pub mod error;
|
||||
pub mod routes;
|
||||
pub mod services;
|
17
src/main.rs
17
src/main.rs
@ -1,16 +1,3 @@
|
||||
use axum::{Server, Router};
|
||||
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();
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
pub mod user_service;
|
@ -1,11 +0,0 @@
|
||||
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...
|
||||
}
|
Loading…
Reference in New Issue
Block a user