Skip to content

Commit

Permalink
Fixes #41 generates a searchID for /search/observationtables POST
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoEVCosta committed May 24, 2019
1 parent fa8f9a2 commit 288fa47
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
58 changes: 58 additions & 0 deletions components/brapi/sqldb/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Created by Bruno Costa 24-05-2019
* Generated by Utilities/createTable.py
*/
'use strict';

module.exports = function(sequelize, DataTypes) {
const Search = sequelize.define('Search', {
id: {
type: DataTypes.INTEGER(11),
autoIncrement: true,
primaryKey: true,
allowNull: false,
unique: true,
},
germplasmDbIds:{
type: DataTypes.JSON,
allowNull: true
},
locationDbIds:{
type: DataTypes.JSON,
allowNull: true
},
observationLevel: DataTypes.STRING(254),
observationTimeStampRangeStart: DataTypes.DATE,
observationTimeStampRangeEnd: DataTypes.DATE,
observationVariableDbIds:{
type: DataTypes.JSON,
allowNull: true
},
programDbIDs:{
type: DataTypes.JSON,
allowNull: true
},
seasonDbIds:{
type: DataTypes.JSON,
allowNull: true
},
studyDbIds:{
type: DataTypes.JSON,
allowNull: true
},
trialDbIds:{
type: DataTypes.JSON,
allowNull: true
},
}, {
tableName: 'Search',
timestamps: false,
underscored: false,

classMethods: {
associate: function associate(models) {
}
},
});
return Search;
};
14 changes: 14 additions & 0 deletions components/brapi/v1.3/models_v1.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ e.Samples_SampleDbId=function(attributes){
})
}

e.observationtables_post=function(attributes){
console.log(attributes)
return db.Search
.create(
attributes.inserts
).then(function(res){
// console.log(res)
return res
}).catch(function(err){
debug_std("model v1.3 | observationtables_post | (create search id ) - Err: "+ err);
return err
})
}

e.germplasm=function(attributes){
return db.Germplasm
.findAndCountAll({
Expand Down
48 changes: 48 additions & 0 deletions components/brapi/v1.3/observationtables_GET.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var fmtWhereAttr = require('./../helpers/formatWhereAttribute');
var controller = require('./../controllers/callController_v1.3');
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
var callStructure = require('./../structures/v1.3/observationtables');
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

module.exports = function(options){
var options= options || {body:{},params:{},query:{}};
options.where={}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
call="observationunits"
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Where logic
//Do this for each where attribute needed.

//missing sort and sortBy


//|||||||||||||||||search||||||||||||||
// attribute=options.params.searchResultsDbId
// var value=fmtWhereAttr(attribute,"in")
// if ( value != null )
// options.where.searchId=value
// delete options.params.searchResultsDbId;
///|||||||||||||||||||||||||||||||||||||||||
attribute="2";
var value=fmtWhereAttr(attribute,"in")
if ( value != null )
options.where.studyId=value




return controller(options,call,callback)

}

function callback(res){
//[The attribute in main table used as uniqueId]
var attribute="id"
//Metadata
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var metadata={}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return {metadata:metadata,attribute:attribute,callStructure:callStructure};
}
51 changes: 51 additions & 0 deletions components/brapi/v1.3/observationtables_POST.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
var controller = require('./../controllers/callControllerCreate_v1.3');
var callStructure = require('./../structures/v1.3/observationtables_POST');
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

module.exports = function(options){
var options= options || {body:{},params:{},query:{}};
options.where={}
options.inserts={}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
call="observationtables_post"
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Where logic
//Do this for each where attribute needed.

//missing sort and sortBy

function sanitizeArray(inString){ //Sanitize the array that is being inserted into table
let array=JSON.parse(inString)
if (array instanceof Array){
function isInt(item){
return typeof item == 'number'
}
if(array.every(isInt)){
return array
}else{
return []
}
}else{
return []
}
}

options.inserts.studyDbIds=sanitizeArray(options.body.studyDbId)


return controller(options,call,callback);

}

function callback(res){
//[The attribute in main table used as uniqueId]
var attribute="id"
//Metadata
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

var metadata={}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
return {metadata:metadata,attribute:attribute,callStructure:callStructure};
}
44 changes: 44 additions & 0 deletions routes/api_v1.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var germplasm = require('./../components/brapi/v1.3/germplasm_GET');
var studies = require('./../components/brapi/v1.3/studies_GET');
var trials = require('./../components/brapi/v1.3/trials_GET');
var observationunits = require('./../components/brapi/v1.3/observationunits_GET');
var observationtables_GET = require('./../components/brapi/v1.3/observationtables_GET')
var observationtables_POST = require('./../components/brapi/v1.3/observationtables_POST')
//var phenotypesSearchV1_3 = require('./../components/brapi/v1.3/phenotypesSearch')
//------------------- End -------------------------------

Expand Down Expand Up @@ -44,5 +46,47 @@ router.get('/observationunits',function(req,res,next){
resolveCall(call,req,res,errMsg);
})

router.post('/search/observationtables',function(req,res){
console.log(req.body)
var errMsg="Router search/observationtables POST - "
var call=observationtables_POST
resolveCall(call,req,res,errMsg);
})

router.get('/search/observationtables/:searchResultsDbId', function(req, res){
var errMsg="Router search/observationtables Get - "
var call=observationtables
call(req).then(function(data){
let refactoredResult=[]
let result = data.result.data
result.forEach(function(data){
let temp={
"headers": Object.keys(data),
"data":[],
"observationVariableDbIds":[],
"observationVariableNames":[]
}
temp.headers.pop()
temp.data=Object.keys(data).map(function(key){
if(typeof data[key]!='object') return data[key]
})
temp.data.pop()
data.observations.forEach(function(obs){
temp.observationVariableDbIds.push(obs.observationVariableDbId)
temp.observationVariableNames.push(obs.observationVariableName)
temp.data.push(obs.value)
})
refactoredResult.push(temp)
})
tsv=refactoredResult[0].headers.reduce(function(res,header){return res+" "+header})+" "+refactoredResult[0].observationVariableDbIds.reduce(function(acum,id){return acum+" "+id})+"<br>"
refactoredResult.forEach(function(row){
tsv+=row.data.reduce(function(res,header){return res+" "+header})+"<br>"
})
res.send(tsv)
}).catch(function(err){
res.json(err)
})
})


module.exports = router;

0 comments on commit 288fa47

Please sign in to comment.