Skip to content

Commit 3243963

Browse files
committed
features 別テーブルに
1 parent ebf9e6d commit 3243963

3 files changed

Lines changed: 88 additions & 7 deletions

File tree

isuumo/webapp/mysql/db/0_Schema.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ CREATE TABLE isuumo.estate
2323
INDEX idx_door_width (door_width)
2424
);
2525

26+
CREATE TABLE isuumo.estate_features
27+
(
28+
estate_id INTEGER NOT NULL,
29+
feature VARCHAR(64) NOT NULL,
30+
INDEX idx_estate_id (estate_id)
31+
);
32+
2633
CREATE TABLE isuumo.chair
2734
(
2835
id INTEGER NOT NULL PRIMARY KEY,
@@ -43,7 +50,8 @@ CREATE TABLE isuumo.chair
4350
INDEX idx_width (width),
4451
INDEX idx_depth (depth),
4552
INDEX idx_color (color),
46-
INDEX idx_kind (kind)
53+
INDEX idx_kind (kind),
54+
INDEX idx_stock (stock)
4755
);
4856

4957
CREATE TABLE isuumo.estate_search_log

isuumo/webapp/mysql/db/estate_features.sql

Lines changed: 38 additions & 0 deletions
Large diffs are not rendered by default.

isuumo/webapp/nodejs/app.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,42 @@ app.post("/initialize", async (req, res, next) => {
6666
"0_Schema.sql",
6767
"1_DummyEstateData.sql",
6868
"2_DummyChairData.sql",
69+
"estate_features.sql"
6970
];
7071
const execfiles = dbfiles.map((file) => path.join(dbdir, file));
7172
for (const execfile of execfiles) {
7273
await exec(
7374
`mysql -h ${dbinfo.host} -u ${dbinfo.user} -p${dbinfo.password} -P ${dbinfo.port} ${dbinfo.database} < ${execfile}`
7475
);
7576
}
77+
78+
// const getConnection = promisify(db.getConnection.bind(db));
79+
// const connection = await getConnection();
80+
// const query = promisify(connection.query.bind(connection));
81+
// try {
82+
// const estates = await query("SELECT * FROM estate");
83+
// for (let e of estates) {
84+
// const features = e.features.split(",");
85+
// if (!features) {
86+
// continue;
87+
// }
88+
// for (let f of features) {
89+
// if (f != '') {
90+
// await query(
91+
// "INSERT INTO estate_features(estate_id, feature) VALUES(?, ?)",
92+
// [e.id, f]
93+
// );
94+
// }
95+
// }
96+
// }
97+
// } catch (e) {
98+
// console.log(e);
99+
// next(e);
100+
// return;
101+
// } finally {
102+
// await connection.release();
103+
// }
104+
76105
res.json({
77106
language: "nodejs",
78107
});
@@ -396,11 +425,9 @@ app.get("/api/estate/search", async (req, res, next) => {
396425

397426
if (!!features) {
398427
const featureConditions = features.split(",");
399-
400-
for (const featureCondition of featureConditions) {
401-
searchQueries.push("FIND_IN_SET(?, features) > 0");
402-
queryParams.push(featureCondition);
403-
}
428+
const sql = "EXISTS(SELECT 1 FROM estate_features WHERE estate_features.estate_id = estate.id AND estate_features.feature IN (?) HAVING COUNT(*) = ?)";
429+
searchQueries.push(sql);
430+
queryParams.push(featureConditions, featureConditions.length)
404431
}
405432

406433
if (searchQueries.length === 0) {
@@ -421,7 +448,7 @@ app.get("/api/estate/search", async (req, res, next) => {
421448
const pageNum = parseInt(page, 10);
422449
const perPageNum = parseInt(perPage, 10);
423450

424-
const sqlprefix = "SELECT SQL_CALC_FOUND_ROWS * FROM estate WHERE ";
451+
const sqlprefix = `SELECT SQL_CALC_FOUND_ROWS * FROM estate WHERE `;
425452
const searchCondition = searchQueries.join(" AND ");
426453
const limitOffset = " ORDER BY popularity DESC, id ASC LIMIT ? OFFSET ?";
427454
const coundSql = "SELECT FOUND_ROWS() as count";
@@ -441,6 +468,7 @@ app.get("/api/estate/search", async (req, res, next) => {
441468
estates: camelcaseKeys(estates),
442469
});
443470
} catch (e) {
471+
console.log(e);
444472
next(e);
445473
} finally {
446474
await connection.release();
@@ -621,6 +649,13 @@ app.post("/api/estate", upload.single("estates"), async (req, res, next) => {
621649
"INSERT INTO estate(id, name, description, thumbnail, address, latitude, longitude, rent, door_height, door_width, features, popularity) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)",
622650
items
623651
);
652+
const features = items[10].split(",");
653+
for (let f of features) {
654+
await query(
655+
"INSERT INTO estate_features(estate_id, feature) VALUES(?,?)",
656+
[items[0], f]
657+
);
658+
}
624659
}
625660
await commit();
626661
res.status(201);

0 commit comments

Comments
 (0)