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

経路検索API修正 #982

Closed
wants to merge 2 commits into from
Closed
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
192 changes: 124 additions & 68 deletions stationapi/src/infrastructure/line_repository.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use async_trait::async_trait;
use sqlx::{MySql, MySqlConnection, Pool};

Expand Down Expand Up @@ -134,7 +136,8 @@ impl InternalLineRepository {
async fn find_by_id(id: u32, conn: &mut MySqlConnection) -> Result<Option<Line>, DomainError> {
let rows: Option<LineRow> = sqlx::query_as!(
LineRow,
"SELECT DISTINCT l.line_cd,
"SELECT
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
Expand Down Expand Up @@ -184,7 +187,8 @@ impl InternalLineRepository {
) -> Result<Option<Line>, DomainError> {
let rows: Option<LineRow> = sqlx::query_as!(
LineRow,
"SELECT DISTINCT l.line_cd,
"SELECT
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
Expand All @@ -209,12 +213,13 @@ impl InternalLineRepository {
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c
FROM `lines` AS l
JOIN `stations` AS s ON s.station_cd = ?
JOIN `station_station_types` AS sst ON sst.station_cd = s.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE l.line_cd = s.line_cd",
FROM `lines` AS l
JOIN `stations` AS s ON s.station_cd = ?
JOIN `station_station_types` AS sst ON sst.station_cd = s.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE l.line_cd = s.line_cd
ORDER BY l.line_cd",
station_id,
)
.fetch_optional(conn)
Expand All @@ -236,6 +241,8 @@ impl InternalLineRepository {
return Ok(vec![]);
}

let ids: HashSet<u32> = ids.into_iter().collect();

let params = format!("?{}", ", ?".repeat(ids.len() - 1));
let query_str = format!(
"SELECT * FROM `lines` WHERE line_cd IN ( {} ) AND e_status = 0",
Expand All @@ -259,7 +266,8 @@ impl InternalLineRepository {
) -> Result<Vec<Line>, DomainError> {
let rows: Vec<LineRow> = sqlx::query_as!(
LineRow,
"SELECT DISTINCT l.line_cd,
"SELECT DISTINCT
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
Expand All @@ -284,14 +292,15 @@ impl InternalLineRepository {
sst.line_group_cd,
s.station_cd,
s.station_g_cd
FROM `lines` AS l
JOIN `stations` AS s ON s.station_g_cd = ?
AND s.e_status = 0
JOIN `station_station_types` AS sst ON sst.station_cd = s.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE l.line_cd = s.line_cd
AND l.e_status = 0",
FROM `lines` AS l
JOIN `stations` AS s ON s.station_g_cd = ?
AND s.e_status = 0
JOIN `station_station_types` AS sst ON sst.station_cd = s.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE l.line_cd = s.line_cd
AND l.e_status = 0
ORDER BY l.line_cd",
station_group_id
)
.fetch_all(conn)
Expand All @@ -309,29 +318,44 @@ impl InternalLineRepository {
return Ok(vec![]);
}

let station_group_id_vec: HashSet<u32> = station_group_id_vec.into_iter().collect();

let params = format!("?{}", ", ?".repeat(station_group_id_vec.len() - 1));
let query_str = format!(
"SELECT
l.*,
s.station_cd,
s.station_g_cd,
sst.line_group_cd,
COALESCE(a.line_name, l.line_name) AS line_name,
COALESCE(a.line_name_k, l.line_name_k) AS line_name_k,
COALESCE(a.line_name_h, l.line_name_h) AS line_name_h,
COALESCE(a.line_name_r, l.line_name_r) AS line_name_r,
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c
"SELECT DISTINCT
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
l.line_symbol_secondary,
l.line_symbol_extra,
l.line_symbol_primary_color,
l.line_symbol_secondary_color,
l.line_symbol_extra_color,
l.line_symbol_primary_shape,
l.line_symbol_secondary_shape,
l.line_symbol_extra_shape,
l.e_status,
l.e_sort,
l.average_distance,
s.station_cd,
s.station_g_cd,
COALESCE(a.line_name, l.line_name) AS line_name,
COALESCE(a.line_name_k, l.line_name_k) AS line_name_k,
COALESCE(a.line_name_h, l.line_name_h) AS line_name_h,
COALESCE(a.line_name_r, l.line_name_r) AS line_name_r,
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c
FROM `lines` AS l
JOIN `stations` AS s ON s.station_g_cd IN ( {} ) AND s.e_status = 0
LEFT JOIN `station_station_types` AS sst ON sst.station_cd = s.station_cd
JOIN `stations` AS s ON s.station_g_cd IN ( {} )
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE
l.line_cd = s.line_cd
AND l.e_status = 0
GROUP BY s.station_cd",
AND s.e_status = 0
ORDER BY l.line_cd",
params
);

Expand All @@ -352,7 +376,8 @@ impl InternalLineRepository {
) -> Result<Vec<Line>, DomainError> {
let rows: Vec<LineRow> = sqlx::query_as!(
LineRow,
"SELECT DISTINCT l.line_cd,
"SELECT DISTINCT
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
Expand All @@ -377,15 +402,14 @@ impl InternalLineRepository {
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c
FROM `lines` AS l
JOIN `station_station_types` AS sst ON sst.line_group_cd = ?
JOIN `stations` AS s ON s.station_cd = sst.station_cd
AND s.e_status = 0
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE l.line_cd = s.line_cd
AND l.e_status = 0
GROUP BY l.line_cd",
FROM `lines` AS l
JOIN `station_station_types` AS sst ON sst.line_group_cd = ?
JOIN `stations` AS s ON s.station_cd = sst.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE l.line_cd = s.line_cd
AND l.e_status = 0
ORDER BY l.line_cd",
line_group_id
)
.fetch_all(conn)
Expand All @@ -402,29 +426,45 @@ impl InternalLineRepository {
return Ok(vec![]);
}

let line_group_id_vec: HashSet<u32> = line_group_id_vec.into_iter().collect();

let params = format!("?{}", ", ?".repeat(line_group_id_vec.len() - 1));
let query_str = format!(
"SELECT DISTINCT
l.*,
sst.line_group_cd,
COALESCE(a.line_name, l.line_name) AS line_name,
COALESCE(a.line_name_k, l.line_name_k) AS line_name_k,
COALESCE(a.line_name_h, l.line_name_h) AS line_name_h,
COALESCE(a.line_name_r, l.line_name_r) AS line_name_r,
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c,
s.station_cd,
s.station_g_cd
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
l.line_symbol_secondary,
l.line_symbol_extra,
l.line_symbol_primary_color,
l.line_symbol_secondary_color,
l.line_symbol_extra_color,
l.line_symbol_primary_shape,
l.line_symbol_secondary_shape,
l.line_symbol_extra_shape,
l.e_status,
l.e_sort,
l.average_distance,
sst.line_group_cd,
COALESCE(a.line_name, l.line_name) AS line_name,
COALESCE(a.line_name_k, l.line_name_k) AS line_name_k,
COALESCE(a.line_name_h, l.line_name_h) AS line_name_h,
COALESCE(a.line_name_r, l.line_name_r) AS line_name_r,
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c,
s.station_cd,
s.station_g_cd
FROM `lines` AS l
JOIN `station_station_types` AS sst ON sst.line_group_cd IN ( {} )
JOIN `stations` AS s ON s.station_cd = sst.station_cd AND s.e_status = 0
JOIN `stations` AS s ON s.station_cd = sst.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE
l.line_cd = s.line_cd
AND l.e_status = 0
GROUP BY sst.line_group_cd, l.line_cd",
ORDER BY l.line_cd",
params
);

Expand All @@ -446,29 +486,45 @@ impl InternalLineRepository {
return Ok(vec![]);
}

let line_group_id_vec: HashSet<u32> = line_group_id_vec.into_iter().collect();

let params = format!("?{}", ", ?".repeat(line_group_id_vec.len() - 1));
let query_str = format!(
"SELECT DISTINCT
l.*,
sst.line_group_cd,
COALESCE(a.line_name, l.line_name) AS line_name,
COALESCE(a.line_name_k, l.line_name_k) AS line_name_k,
COALESCE(a.line_name_h, l.line_name_h) AS line_name_h,
COALESCE(a.line_name_r, l.line_name_r) AS line_name_r,
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c,
s.station_cd,
s.station_g_cd
l.line_cd,
l.company_cd,
l.line_type,
l.line_symbol_primary,
l.line_symbol_secondary,
l.line_symbol_extra,
l.line_symbol_primary_color,
l.line_symbol_secondary_color,
l.line_symbol_extra_color,
l.line_symbol_primary_shape,
l.line_symbol_secondary_shape,
l.line_symbol_extra_shape,
l.e_status,
l.e_sort,
l.average_distance,
sst.line_group_cd,
COALESCE(a.line_name, l.line_name) AS line_name,
COALESCE(a.line_name_k, l.line_name_k) AS line_name_k,
COALESCE(a.line_name_h, l.line_name_h) AS line_name_h,
COALESCE(a.line_name_r, l.line_name_r) AS line_name_r,
COALESCE(a.line_name_zh, l.line_name_zh) AS line_name_zh,
COALESCE(a.line_name_ko, l.line_name_ko) AS line_name_ko,
COALESCE(a.line_color_c, l.line_color_c) AS line_color_c,
s.station_cd,
s.station_g_cd
FROM `lines` AS l
LEFT JOIN `station_station_types` AS sst ON sst.line_group_cd IN ( {} )
LEFT JOIN `stations` AS s ON s.station_cd = sst.station_cd AND s.e_status = 0
LEFT JOIN `stations` AS s ON s.station_cd = sst.station_cd
LEFT JOIN `line_aliases` AS la ON la.station_cd = s.station_cd
LEFT JOIN `aliases` AS a ON la.alias_cd = a.id
WHERE
l.line_cd = s.line_cd
AND l.e_status = 0
GROUP BY l.line_cd",
ORDER BY l.line_cd",
params
);

Expand Down
Loading
Loading