Skip to content

Commit

Permalink
features 別テーブルに
Browse files Browse the repository at this point in the history
  • Loading branch information
yukukotani committed Sep 12, 2020
1 parent ebf9e6d commit 3243963
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
10 changes: 9 additions & 1 deletion isuumo/webapp/mysql/db/0_Schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ CREATE TABLE isuumo.estate
INDEX idx_door_width (door_width)
);

CREATE TABLE isuumo.estate_features
(
estate_id INTEGER NOT NULL,
feature VARCHAR(64) NOT NULL,
INDEX idx_estate_id (estate_id)
);

CREATE TABLE isuumo.chair
(
id INTEGER NOT NULL PRIMARY KEY,
Expand All @@ -43,7 +50,8 @@ CREATE TABLE isuumo.chair
INDEX idx_width (width),
INDEX idx_depth (depth),
INDEX idx_color (color),
INDEX idx_kind (kind)
INDEX idx_kind (kind),
INDEX idx_stock (stock)
);

CREATE TABLE isuumo.estate_search_log
Expand Down
38 changes: 38 additions & 0 deletions isuumo/webapp/mysql/db/estate_features.sql

Large diffs are not rendered by default.

47 changes: 41 additions & 6 deletions isuumo/webapp/nodejs/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,42 @@ app.post("/initialize", async (req, res, next) => {
"0_Schema.sql",
"1_DummyEstateData.sql",
"2_DummyChairData.sql",
"estate_features.sql"
];
const execfiles = dbfiles.map((file) => path.join(dbdir, file));
for (const execfile of execfiles) {
await exec(
`mysql -h ${dbinfo.host} -u ${dbinfo.user} -p${dbinfo.password} -P ${dbinfo.port} ${dbinfo.database} < ${execfile}`
);
}

// const getConnection = promisify(db.getConnection.bind(db));
// const connection = await getConnection();
// const query = promisify(connection.query.bind(connection));
// try {
// const estates = await query("SELECT * FROM estate");
// for (let e of estates) {
// const features = e.features.split(",");
// if (!features) {
// continue;
// }
// for (let f of features) {
// if (f != '') {
// await query(
// "INSERT INTO estate_features(estate_id, feature) VALUES(?, ?)",
// [e.id, f]
// );
// }
// }
// }
// } catch (e) {
// console.log(e);
// next(e);
// return;
// } finally {
// await connection.release();
// }

res.json({
language: "nodejs",
});
Expand Down Expand Up @@ -396,11 +425,9 @@ app.get("/api/estate/search", async (req, res, next) => {

if (!!features) {
const featureConditions = features.split(",");

for (const featureCondition of featureConditions) {
searchQueries.push("FIND_IN_SET(?, features) > 0");
queryParams.push(featureCondition);
}
const sql = "EXISTS(SELECT 1 FROM estate_features WHERE estate_features.estate_id = estate.id AND estate_features.feature IN (?) HAVING COUNT(*) = ?)";
searchQueries.push(sql);
queryParams.push(featureConditions, featureConditions.length)
}

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

const sqlprefix = "SELECT SQL_CALC_FOUND_ROWS * FROM estate WHERE ";
const sqlprefix = `SELECT SQL_CALC_FOUND_ROWS * FROM estate WHERE `;
const searchCondition = searchQueries.join(" AND ");
const limitOffset = " ORDER BY popularity DESC, id ASC LIMIT ? OFFSET ?";
const coundSql = "SELECT FOUND_ROWS() as count";
Expand All @@ -441,6 +468,7 @@ app.get("/api/estate/search", async (req, res, next) => {
estates: camelcaseKeys(estates),
});
} catch (e) {
console.log(e);
next(e);
} finally {
await connection.release();
Expand Down Expand Up @@ -621,6 +649,13 @@ app.post("/api/estate", upload.single("estates"), async (req, res, next) => {
"INSERT INTO estate(id, name, description, thumbnail, address, latitude, longitude, rent, door_height, door_width, features, popularity) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)",
items
);
const features = items[10].split(",");
for (let f of features) {
await query(
"INSERT INTO estate_features(estate_id, feature) VALUES(?,?)",
[items[0], f]
);
}
}
await commit();
res.status(201);
Expand Down

0 comments on commit 3243963

Please sign in to comment.