Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
Implemented API search methods; search trigger, actions and reducer. …
Browse files Browse the repository at this point in the history
…Reimplemented Search component
  • Loading branch information
artkravchenko committed May 24, 2016
1 parent 455911c commit 14dca91
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 87 deletions.
16 changes: 16 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export const UPDATE_EDIT_POST_FORM = 'UPDATE_EDIT_POST_FORM';
export const UI__SET_PROGRESS = 'UI__SET_PROGRESS';
export const UI__TOGGLE_SIDEBAR = 'UI__TOGGLE_SIDEBAR';

export const SET_SEARCH_RESULTS = 'SET_SEARCH_RESULTS';
export const CLEAR_SEARCH_RESULTS = 'CLEAR_SEARCH_RESULTS';

export function addUser(user) {
return {
type: ADD_USER,
Expand Down Expand Up @@ -628,3 +631,16 @@ export function createCommentFailure(postId, message) {
message
};
}

export function setSearchResults(results) {
return {
type: SET_SEARCH_RESULTS,
results
};
}

export function clearSearchResults() {
return {
type: CLEAR_SEARCH_RESULTS
};
}
12 changes: 11 additions & 1 deletion src/api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,17 @@ export default class ApiClient
return response.body;
}

async search(query) {
const response = await this.get(`/api/v1/search/${query}`);
return response.body;
}

async tagCloud() {
const response = await this.get('/api/v1/tag-cloud');
return response.body;
}

async searchTags(query) {
async searchHashtags(query) {
const response = await this.get(`/api/v1/tags/search/${query}`);
return response.body;
}
Expand All @@ -419,6 +424,11 @@ export default class ApiClient
return response.body;
}

async searchSchools(query) {
const response = await this.get(`/api/v1/schools/search/${query}`);
return response.body;
}

async followSchool(name) {
const response = await this.post(`/api/v1/school/${name}/follow`);
return response.body;
Expand Down
83 changes: 69 additions & 14 deletions src/api/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2257,33 +2257,39 @@ export default class ApiController {
}
};

search = async (ctx) => {
const query = ctx.params.query;

try {
const geotags = await this.getSimilarGeotags(query);
const hashtags = await this.getSimilarHashtags(query);
const schools = await this.getSimilarSchools(query);

ctx.body = { geotags, hashtags, schools };
} catch (e) {
ctx.status = 500;
ctx.body = { error: e.message };
}
};

searchGeotags = async (ctx) => {
const Geotag = this.bookshelf.model('Geotag');
const query = ctx.params.query;

try {
const geotags = await Geotag.collection().query(function (qb) {
qb
.where('name', 'ILIKE', `${ctx.params.query}%`)
.limit(10);
}).fetch({ withRelated: ['country', 'admin1'] });
const geotags = await this.getSimilarGeotags(query);

ctx.body = { geotags };
} catch (e) {
ctx.status = 500;
ctx.body = { error: e.message };
return;
}
};

searchTags = async (ctx) => {
const Hashtag = this.bookshelf.model('Hashtag');
searchHashtags = async (ctx) => {
const query = ctx.params.query;

try {
const hashtags = await Hashtag.collection().query(function (qb) {
qb
.where('name', 'ILIKE', `${ctx.params.query}%`)
.limit(10);
}).fetch();
const hashtags = await this.getSimilarHashtags(query);

ctx.body = { hashtags };
} catch (e) {
Expand All @@ -2292,6 +2298,55 @@ export default class ApiController {
}
};

searchSchools = async (ctx) => {
const query = ctx.params.query;

try {
const schools = await this.getSimilarSchools(query);

ctx.body = { schools };
} catch (e) {
ctx.status = 500;
ctx.body = { error: e.message };
}
};

getSimilarGeotags = async (query) => {
const Geotag = this.bookshelf.model('Geotag');

const geotags = await Geotag.collection().query(function (qb) {
qb
.where('name', 'ILIKE', `${query}%`)
.limit(10);
}).fetch({ withRelated: ['country', 'admin1'] });

return geotags;
};

getSimilarHashtags = async (query) => {
const Hashtag = this.bookshelf.model('Hashtag');

const hashtags = await Hashtag.collection().query(function (qb) {
qb
.where('name', 'ILIKE', `${query}%`)
.limit(10);
}).fetch();

return hashtags;
};

getSimilarSchools = async (query) => {
const School = this.bookshelf.model('School');

const schools = await School.collection().query(function (qb) {
qb
.where('name', 'ILIKE', `${query}%`)
.limit(10);
}).fetch();

return schools;
};

/**
* Gets 3 related posts ordered by a number of matching tags + a random number between 0 and 3.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/api/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function initApi(bookshelf) {

api.get('/school-cloud', controller.getSchoolCloud);
api.get('/schools', controller.getSchools);
api.get('/schools/:query', controller.searchSchools);
api.head('/school/:name', controller.checkSchoolExists);
api.get('/school/:url_name', controller.getSchool);
api.post('/school/:id', controller.updateSchool);
Expand Down Expand Up @@ -102,7 +103,7 @@ export function initApi(bookshelf) {
api.get('/pickpoint', controller.pickpoint);

api.get('/tag-cloud', controller.getTagCloud);
api.get('/tags/search/:query', controller.searchTags);
api.get('/tags/search/:query', controller.searchHashtags);
api.get('/tag/:name', controller.getHashtag);
api.post('/tag/:id', controller.updateHashtag);
api.post('/tag/:name/follow', controller.followTag);
Expand All @@ -122,6 +123,8 @@ export function initApi(bookshelf) {

api.get('/quotes', controller.getQuotes);

api.get('/search/:query', controller.search);


return api.routes();
}

0 comments on commit 14dca91

Please sign in to comment.