diff --git a/src/app.js b/src/app.js index 2c5e13e..95a593a 100644 --- a/src/app.js +++ b/src/app.js @@ -11,7 +11,7 @@ Sentry.init({ dsn: null }); const cache = expressLru({ max: 1000, ttl: 60000 * 240, - skip: function(req) { + skip: function (req) { // Don't run if bounds passed in, possibly other cases as well return !!req.user || !!req.query.bounds; } @@ -22,13 +22,13 @@ app.use(bodyParser.json()); // parse application/json app.set('view engine', 'html'); const allowCrossDomain = (req, res, next) => { - res.header('Access-Control-Allow-Origin', '*'); - res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); - res.header('Access-Control-Allow-Headers', 'Content-Type'); - next(); + res.header('Access-Control-Allow-Origin', '*'); + res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); + res.header('Access-Control-Allow-Headers', 'Content-Type'); + next(); } -if(process.env.NODE_ENV == 'dev'){ +if (process.env.NODE_ENV == 'dev') { console.log('disable cors'); app.use(allowCrossDomain); } @@ -56,31 +56,34 @@ app.use("/entities", entity); const nearest = require("./api/nearest"); app.use("/nearest", nearest); -app.get("/tree", async function (req, res){ +app.get("/tree", async function (req, res) { try { - console.log('get tree') - const tree = new Tree(); - const treeId = req.query.tree_id; - const uuid = req.query.uuid; - const treeName = req.query.tree_name; - let treeDetail = {}; - if(treeId){ - treeDetail = await tree.getTreeById(treeId); - } else if(uuid){ - treeDetail = await tree.getTreeByUUID(uuid); - } else if(treeName){ - treeDetail = await tree.getTreeByName(treeName); - } else { - console.warn("tree_id did not match any record", treeId); - res.status(400).json({message:"tree_id did not match any record"}); - } - delete treeDetail.planter_identifier; + console.log('get tree') + const tree = new Tree(); + const treeId = req.query.tree_id; + const uuid = req.query.uuid; + const token = req.query.token; + const treeName = req.query.tree_name; + let treeDetail = {}; + if (treeId) { + treeDetail = await tree.getTreeById(treeId); + } else if (uuid) { + treeDetail = await tree.getTreeByUUID(uuid); + } else if (treeName) { + treeDetail = await tree.getTreeByName(treeName); + } else if (token) { + treeDetail = await tree.getTreeByToken(token); + } else { + console.warn("tree_id did not match any record", treeId); + res.status(400).json({ message: "tree_id did not match any record" }); + } + delete treeDetail.planter_identifier; - res.status(200).json(treeDetail); + res.status(200).json(treeDetail); } catch (error) { console.log(error) - res.status(500).json({message:"something wrong:" + error}); + res.status(500).json({ message: "something wrong:" + error }); } }); diff --git a/src/models/Tree.js b/src/models/Tree.js index 9d26ebf..b3bf7ec 100644 --- a/src/models/Tree.js +++ b/src/models/Tree.js @@ -1,18 +1,20 @@ const SQLTree = require("./sqls/SQLTree"); -const { Pool} = require('pg'); +const { Pool } = require('pg'); +const log = require("loglevel"); -class Tree{ - constructor(){ +class Tree { + constructor() { + log.warn("with db:", process.env.DATABASE_URL); this.pool = new Pool({ connectionString: process.env.DATABASE_URL }); } - async getTreeById(treeId){ + async getTreeById(treeId) { const sql = new SQLTree(); sql.setTreeId(treeId); const query = await sql.getQuery(); const result = await this.pool.query(query); - if(result.rows.length === 0){ -// throw new Error("can not find tree", treeId); + if (result.rows.length === 0) { + // throw new Error("can not find tree", treeId); return undefined; } const treeObject = result.rows[0]; @@ -24,7 +26,7 @@ class Tree{ }; const attributes = await this.pool.query(query); const attributeJson = {}; - for(const r of attributes.rows){ + for (const r of attributes.rows) { attributeJson[r.key] = r.value; } treeObject.attributes = attributeJson; @@ -32,13 +34,13 @@ class Tree{ return treeObject; } - async getTreeByUUID(uuid){ + async getTreeByUUID(uuid) { const sql = new SQLTree(); sql.setTreeUUID(uuid); const query = await sql.getQueryUUID(); const result = await this.pool.query(query); - if(result.rows.length === 0){ -// throw new Error("can not find tree", treeId); + if (result.rows.length === 0) { + // throw new Error("can not find tree", treeId); return undefined; } const treeObject = result.rows[0]; @@ -51,7 +53,7 @@ class Tree{ console.log(query) const attributes = await this.pool.query(query); const attributeJson = {}; - for(const r of attributes.rows){ + for (const r of attributes.rows) { attributeJson[r.key] = r.value; } treeObject.attributes = attributeJson; @@ -59,13 +61,13 @@ class Tree{ return treeObject; } - async getTreeByName(treeName){ + async getTreeByName(treeName) { const sql = new SQLTree(); sql.setTreeName(treeName); const query = await sql.getQuery(); const result = await this.pool.query(query); - if(result.rows.length === 0){ -// throw new Error(`can not find tree ${treeName}`); + if (result.rows.length === 0) { + // throw new Error(`can not find tree ${treeName}`); return undefined; } const treeObject = result.rows[0]; @@ -77,7 +79,34 @@ class Tree{ }; const attributes = await this.pool.query(query); const attributeJson = {}; - for(const r of attributes.rows){ + for (const r of attributes.rows) { + attributeJson[r.key] = r.value; + } + treeObject.attributes = attributeJson; + } + return treeObject; + } + + async getTreeByToken(token) { + const sql = new SQLTree(); + sql.setToken(token); + const query = await sql.getQuery(); + const result = await this.pool.query(query); + if (result.rows.length === 0) { + // throw new Error("can not find tree", treeId); + return undefined; + } + const treeObject = result.rows[0]; + //attribute + { + const query = { + text: "select * from tree_attributes where tree_id = $1", + values: [treeObject.id], + }; + console.log(query) + const attributes = await this.pool.query(query); + const attributeJson = {}; + for (const r of attributes.rows) { attributeJson[r.key] = r.value; } treeObject.attributes = attributeJson; diff --git a/src/models/sqls/SQLTree.js b/src/models/sqls/SQLTree.js index b3daf7f..42c3e73 100644 --- a/src/models/sqls/SQLTree.js +++ b/src/models/sqls/SQLTree.js @@ -1,17 +1,21 @@ -class SQLTree{ +class SQLTree { - setTreeId(treeId){ + setTreeId(treeId) { this.treeId = treeId; } - setTreeName(treeName){ + setTreeName(treeName) { this.treeName = treeName; } - getQuery(){ - if(!this.treeId && !this.treeName){ - throw new Error("treeId or treeName required"); + setToken(token) { + this.token = token; + } + + getQuery() { + if (!this.treeId && !this.treeName && !this.token) { + throw new Error("treeId or treeName or token required"); } const query = { @@ -37,28 +41,31 @@ class SQLTree{ AND trees.active = true `, values: [], - }; + }; console.log("tree:", query); return query; } - getFilter(){ + getFilter() { let filter = ""; - if(this.treeId){ + if (this.treeId) { filter += `AND trees.id = ${this.treeId}`; } - if(this.treeName){ + if (this.treeName) { filter += `AND trees.name = '${this.treeName}'`; } + if (this.token) { + filter += `AND token.id = '${this.token}'`; + } return filter; } - setTreeUUID(uuid){ + setTreeUUID(uuid) { this.treeUUID = uuid; } - getQueryUUID(){ - if(!this.treeUUID){ + getQueryUUID() { + if (!this.treeUUID) { throw new Error("treeUUID required"); } @@ -83,7 +90,7 @@ class SQLTree{ trees.uuid = $1 AND trees.active = true `, values: [this.treeUUID], - }; + }; console.log("tree:", query); return query; }