Skip to content
Snippets Groups Projects
Commit 34f49cf8 authored by Gustavo Soviersovski's avatar Gustavo Soviersovski
Browse files

Merge branch 'issue/210' into 'development'

Issue/210: Rota para pesquisa de localidade

See merge request !31
parents 962c84b3 fb19ba5a
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!31Issue/210: Rota para pesquisa de localidade
Pipeline #
......@@ -27,6 +27,7 @@ class ReqQueryFields {
// fieldValues: {}
// }
if(typeof this.fields[field.name] === 'undefined') {
log.debug("added field "+field.name);
this.fields[field.name] = field;
}
return this;
......@@ -55,6 +56,21 @@ class ReqQueryFields {
if(typeof this.fieldValues[fieldValue.name] === 'undefined') {
this.fieldValues[fieldValue.name] = fieldValue;
log.debug("added value "+fieldValue.name);
}
return this;
}
addValueToField(fieldValue, field) {
if(typeof this.fields[field] === 'undefined') {
throw 'No field with name ' +field+ ' defined';
}
if(typeof this.fields[field].values === 'undefined') {
this.fields[field].values = {};
}
if(typeof this.fields[field].values[fieldValue.name] === 'undefined') {
this.fields[field].values[fieldValue.name] = fieldValue;
log.debug("added value "+fieldValue.name+ ' to field ' + field);
}
return this;
}
......@@ -66,8 +82,10 @@ class ReqQueryFields {
let f = this.fields[key];
log.debug('f');
log.debug(f);
Object.keys(this.fieldValues).map((k, i) => {
let value = this.fieldValues[k];
// Unimos os valores aceitos globalmente com os aceitos apenas pelo parâmetro
let values = _.merge(this.fieldValues, f.values);
Object.keys(values).map((k, i) => {
let value = values[k];
log.debug('value');
log.debug(value);
params.push(value.name);
......@@ -119,9 +137,13 @@ class ReqQueryFields {
let field = this.fields[key];
log.debug(field);
let param = req[field.name];
log.debug('param');
log.debug(param);
Object.keys(param).map((k, i) => {
let values = this.fieldValues;
let values = _.merge(this.fieldValues, field.values);
log.debug('ValueS');
log.debug(values);
log.debug('k');
log.debug(k);
if(typeof values[k] !== 'undefined') {
// Clonamos para não alterar
......@@ -151,8 +173,11 @@ class ReqQueryFields {
// Valor sempre vem como string, necessário fazer parse para o banco
if(value.where.type === 'integer') whereValue = parseInt(whereValue, 10);
if(value.where.type === 'double') whereValue = parseFloat(whereValue);
if(value.where.type === 'string') whereValue = '%'+whereValue+'%';
let tbl = value.where.table || value.table;
req.sql.where(tbl+'.'+value.where.field+' '+value.where.relation+' ?', whereValue);
let whereField = (value.where.type === 'string')? 'LOWER('+tbl+'.'+value.where.field+')' : tbl+'.'+value.where.field;
let lower = (value.where.type === 'string') ? ' LOWER(?) ' : ' ? ';
req.sql.where(whereField + ' ' + value.where.relation + lower, whereValue);
}
}
});
......
......@@ -43,7 +43,20 @@ rqf.addField({
foreign: 'fk_estado_id',
foreignTable: 'municipio'
}
});
}).addField({
name: 'search',
field: false,
where: true
}).addValueToField({
name: 'name',
table: 'municipio',
tableField: 'nome',
where: {
relation: 'LIKE',
type: 'string',
field: 'nome'
}
}, 'search');;
// Return all cities
cityApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
......@@ -51,4 +64,4 @@ cityApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
next();
}, query, response('city'));
module.exports = cityApp;
\ No newline at end of file
module.exports = cityApp;
......@@ -28,7 +28,21 @@ rqf.addField({
field: 'pk_regiao_id',
table: 'regiao'
}
});
}).addField({
name: 'search',
field: false,
where: true
}).addValueToField({
name: 'name',
table: 'regiao',
tableField: 'nome',
where: {
relation: 'LIKE',
type: 'string',
field: 'nome',
table: 'regiao'
}
}, 'search');
regionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
req.sql.from('regiao');
......
......@@ -43,7 +43,20 @@ rqf.addField({
foreign: 'fk_regiao_id',
foreignTable: 'estado'
}
});
}).addField({
name: 'search',
field: false,
where: true
}).addValueToField({
name: 'name',
table: 'estado',
tableField: 'nome',
where: {
relation: 'LIKE',
type: 'string',
field: 'nome'
}
}, 'search');
stateApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
req.sql.from('estado')
......
......@@ -70,4 +70,18 @@ describe('request cities', () => {
done();
})
})
it('should search for Curitiba', (done) => {
chai.request(server)
.get('/api/v1/city?search=name:curitiba')
.end((err, res) => {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('result');
res.body.result.should.be.a('array');
res.body.result[0].should.have.property('pk_cod_ibge');
res.body.result[0].should.have.property('nome');
done();
});
});
});
......@@ -53,4 +53,19 @@ describe('request regions', () => {
done();
});
});
it('shoul search for south region', (done) => {
chai.request(server)
.get('/api/v1/region?search=name:sul')
.end((err, res) => {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('result');
res.body.result.should.be.a('array');
res.body.result.should.have.length(1);
res.body.result[0].should.have.property('pk_regiao_id');
res.body.result[0].should.have.property('nome');
done();
});
});
});
......@@ -71,4 +71,20 @@ describe('request states', () => {
done();
});
});
it('should search for Paraná', (done) => {
chai.request(server)
.get('/api/v1/state?search=name:paran')
.end((err, res) => {
res.should.have.status(200);
res.should.be.json;
res.body.should.have.property('result');
res.body.result.should.be.a('array');
res.body.result.should.have.length(1);
res.body.result[0].should.have.property('pk_estado_id');
res.body.result[0].should.have.property('fk_regiao_id');
res.body.result[0].should.have.property('nome');
done();
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment