99% of base complete

This commit is contained in:
2025-02-11 19:59:00 +01:00
parent 6d08c39a6d
commit 4451a3cf3d
36 changed files with 867 additions and 5685 deletions

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

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

View File

@@ -1,61 +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 = "product")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub description: Option<String>,
// ... other product-specific fields ...
pub created_at: DateTime,
pub updated_at: DateTime,
}
#[derive(Copy, Clone, Debug, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
// ActiveModel - CORRECT STRUCTURE (No DeriveModel!)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ActiveModel {
pub id: sea_orm::ActiveValue<i32>,
pub name: sea_orm::ActiveValue<String>,
pub description: sea_orm::ActiveValue<Option<String>>,
// ... other fields ...
pub created_at: sea_orm::ActiveValue<DateTime>,
pub updated_at: sea_orm::ActiveValue<DateTime>,
}
impl Entity {
pub fn find_by_id(id: i32) -> Select<'static> {
Self::find().filter(Column::Id.eq(id))
}
}
// Implement get and set methods for the ActiveModel
impl ActiveModel {
pub fn get_name(&self) -> Option<String> {
match self.name {
sea_orm::ActiveValue::Set(val) => Some(val),
_ => None
}
}
pub fn set_name(&mut self, val: String) {
self.name = sea_orm::Set(val);
}
pub fn get_description(&self) -> Option<String> {
match self.description {
sea_orm::ActiveValue::Set(val) => Some(val),
_ => None
}
}
pub fn set_description(&mut self, val: String) {
self.description = sea_orm::Set(val);
}
}

View File

@@ -0,0 +1,29 @@
use axum::Router;
use axum::routing::get;
use crate::base::controller::BaseController;
use super::model::User;
use super::service::UserService;
pub struct UserController {
base_controller: BaseController<User, UserService>,
}
impl UserController {
pub fn new(service: UserService) -> Self {
Self {
base_controller: BaseController::new(service),
}
}
pub fn routes() -> Router {
Router::new()
.route("/users", get(Self::get_users))
}
async fn get_users(
State(controller): State<Arc<UserController>>,
) -> impl IntoResponse {
controller.base_controller.get_all().await
}
}

4
src/routes/user/mod.rs Normal file
View File

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

18
src/routes/user/model.rs Normal file
View File

@@ -0,0 +1,18 @@
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use crate::base::entity::BaseEntity;
use super::schema::users;
#[derive(Debug, Clone, Serialize, Deserialize, Queryable, Identifiable)]
#[diesel(table_name = users)]
pub struct User {
pub id: i32,
pub name: String,
pub email: String,
}
impl BaseEntity for User {
fn id(&self) -> i32 {
self.id
}
}

View File

@@ -0,0 +1,7 @@
diesel::table! {
users (id) {
id -> Int4,
name -> Varchar,
email -> Varchar,
}
}

View File

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