{"size": 3901, "ext": "js", "max_stars_repo_path": "api/building/infrastructure/buildingRepository.js", "max_stars_repo_name": "stevejkang/junctionx-asia-2020-nodejs", "max_stars_repo_stars_event_min_datetime": "2020-07-05T15:39:00.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-05T15:39:00.000Z", "max_issues_repo_path": "api/building/infrastructure/buildingRepository.js", "max_issues_repo_name": "ENvironmentSet/junctionx-asia-2020-nodejs", "max_issues_repo_issues_event_min_datetime": "2020-06-25T06:01:40.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-25T06:01:40.000Z", "max_forks_repo_path": "api/building/infrastructure/buildingRepository.js", "max_forks_repo_name": "ENvironmentSet/junctionx-asia-2020-nodejs", "max_forks_repo_forks_event_min_datetime": "2020-06-22T06:46:26.000Z", "max_forks_repo_forks_event_max_datetime": "2020-07-05T15:39:11.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 32.2396694215, "max_line_length": 231, "alphanum_fraction": 0.5326839272}
const async = require('async');
const dbInfo = require('../../../config/database');

const conn = dbInfo.init();

module.exports = {
  getBuildingList: () => new Promise((resolve) => {
    async.waterfall([
      (callback) => {
        const query = `
          SELECT 
           jbd_idx AS buildingIdx,
           jbd_building_name AS buildingName,
           jbd_longitude AS longitude,
           jbd_latitude AS latitude
         FROM jxh_building;`;
        conn.query(query, [], (error, result) => {
          if (error) callback(error);
          else callback(null, result)
        })
      }
    ],
    (error, result) => {
      resolve({result: (!error), data: result});
    })
  }),
  checkItem: (buildingIdx) => new Promise((resolve) => {
    async.waterfall([
        (callback) => {
          const query = `
            SELECT
              IF(AVG(jbdr_field4) > 3, "Y", "N") AS "thermal",
              IF(AVG(jbdr_field6) > 3, "Y", "N") AS "mask",
              IF(AVG(jbdr_field3) > 3, "Y", "N") AS "disinfection"
            FROM jxh_building_review
            WHERE jbdr_jbd_idx = ${buildingIdx};`;
          conn.query(query, [], (error, result) => {
            if (error) callback(error);
            else callback(null, result)
          })
        }
      ],
      (error, result) => {
        resolve({result: (!error), data: result});
      })
  }),
  checkUserIsReviewed: (buildingIdx, userName) => new Promise((resolve) => {
    async.waterfall([
      (callback) => {
        const query = `
          SELECT
            COUNT(*) AS count
          FROM jxh_building_review
          WHERE jbdr_ju_username = '${userName}' AND jbdr_jbd_idx = ${buildingIdx};`;
        conn.query(query, [], (error, result) => {
          if (error) callback(error);
          else callback(null, result)
        })
      }
    ],
    (error, result) => {
      if (error || parseInt(result[0].count) === 0) {
        resolve({result: false});
      } else {
        resolve({result: true})
      }
    })
  }),
  getSpecificBuildingReviewInformation: (buildingIdx) => new Promise((resolve) => {
    async.waterfall([
      (callback) => {
        const query = `
          SELECT 
            jbdr_field1, jbdr_field2, jbdr_field3, jbdr_field4, jbdr_field5, jbdr_field6
          FROM jxh_building_review
          WHERE jbdr_jbd_idx = ${buildingIdx}`;
        conn.query(query, [], (error, result) => {
          if (error) callback(error);
          else callback(null, result)
        })
      }
    ],
    (error, result) => {
      resolve({result: (!error), data: result});
    })
  }),
  getSpecificBuildingInformation: (buildingIdx) => new Promise((resolve) => {
    async.waterfall([
      (callback) => {
        const query = `
          SELECT 
            jbd_building_name AS buildingName
          FROM jxh_building
          WHERE jbd_idx = ${buildingIdx}`;
        conn.query(query, [], (error, result) => {
          if (error) callback(error);
          else callback(null, result[0])
        })
      }
    ],
    (error, result) => {
      resolve({result: (!error), data: result});
    })
  }),
  submitReview: (requestParams) => new Promise((resolve) => {
    async.waterfall([
        (callback) => {
          const query = `
            INSERT INTO jxh_building_review(jbdr_field1, jbdr_field2, jbdr_field3, jbdr_field4, jbdr_field5, jbdr_field6, jbdr_jbd_idx, jbdr_ju_username)
              VALUES(${requestParams.field1}, ${requestParams.field2}, ${requestParams.field3}, ${requestParams.field4}, ${requestParams.field5}, ${requestParams.field6}, ${requestParams.buildingIdx}, '${requestParams.username}')`;
          conn.query(query, [], (error, result) => {
            if (error) callback(error);
            else callback(null, result[0])
          })
        }
      ],
      (error, result) => {
        resolve({result: (!error)});
      })
  })
}