Skip to content

Commit

Permalink
Fix rust build
Browse files Browse the repository at this point in the history
  • Loading branch information
rashed091 committed Nov 21, 2023
1 parent c350bff commit bb54a67
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
6 changes: 1 addition & 5 deletions src/controllers/user_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use salvo::prelude::*;
use crate::common::database::Database;

#[handler]
pub async fn all_users(
req: &mut Request,
depot: &mut Depot,
res: &mut Response,
) {
pub async fn all_users(_req: &mut Request, _depot: &mut Depot, res: &mut Response) {
let mut connection = Database::new().get_connection();
let users = UserService::list(&mut connection).await;
res.render(Json(users));
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod common;
mod controllers;
mod models;
mod services;
mod schema;
mod services;

use controllers::user_controller;
use dotenv::dotenv;
Expand All @@ -21,13 +21,11 @@ async fn main() {

dotenv().ok();

let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
let host = env::var("HOST").expect("HOST is not set in .env file");
let port = env::var("PORT").expect("PORT is not set in .env file");
let server_url = format!("{host}:{port}");

let router = Router::new()
.get(user_controller::all_users);
let router = Router::new().get(user_controller::all_users);
let acceptor = TcpListener::new(server_url).bind().await;

Server::new(acceptor).serve(router).await;
Expand Down
4 changes: 2 additions & 2 deletions src/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use serde::{Deserialize, Serialize};
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::schema::users;

#[derive(Serialize, Deserialize, Queryable, Insertable)]
#[table_name = "users"]
#[diesel(table_name = users)]
pub struct User {
pub id: Uuid,
pub name: String,
Expand Down
41 changes: 20 additions & 21 deletions src/services/user_service.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use diesel;
use diesel::prelude::*;
use uuid::Uuid;
// use uuid::Uuid;

use crate::common::database::Connection;
use crate::models::user::User;

use crate::schema::users;
use crate::schema::users::dsl::users as all_users;

// use crate::schema::users::dsl::users as all_users;

pub struct UserService;

Expand All @@ -16,26 +15,26 @@ impl UserService {
users::table.load::<User>(connection).unwrap()
}

pub async fn create(connection: &mut Connection, new_user: &User) -> User {
let data = User {
id: Uuid::new_v4(),
name: new_user.name.clone(),
age: new_user.age.clone()
};
// pub async fn create(connection: &mut Connection, new_user: &User) -> User {
// let data = User {
// id: Uuid::new_v4(),
// name: new_user.name.clone(),
// age: new_user.age.clone()
// };

let _result = diesel::insert_into(users::table).values(&data).execute(connection);
// let _result = diesel::insert_into(users::table).values(&data).execute(connection);

users::table
.order(users::id.desc())
.first(connection)
.unwrap()
}
// users::table
// .order(users::id.desc())
// .first(connection)
// .unwrap()
// }

pub async fn delete(connection: &mut Connection, user_id: Uuid) -> bool {
diesel::delete(users::table.find(user_id)).execute(connection).is_ok()
}
// pub async fn delete(connection: &mut Connection, user_id: Uuid) -> bool {
// diesel::delete(users::table.find(user_id)).execute(connection).is_ok()
// }

pub async fn delete_all(connection: &mut Connection) -> bool {
diesel::delete(all_users).execute(connection).is_ok()
}
// pub async fn delete_all(connection: &mut Connection) -> bool {
// diesel::delete(all_users).execute(connection).is_ok()
// }
}

0 comments on commit bb54a67

Please sign in to comment.