Skip to content

Commit

Permalink
Merge pull request #1104 from TrainLCD/fix/remove-pool-clone
Browse files Browse the repository at this point in the history
poolをcloneせずarc使う
  • Loading branch information
TinyKitten authored Oct 27, 2024
2 parents ff8b182 + d2d60ca commit 1729c2c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
6 changes: 3 additions & 3 deletions stationapi/src/infrastructure/company_repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use sqlx::{MySql, MySqlConnection, Pool};
use std::sync::Arc;

use crate::domain::{
entity::company::Company, error::DomainError, repository::company_repository::CompanyRepository,
Expand Down Expand Up @@ -40,13 +41,12 @@ impl From<CompanyRow> for Company {
}
}

#[derive(Debug, Clone)]
pub struct MyCompanyRepository {
pool: Pool<MySql>,
pool: Arc<Pool<MySql>>,
}

impl MyCompanyRepository {
pub fn new(pool: Pool<MySql>) -> Self {
pub fn new(pool: Arc<Pool<MySql>>) -> Self {
Self { pool }
}
}
Expand Down
6 changes: 3 additions & 3 deletions stationapi/src/infrastructure/line_repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use sqlx::{MySql, MySqlConnection, Pool};
use std::sync::Arc;

use crate::domain::{
entity::line::Line, error::DomainError, repository::line_repository::LineRepository,
Expand Down Expand Up @@ -70,13 +71,12 @@ impl From<LineRow> for Line {
}
}

#[derive(Debug, Clone)]
pub struct MyLineRepository {
pool: Pool<MySql>,
pool: Arc<Pool<MySql>>,
}

impl MyLineRepository {
pub fn new(pool: Pool<MySql>) -> Self {
pub fn new(pool: Arc<Pool<MySql>>) -> Self {
Self { pool }
}
}
Expand Down
6 changes: 3 additions & 3 deletions stationapi/src/infrastructure/station_repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use async_trait::async_trait;
use sqlx::{MySql, MySqlConnection, Pool};
use std::sync::Arc;

use crate::{
domain::{
Expand Down Expand Up @@ -174,13 +175,12 @@ struct DistanceWithIdRow {
average_distance: f64,
}

#[derive(Debug, Clone)]
pub struct MyStationRepository {
pool: Pool<MySql>,
pool: Arc<Pool<MySql>>,
}

impl MyStationRepository {
pub fn new(pool: Pool<MySql>) -> Self {
pub fn new(pool: Arc<Pool<MySql>>) -> Self {
Self { pool }
}
}
Expand Down
6 changes: 3 additions & 3 deletions stationapi/src/infrastructure/train_type_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::domain::{
};
use async_trait::async_trait;
use sqlx::{MySql, MySqlConnection, Pool};
use std::sync::Arc;

#[derive(sqlx::FromRow, Clone)]
pub struct TrainTypeRow {
Expand Down Expand Up @@ -59,13 +60,12 @@ impl From<TrainTypeRow> for TrainType {
}
}

#[derive(Debug, Clone)]
pub struct MyTrainTypeRepository {
pool: Pool<MySql>,
pool: Arc<Pool<MySql>>,
}

impl MyTrainTypeRepository {
pub fn new(pool: Pool<MySql>) -> Self {
pub fn new(pool: Arc<Pool<MySql>>) -> Self {
Self { pool }
}
}
Expand Down
11 changes: 6 additions & 5 deletions stationapi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use stationapi::{
station_api::station_api_server::StationApiServer,
use_case::interactor::query::QueryInteractor,
};
use std::sync::Arc;
use std::{
env::{self, VarError},
net::{AddrParseError, SocketAddr},
Expand Down Expand Up @@ -55,12 +56,12 @@ async fn run() -> std::result::Result<(), anyhow::Error> {
let addr = fetch_addr()?;

let db_url = fetch_database_url();
let pool = MySqlPool::connect(db_url.as_str()).await?;
let pool = Arc::new(MySqlPool::connect(db_url.as_str()).await?);

let station_repository = MyStationRepository::new(pool.clone());
let line_repository = MyLineRepository::new(pool.clone());
let train_type_repository = MyTrainTypeRepository::new(pool.clone());
let company_repository = MyCompanyRepository::new(pool.clone());
let station_repository = MyStationRepository::new(Arc::clone(&pool));
let line_repository = MyLineRepository::new(Arc::clone(&pool));
let train_type_repository = MyTrainTypeRepository::new(Arc::clone(&pool));
let company_repository = MyCompanyRepository::new(Arc::clone(&pool));

let query_use_case = QueryInteractor {
station_repository,
Expand Down

0 comments on commit 1729c2c

Please sign in to comment.