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

added find reviews by vehicle route #4

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions typescript-express-reviews/src/api/Review/findByVehicle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express, { Request, Response } from 'express';
import Review from '../../models/review';

async function findByVehicle (request: Request, response: Response) {
const limit = request.body.limit != null ? request.body.limit : 5;
const skip = request.body.skip != null ? request.body.skip : 5;
const reviews = await Review.find({ vehicleId: request.body.vehicleId }).sort({ createdAt: -1 }).skip(skip).limit(limit);
return response.status(200).json({ reviews: reviews });
};

export default findByVehicle;
2 changes: 1 addition & 1 deletion typescript-express-reviews/src/api/Vehicle/findById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Vehicle from '../../models/vehicle';
import Review from '../../models/review';
Copy link
Member

Choose a reason for hiding this comment

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

Why is this file Vehicle/findById instead of Review/findByVehicle?

Copy link
Contributor Author

@IslandRhythms IslandRhythms Jul 29, 2022

Choose a reason for hiding this comment

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


async function last5(request: Request, response: Response) {
const limit = request.body.limit ? request.body.limit : 5;
const limit = request.body.limit != null ? request.body.limit : 5;
const vehicle = await Vehicle.findById({ _id: request.body.vehicleId });
const reviews = await Review.find({ vehicleId: request.body.vehicleId }).sort({ createdAt: -1 }).limit(limit);

Expand Down
2 changes: 2 additions & 0 deletions typescript-express-reviews/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import register from './User/register';
import login from './User/login';
import create from './Review/create';
import last5 from './Vehicle/findById';
import findByVehicle from './Review/findByVehicle';
import bodyParser from 'body-parser';
import connect from '../models/connect';

Expand All @@ -22,6 +23,7 @@ void async function main() {
app.use('/login', login);
app.use('/review/create', create);
app.use('/vehicle/recent/reviews', last5);
app.use('/vehicle/reviews', findByVehicle);

await app.listen(port);
console.log('Listening on port ' + port);
Expand Down
48 changes: 48 additions & 0 deletions typescript-express-reviews/tests/Vehicle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Review from '../src/models/review';
import User from '../src/models/user';
import { describe, it } from 'mocha';
import last5 from '../src/api/Vehicle/findById';
import findByVehicle from '../src/api/Review/findByVehicle';
import assert from 'assert';
import sinon from 'sinon';

Expand Down Expand Up @@ -60,4 +61,51 @@ describe('Vehicle', function() {
assert.equal(res.json.getCall(0).args[0].reviews.length, 5);
assert(res.json.getCall(0).args[0].reviews[0].text.endsWith('6'));
});

it('Should find all the reviews for the given vehicleId, adhering to the skip and limit parameters', async function() {
const mockRequest = (body) => ({
body
})
const mockResponse = (): ResponseStub => {
const res: ResponseStub = {
status: sinon.stub().returnsThis(),
json: sinon.stub().returnsThis()
};
return res;
};
const user = await User.create({
email: 'test@localhost.com',
firstName: 'Test',
lastName: 'Testerson'
});
const vehicle = await Vehicle.create(
{
make: 'Tesla',
model: 'Model S',
year: 2022,
images: [
'https://tesla-cdn.thron.com/delivery/public/image/tesla/6139697c-9d6a-4579-837e-a9fc5df4a773/bvlatuR/std/1200x628/Model-3-Homepage-Social-LHD',
'https://www.tesla.com/sites/default/files/images/blogs/models_blog_post.jpg'
],
numReviews: 0,
averageReview: 0
},
);
for (let i = 0; i < 7; i++) {
await Review.create({
rating: i > 5 ? 5 : i,
text: 'This is a review that must have length greater than 30. ' + i,
vehicleId: vehicle._id,
userId: user._id
});
}
vehicle.numReviews = 6;
vehicle.averageReview = 3;
await vehicle.save();
const req = mockRequest({ vehicleId: vehicle._id, limit: 0, skip: 0 });
const res = mockResponse();
await findByVehicle(req, res);
assert.equal(res.json.getCall(0).args[0].reviews.length, 7);
assert(res.json.getCall(0).args[0].reviews[0].text.endsWith('6'));
});
});