BLOG-56 refactor: remove redoundent clone
This commit is contained in:
parent
1dc8ca742b
commit
b1b8fab108
@ -1,4 +1,4 @@
|
|||||||
use std::{collections::HashMap, sync::Arc};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
@ -16,11 +16,11 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct PostDbServiceImpl {
|
pub struct PostDbServiceImpl {
|
||||||
db_pool: Arc<Pool<Postgres>>,
|
db_pool: Pool<Postgres>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PostDbServiceImpl {
|
impl PostDbServiceImpl {
|
||||||
pub fn new(db_pool: Arc<Pool<Postgres>>) -> Self {
|
pub fn new(db_pool: Pool<Postgres>) -> Self {
|
||||||
Self { db_pool }
|
Self { db_pool }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ impl PostDbService for PostDbServiceImpl {
|
|||||||
|
|
||||||
let records = query_builder
|
let records = query_builder
|
||||||
.build_query_as::<PostInfoWithLabelRecord>()
|
.build_query_as::<PostInfoWithLabelRecord>()
|
||||||
.fetch_all(&*self.db_pool)
|
.fetch_all(&self.db_pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| PostError::DatabaseError(err.to_string()))?;
|
.map_err(|err| PostError::DatabaseError(err.to_string()))?;
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ impl PostDbService for PostDbServiceImpl {
|
|||||||
|
|
||||||
let records = query_builder
|
let records = query_builder
|
||||||
.build_query_as::<PostWithLabelRecord>()
|
.build_query_as::<PostWithLabelRecord>()
|
||||||
.fetch_all(&*self.db_pool)
|
.fetch_all(&self.db_pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| PostError::DatabaseError(err.to_string()))?;
|
.map_err(|err| PostError::DatabaseError(err.to_string()))?;
|
||||||
|
|
||||||
|
@ -18,8 +18,8 @@ pub struct Container {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Container {
|
impl Container {
|
||||||
pub fn new(db_pool: Arc<Pool<Postgres>>) -> Self {
|
pub fn new(db_pool: Pool<Postgres>) -> Self {
|
||||||
let post_db_service = Arc::new(PostDbServiceImpl::new(db_pool));
|
let post_db_service = Arc::new(PostDbServiceImpl::new(db_pool.clone()));
|
||||||
|
|
||||||
let post_repository = Arc::new(PostRepositoryImpl::new(post_db_service.clone()));
|
let post_repository = Arc::new(PostRepositoryImpl::new(post_db_service.clone()));
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ use actix_web::{
|
|||||||
use post::framework::web::post_web_routes::configure_post_routes;
|
use post::framework::web::post_web_routes::configure_post_routes;
|
||||||
use server::container::Container;
|
use server::container::Container;
|
||||||
use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
|
use sqlx::{Pool, Postgres, postgres::PgPoolOptions};
|
||||||
use std::{env, sync::Arc};
|
use std::env;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_database() -> Arc<Pool<Postgres>> {
|
async fn init_database() -> Pool<Postgres> {
|
||||||
let database_url = env::var("DATABASE_URL")
|
let database_url = env::var("DATABASE_URL")
|
||||||
.unwrap_or_else(|_| "postgres://postgres@localhost:5432/postgres".to_string());
|
.unwrap_or_else(|_| "postgres://postgres@localhost:5432/postgres".to_string());
|
||||||
|
|
||||||
@ -37,11 +37,11 @@ async fn init_database() -> Arc<Pool<Postgres>> {
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to run database migrations");
|
.expect("Failed to run database migrations");
|
||||||
|
|
||||||
Arc::new(db_pool)
|
db_pool
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_app(
|
fn create_app(
|
||||||
db_pool: Arc<Pool<Postgres>>,
|
db_pool: Pool<Postgres>,
|
||||||
) -> App<
|
) -> App<
|
||||||
impl ServiceFactory<
|
impl ServiceFactory<
|
||||||
ServiceRequest,
|
ServiceRequest,
|
||||||
@ -51,10 +51,9 @@ fn create_app(
|
|||||||
Error = Error,
|
Error = Error,
|
||||||
>,
|
>,
|
||||||
> {
|
> {
|
||||||
let container = Container::new(db_pool.clone());
|
let container = Container::new(db_pool);
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(web::Data::new(db_pool))
|
|
||||||
.app_data(web::Data::from(container.post_controller))
|
.app_data(web::Data::from(container.post_controller))
|
||||||
.configure(configure_post_routes)
|
.configure(configure_post_routes)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user