Skip to content

Commit

Permalink
Merge pull request #340 from dadiorchen/soccer
Browse files Browse the repository at this point in the history
Support get tree by token
  • Loading branch information
dadiorchen authored Feb 28, 2022
2 parents 862a4b5 + 88c4ef0 commit 8bd23c6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 55 deletions.
55 changes: 29 additions & 26 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 });
}
});

Expand Down
59 changes: 44 additions & 15 deletions src/models/Tree.js
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -24,21 +26,21 @@ 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 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];
Expand All @@ -51,21 +53,21 @@ 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;
}
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];
Expand All @@ -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;
Expand Down
35 changes: 21 additions & 14 deletions src/models/sqls/SQLTree.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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");
}

Expand All @@ -83,7 +90,7 @@ class SQLTree{
trees.uuid = $1 AND trees.active = true
`,
values: [this.treeUUID],
};
};
console.log("tree:", query);
return query;
}
Expand Down

0 comments on commit 8bd23c6

Please sign in to comment.