Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#169817560 add search functionality #52

Merged
merged 3 commits into from
Jan 28, 2020

Conversation

gadishimwe
Copy link
Contributor

@gadishimwe gadishimwe commented Jan 16, 2020

What does this PR do?

Add search functionality feature

Description of Task to be completed?

  • Add search controller
  • Add search service
  • Add search tests
  • Add search api documentation

How should this be manually tested?

$ git clone https://github.com/andela/the_spinners-backend.git
$ cd the_spinners-backend
$ git checkout  ft-search-functionality-169817560

then
$ npm run test

or you can use Postman to send a get request to /api/search?term={query} and replace the query with your search choice

What are the relevant pivotal tracker stories?

#169817560

Screenshots

Screen Shot 2020-01-17 at 15 18 47

Screen Shot 2020-01-16 at 10 35 28

* tags:
* - Search
* name: Search the requests
* parameters:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/swagger/search.swagger.js Outdated Show resolved Hide resolved
@gadishimwe gadishimwe added Work In Progress Work not yet finished and removed needs review labels Jan 16, 2020
@gadishimwe gadishimwe force-pushed the ft-search-functionality-169817560 branch from aa759ee to 29127a2 Compare January 16, 2020 10:59
@gadishimwe gadishimwe added needs review and removed Work In Progress Work not yet finished labels Jan 16, 2020

const pagination = {
limit,
offset: Number.isNaN(offset) ? undefined : offset
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offset should be zero by default. Also, line 22 cannot return a NaN value so the check here is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adesege Because pagination in this search is optional, I was thinking if the user searches without providing the page value or limit value, he should get the data without pagination. In that situation, line 22 would return NaN and that's why I was checking first. If you say that offset should be zero by default, there would be no pagination. Can you give me your suggestions? because for me without setting the default values for page and limit which would be pagination, I'm not finding any other option except checking.

limit,
offset: Number.isNaN(offset) ? undefined : offset
};
const results = await SearchService.searchAll(userData, term, pagination);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always pass an object as a single param to a function as opposed to setting multiple params

@gadishimwe gadishimwe added Work In Progress Work not yet finished and removed needs review labels Jan 16, 2020
* @returns {response} @memberof SearchService
*/
static getStatusQuery(term) {
if (term === 'approved' || term === 'rejected' || term === 'pending') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the .includes() instead

@gadishimwe gadishimwe force-pushed the ft-search-functionality-169817560 branch from 09dfc7c to 3e6b3ed Compare January 17, 2020 13:16
@gadishimwe gadishimwe added needs review and removed Work In Progress Work not yet finished labels Jan 17, 2020
offset
};
let results;
if (location) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the user searches by both location and name, for instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adesege It returns results for both of them, see below image, it returns results for both location and status and they are different requests as you see
Screen Shot 2020-01-20 at 15 34 46

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Run a search for approved status and location. Show the location property in the return object. Also, remove the pagination queries in the request url

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yh I get it. let me fix it

*
* @class searchController
*/
class searchController {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class names must be in pascal case

Comment on lines 57 to 75
const newTripComment = {
id: faker.random.number({ min: 20, max: 50 }),
userId: loggedInUser.id,
tripId: faker.random.number(),
tripType: 'one-way',
departure: faker.address.city(),
destination: faker.address.city(),
travelDate: faker.date.future(),
originId: faker.random.number({ min: 1, max: 2 }),
destinationId: faker.random.number({ min: 1, max: 2 }),
departureDate: faker.date.future(),
travelReasons: faker.lorem.sentence(),
accommodation: faker.lorem.sentence(),
accommodationId: faker.random.number()
};

const tripComment = {
userId: loggedInUser.id,
tripType: 'one-way',
departure: faker.address.city(),
destination: faker.address.city(),
travelDate: faker.date.future(),
originId: faker.random.number({ min: 1, max: 2 }),
destinationId: faker.random.number({ min: 1, max: 2 }),
departureDate: faker.date.future(),
travelReasons: faker.lorem.sentence(),
accommodation: faker.lorem.sentence(),
accommodationId: faker.random.number()
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use spread operator to copy common object properties

chai.use(chaiHttp);

describe('Test searching requests:', () => {
let departuredate;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A variable with multiple words should be in camel case

done();
});
});
it('Should return status code of 201 on successful trip creation', (done) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you creating a trip here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was creating a trip that later needs to be searched for. But maybe I can use the fixture instead

Comment on lines 54 to 57
const fakerDate = new Date(departuredate);
const month = (`0${fakerDate.getMonth() + 1}`).slice(-2);
const date = (`0${fakerDate.getDate()}`).slice(-2);
const formattedDate = `${fakerDate.getFullYear()}-${month}-${date}`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to a function

});
it('Should return status code of 200 on with search results when search by status and with pagination', (done) => {
chai.request(app)
.get(`/api/search?status=pending&page=${faker.random.number({ min: 1, max: 1 })}&limit=${faker.random.number()}`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no point using faker to generate the page and limit values.

@adesege adesege added Work In Progress Work not yet finished and removed needs review labels Jan 20, 2020
- Add search controller
- Add search service
- Add search tests
- Add search api documentation

[stars #169817560]
@gadishimwe gadishimwe force-pushed the ft-search-functionality-169817560 branch 2 times, most recently from 1ea764d to fc647d5 Compare January 24, 2020 14:17
@gadishimwe gadishimwe force-pushed the ft-search-functionality-169817560 branch from fc647d5 to 3351620 Compare January 24, 2020 14:27
@gadishimwe gadishimwe removed the Work In Progress Work not yet finished label Jan 26, 2020
@bdushimi bdushimi merged commit 5976365 into develop Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants