Skip to content

Commit

Permalink
Merge pull request #171 from Victor-Palha/feature/issue-81/random-quote
Browse files Browse the repository at this point in the history
feature: add controller and test to random quote
  • Loading branch information
MateuszKikmunter authored Oct 29, 2024
2 parents ce9e229 + bf9a9d4 commit a542476
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
50 changes: 50 additions & 0 deletions backend/controllers/quote.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const router = Router();

router.route('/quote').get(quoteController.getQuotes);
router.route('/quote/:id').get(quoteController.getQuote);
router.route('/quotes/random').get(quoteController.getRandomQuote);

app.use(express.json());
app.use('/v2', router);
Expand Down Expand Up @@ -75,4 +76,53 @@ describe('quote controller', () => {
expect(response.body.success).toEqual(false);
expect(response.body.message).toEqual('Something went wrong.');
});

it("/quotes/random should return a random quote", async () => {
const fakeQuotes = [
{
_id: '5cd96e05de30eff6ebccedfc',
dialog: 'Tomatoes, sausages, nice crispy bacon',
movie: '5cd95395de30eff6ebccde5c',
character: '5cd99d4bde30eff6ebccfc7c',
id: '5cd96e05de30eff6ebccedfc'
},
{
_id: '5cd96e05de30eff6ebcce99c',
dialog: 'Sam, no!',
movie: '5cd95395de30eff6ebccde5d',
character: '5cd99d4bde30eff6ebccfc15',
id: '5cd96e05de30eff6ebcce99c'
},
{
_id: "5cd96e05de30eff6ebcce89a",
dialog: "DEATH!",
movie: "5cd95395de30eff6ebccde5d",
character: "5cdbe49b7ed9587226e794a0",
id: "5cd96e05de30eff6ebcce89a"
},
{
_id: "5cd96e05de30eff6ebcce8cd",
dialog: "You'll see. Oh yes, you will see.",
movie: "5cd95395de30eff6ebccde5d",
character: "5cd99d4bde30eff6ebccfe9e",
id: "5cd96e05de30eff6ebcce8cd"
},
{
_id: "5cd96e05de30eff6ebcced9d",
dialog: "What business does an Elf, a Man and a Dwarf have in the Riddermark? Speak quickly!",
movie: "5cd95395de30eff6ebccde5b",
character: "5cdbdecb6dc0baeae48cfa5a",
id: "5cd96e05de30eff6ebcced9d"
}
];

mockingoose(QuoteModel).toReturn(fakeQuotes.length, 'estimatedDocumentCount');
mockingoose(QuoteModel).toReturn(fakeQuotes, 'find');

const response = await request(app).get('/v2/quotes/random');

expect(response.statusCode).toEqual(HttpCode.OK);
expect(fakeQuotes).toContainEqual(response.body);
});

});
20 changes: 20 additions & 0 deletions backend/controllers/quote.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response, NextFunction } from 'express';

import { getOptions } from './../helpers/config';
import { QuoteModel } from '../models/quote.model';
import { HttpCode } from '../helpers/constants';

export const quoteController = {
getQuotes: async (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -40,5 +41,24 @@ export const quoteController = {
} catch (err) {
return next(err);
}
},

getRandomQuote: async (_req: Request, res: Response, next: NextFunction) => {
try {
const count = await QuoteModel.estimatedDocumentCount();

if (count === 0) {
return res.status(HttpCode.NOT_FOUND).json({ message: "No quotes found" });
}

const randomIndex = Math.floor(Math.random() * count);

const quotes = await QuoteModel.find();
const quote = quotes[randomIndex];

return res.json(quote);
} catch (error) {
return next(error);
}
}
};
15 changes: 11 additions & 4 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
"@types/express": "^4.17.21",
"@types/express-rate-limit": "^6.0.0",
"@types/helmet": "^4.0.0",
"@types/jest": "^29.5.11",
"@types/jest": "^29.5.14",
"@types/jsonwebtoken": "^9.0.5",
"@types/mocha": "^10.0.9",
"@types/mongoose": "^5.11.97",
"@types/mongoose-paginate": "^5.0.16",
"@types/nanoid": "^3.0.0",
Expand All @@ -68,4 +69,4 @@
"ts-node": "^10.9.1",
"typescript": "^5.3.2"
}
}
}
1 change: 1 addition & 0 deletions backend/routes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ router.route('/character/:id/quote').get([passportHelpers.authenticate, characte

router.route('/quote').get([passportHelpers.authenticate, quoteController.getQuotes]);
router.route('/quote/:id').get([passportHelpers.authenticate, quoteController.getQuote]);
router.route('/quotes/random').get([passportHelpers.authenticate, quoteController.getRandomQuote]);
router.route('*').get(async (req, res) => {
return res.status(HttpCode.NOT_FOUND).send(notFoundResponse);
});
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/pages/Documentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ const Documentation: React.FC = () => {
<td data-label="Response">Request one specific movie quote</td>
<td data-label="Token required">yes</td>
</tr>
<tr>
<td data-label="Endpoint">/quotes/random/</td>
<td data-label="Response">Request one random movie quote</td>
<td data-label="Token required">yes</td>
</tr>
<tr>
<td data-label="Endpoint">
<strong>/chapter</strong>
Expand Down

0 comments on commit a542476

Please sign in to comment.