Skip to content

Commit 3a233ff

Browse files
authored
Merge pull request #157 from PokemonGoers/descriptionFeature
Description feature
2 parents 5458382 + 952cb64 commit 3a233ff

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed

app/api/pokemon.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,28 @@ module.exports = {
161161
res.status(404).json({message: 'Failure. No pokemon details with this name exists!', data: {'name': req.params.name}});
162162
});
163163
},
164+
165+
/**
166+
* @api {get} /api/pokemon/description/:description/ GetPokemonByDescription
167+
* @apiVersion 0.0.1
168+
* @apiName GetPokemonByDescription
169+
* @apiDescription Get pokemon by specific description
170+
* @apiGroup Pokemon
171+
* @apiParam {String} description Description of the pokemon
172+
*
173+
* @apiUse SamplePokemon
174+
* @apiUse NoRecords
175+
*
176+
*/
177+
getByDescription: function(req, res) {
178+
logger.info('Get Pokemon description');
179+
pokemon.getByDescription(req.params.description, function (success, message) {
180+
if(success === 1)
181+
res.status(200).json({message: 'Success', data: message});
182+
else
183+
res.status(404).json({message: 'Failure. No pokemon description of that kind exists!', data: {'description': req.params.description}});
184+
});
185+
},
164186
/**
165187
* @api {get} /api/pokemon/type/:type/ GetPokemonByType
166188
* @apiVersion 0.0.1

app/controllers/filler/pokemon.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020

2121
base.pokemonId = Number(pokemon[i]['Number']);
2222
base.name = pokemon[i]['Name'];
23+
base.description = pokemon[i]['Description'];
2324
base.classification = pokemon[i]['Classification'];
2425

2526
base.types = [];

app/models/pokemon.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Schema = mongoose.Schema;
55

66
let pokemon = {
77
pokemonId: Number,
8+
description: {type: String},
89
name: {type: String},
910
icon: {type: String},
1011
classification: {type: String},

app/stores/pokemon.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,32 @@ module.exports = {
8585
});
8686
callback(1, matches);
8787
},
88+
/*
89+
* searching the pokemon details by closest description
90+
*/
91+
getClosestDescription: function (description, data, callback) {
92+
description = description.toLowerCase();
93+
94+
/*Filter the records that match the first 3 characters of the query*/
95+
let data_clone = _.filter(data, function (datum) {
96+
let datumDescription = datum.description.toLowerCase();
97+
return datum.description.length > 8 && description.substring(0, 7) === datumDescription.substring(0, 7);
98+
});
99+
/*Loop through the data and compute the distance between the query and each name value in the data*/
100+
data_clone.forEach(function (pokemon) {
101+
let pokemonDescription = pokemon.description.toLowerCase();
102+
pokemon.distance = this.getStrDistance(description, pokemonDescription);
103+
}.bind(this));
104+
105+
/*Sort the data in ascending order of distance*/
106+
data_clone = _.sortBy(data_clone, "distance");
107+
/*Select the first 3 data records (3 records that are closest to the query).
108+
* Remove the "distance" */
109+
let matches = _.map(_.first(data_clone, 3), function (match) {
110+
return match;//_.omit(match, "distance");
111+
});
112+
callback(1, matches);
113+
},
88114
/*
89115
* searching the pokemonIcon details by Id
90116
*/
@@ -128,6 +154,26 @@ module.exports = {
128154
}
129155
}.bind(this));
130156
},
157+
/*
158+
* get the pokemon details of particular pokemon based on description
159+
*/
160+
getByDescription : function (description, callback) {
161+
this.get({'description': new RegExp('^.*' + description + '.*$', 'i')}, function (status, response) {
162+
if (status === 1 && _.isEmpty(response) && description.length > 8) {
163+
this.getAll(function (status, response) {
164+
if (status === 1) {
165+
this.getClosestDescription(description, response, function (status, response) {
166+
callback(status, response);
167+
});
168+
} else {
169+
callback(status, response);
170+
}
171+
}.bind(this));
172+
} else {
173+
callback(status, response);
174+
}
175+
}.bind(this));
176+
},
131177
/*
132178
* get the pokemon details of particular pokemon based on type
133179
*/

0 commit comments

Comments
 (0)