Skip to content

Commit

Permalink
First save in database
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreOucif committed Jul 8, 2023
1 parent 674b06b commit fb40374
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
11 changes: 9 additions & 2 deletions bootstrap/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ use rest_api_adapter::api::user_api::UserApi;

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
PostgresClient {}.run_migrations();
let user_storage_port = Arc::new(PostgresUserAdapter {});
let postgres_client = PostgresClient {
user: "postgres".to_string(),
password: "Passw0rd".to_string(),
host: "localhost".to_string(),
port: "5432".to_string(),
database: "od-rust-template".to_string(),
};
postgres_client.run_migrations();
let user_storage_port = Arc::new(PostgresUserAdapter { postgres_client });
let user_facade_port = Arc::new(UserService { user_storage_port });
let user_api = UserApi {};
user_api.build_api(rocket::build(), user_facade_port).launch().await?;
Expand Down
22 changes: 21 additions & 1 deletion infrastructure/postgres-adapter/src/adapter/postgres_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
use diesel::{prelude::*, RunQueryDsl};
use domain::{model::user::User, port::output::user_storage_port::UserStoragePort};

pub struct PostgresUserAdapter {}
use crate::{
client::postgres_client::PostgresClient,
entity::{schema::users::table, user_entity::UserEntity},
};

pub struct PostgresUserAdapter {
pub postgres_client: PostgresClient,
}

impl UserStoragePort for PostgresUserAdapter {
fn create_user(&self, user: User) -> Result<User, String> {
println!("Saving user for name {}", user.name());
let connection = &mut self.postgres_client.establish_connection();

let user_entity = UserEntity {
id: user.id.to_string(),
name: user.name.to_string(),
};

diesel::insert_into(table)
.values(&user_entity)
.returning(UserEntity::as_returning())
.get_result(connection)
.expect("Error while saving user");

Ok(user)
}
Expand Down
17 changes: 13 additions & 4 deletions infrastructure/postgres-adapter/src/client/postgres_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();

pub struct PostgresClient {}
pub struct PostgresClient {
pub user: String,
pub password: String,
pub host: String,
pub port: String,
pub database: String,
}

impl PostgresClient {
pub fn establish_connection(&self) -> PgConnection {
let database_url = "postgres://postgres:Passw0rd@localhost:5432/od-rust-template";
let database_url = format!(
"postgres://{}:{}@{}:{}/{}",
&self.user, &self.password, &self.host, &self.port, &self.database
);

PgConnection::establish(database_url)
.unwrap_or_else(|error| panic!("Error connecting to {} : {}", database_url, error))
PgConnection::establish(&database_url)
.unwrap_or_else(|error| panic!("Error connecting to {} : {}", &database_url, error))
}

pub fn run_migrations(&self) {
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/postgres-adapter/src/entity/user_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use diesel::prelude::*;

use crate::entity::schema::users;

#[derive(Queryable, Selectable)]
#[derive(Queryable, Selectable, Insertable)]
#[diesel(table_name = users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct UserEntity {
Expand Down

0 comments on commit fb40374

Please sign in to comment.