@@ -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