Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate new sea-orm entites and update services #21

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/template/src/entities/mod.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
pub mod user;
{{/if}}
{{#if is_sea_orm}}
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3

pub mod prelude;

pub mod user;
pub mod users;
{{/if}}
{{#if is_rbatis}}
pub mod user;
Expand Down
5 changes: 3 additions & 2 deletions src/template/src/entities/prelude.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3

pub use super::users::Entity as Users;

pub use super::user::Entity as User;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct User {
}
{{/if}}
{{#if is_sea_orm}}
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down
18 changes: 9 additions & 9 deletions src/template/src/services/user.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
UserUpdateRequest,
},
middleware::jwt::get_token,
entities::{prelude::User,user},
entities::{prelude::Users,users},
utils::rand_utils,
};
use sea_orm::{EntityTrait, Set, ActiveModelTrait, QueryFilter, ColumnTrait};
Expand Down Expand Up @@ -142,12 +142,12 @@ pub async fn users() -> AppResult<Vec<UserResponse>> {
{{#if is_sea_orm}}
pub async fn add_user(req: UserAddRequest) -> AppResult<UserResponse> {
let db = DB.get().ok_or(anyhow::anyhow!("{{database_connection_failed}}"))?;
let model =user::ActiveModel {
let model =users::ActiveModel {
id: Set(Uuid::new_v4().to_string()),
username: Set(req.username.clone()),
password: Set(rand_utils::hash_password(req.password).await?),
};
let user = User::insert(model).exec(db).await?;
let user = Users::insert(model).exec(db).await?;
Ok(UserResponse {
id: user.last_insert_id,
username: req.username,
Expand All @@ -156,7 +156,7 @@ pub async fn add_user(req: UserAddRequest) -> AppResult<UserResponse> {

pub async fn login(req: UserLoginRequest) -> AppResult<UserLoginResponse> {
let db = DB.get().ok_or(anyhow::anyhow!("{{database_connection_failed}}"))?;
let user = User::find().filter(user::Column::Username.eq(req.username)).one(db).await?;
let user = Users::find().filter(users::Column::Username.eq(req.username)).one(db).await?;
if user.is_none() {
return Err(anyhow::anyhow!("{{user_does_not_exist}}").into());
}
Expand All @@ -180,16 +180,16 @@ pub async fn login(req: UserLoginRequest) -> AppResult<UserLoginResponse> {
pub async fn update_user(req: UserUpdateRequest) -> AppResult<UserResponse> {
let db = DB.get().ok_or(anyhow::anyhow!("{{database_connection_failed}}"))?;

let user = User::find_by_id(req.id).one(db).await?;
let user = Users::find_by_id(req.id).one(db).await?;
if user.is_none() {
return Err(anyhow::anyhow!("{{user_does_not_exist}}").into());
}
let mut user: user::ActiveModel = user.unwrap().into();
let mut user: users::ActiveModel = user.unwrap().into();

user.username = Set(req.username.to_owned());
user.password = Set(rand_utils::hash_password(req.password).await?);

let user: user::Model = user.update(db).await?;
let user: users::Model = user.update(db).await?;

Ok(UserResponse {
id: user.id,
Expand All @@ -199,13 +199,13 @@ pub async fn update_user(req: UserUpdateRequest) -> AppResult<UserResponse> {

pub async fn delete_user(id: String) -> AppResult<()> {
let db = DB.get().ok_or(anyhow::anyhow!("{{database_connection_failed}}"))?;
User::delete_by_id(id).exec(db).await?;
Users::delete_by_id(id).exec(db).await?;
Ok(())
}

pub async fn users() -> AppResult<Vec<UserResponse>> {
let db = DB.get().ok_or(anyhow::anyhow!("{{database_connection_failed}}"))?;
let users = User::find().all(db).await?;
let users = Users::find().all(db).await?;
let res = users
.into_iter()
.map(|user| UserResponse {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ pub fn write_project_file(
include_str!("../template/src/entities/mod.hbs"),
),
(
"src/entities/user.rs",
include_str!("../template/src/entities/user.hbs"),
"src/entities/users.rs",
include_str!("../template/src/entities/users.hbs"),
),
(".env", include_str!("../template/.env.hbs")),
]
Expand Down Expand Up @@ -414,7 +414,7 @@ pub fn write_project_file(
),
(
"src/entities/user.rs",
include_str!("../template/src/entities/user.hbs"),
include_str!("../template/src/entities/users.hbs"),
),
]
.as_mut(),
Expand Down Expand Up @@ -456,7 +456,7 @@ pub fn write_project_file(
),
(
"src/entities/user.rs",
include_str!("../template/src/entities/user.hbs"),
include_str!("../template/src/entities/users.hbs"),
),
]
.as_mut(),
Expand Down
Loading