Skip to content
Permalink
Browse files

Refactor: controllers

- move the filter for quote length into a separate function.

- Instead of listing all the fields that should be included in the
  response, just exclude the ones that we don't want (`__v` and
  `authorId`)
  • Loading branch information
lukePeavey committed Feb 11, 2020
1 parent a1f1e47 commit 64b4c4f859a06ef1bcb77cf9973a633c9c300a33
@@ -24,9 +24,9 @@ Returns a single random quote from the database

| param | type | Description |
| :------- | :------- | :----------------------------------------------------------- |
| tags | `String` | Filter random quote by tag(s). Takes a list of one or more tag names, separated by a comma (meaning `AND`) or a pipe (meaning `OR`). A comma separated list will match quotes that have **_all_** of the given tags. While a pipe (`\|`) separated list will match quotes that have **_either_** of the provided tags. |
| maxLength | `Int` | The maximum Length in characters ( can be combined with `minLength` ) |
| minLength | `Int`| The minimum Length in characters ( can be combined with `maxLength` )
| tags | `String` | Filter random quote by tag(s). Takes a list of one or more tag names, separated by a comma (meaning `AND`) or a pipe (meaning `OR`). A comma separated list will match quotes that have **_all_** of the given tags. While a pipe (`\|`) separated list will match quotes that have **_either_** of the provided tags. |

#### Request

@@ -58,9 +58,9 @@ Get a paginated list of all quotations in the database. This method supports sev
| authorId | `String` | Filter quotes by author ID. |
| limit | `Int` | The number of quotes to return per request. (for pagination).|
| skip | `Int` | The number of items to skip (for pagination). |
| tags | `String` | Filter quotes by tag(s). Takes a list of one or more tag names, separated by a comma (meaning `AND`) or a pipe (meaning `OR`). A comma separated list will match quotes that have **_all_** of the given tags. While a pipe (`\|`) separated list will match quotes that have **_either_** of the provided tags. |
| maxLength | `Int` | The maximum Length in characters ( can be combined with `minLength` ) |
| minLength | `Int`| The minimum Length in characters ( can be combined with `maxLength` )
| tags | `String` | Filter quotes by tag(s). Takes a list of one or more tag names, separated by a comma (meaning `AND`) or a pipe (meaning `OR`). A comma separated list will match quotes that have **_all_** of the given tags. While a pipe (`\|`) separated list will match quotes that have **_either_** of the provided tags. |

#### Request

@@ -19,7 +19,7 @@ module.exports = async function getAuthorById(req, res, next) {
return next(createError(404, 'The requested resource could not be found'))
}
// Get quotes by this author
const quotes = await Quotes.find({ authorId: id }).select('content author')
const quotes = await Quotes.find({ authorId: id }).select('__v')

res.status(200).json({
_id: author._id,
@@ -41,7 +41,7 @@ module.exports = async function listAuthors(req, res, next) {
.sort({ [sortBy]: sortOrder })
.limit(limit)
.skip(skip)
.select('name quoteCount'),
.select('-__v'),
Authors.countDocuments(filter),
])

@@ -7,17 +7,13 @@ const Quotes = require('../../models/Quotes')
module.exports = async function getQuoteById(req, res, next) {
try {
const { id } = req.params
const quote = await Quotes.findById(id).select('content author tags')
const result = await Quotes.findById(id).select('-__v -authorId')

if (!quote) {
if (!result) {
return next(createError(404, 'The requested resource could not be found'))
}

res.status(200).json({
_id: quote._id,
content: quote.content,
author: quote.author,
})
res.status(200).json(result)
} catch (error) {
return next(error)
}
@@ -2,6 +2,7 @@ const clamp = require('lodash/clamp')
const escapeRegExp = require('lodash/escapeRegExp')
const Quotes = require('../../models/Quotes')
const getTagsFilter = require('../utils/getTagsFilter')
const getLengthFilter = require('../utils/getLengthFilter')

/**
* Get multiple quotes from the database.
@@ -15,26 +16,11 @@ const getTagsFilter = require('../utils/getTagsFilter')
*/
module.exports = async function listQuotes(req, res, next) {
try {
const {
author,
authorId,
tags,
minLength = 0,
maxLength = 99999999,
} = req.query
const { author, authorId, tags, minLength, maxLength } = req.query
let { limit, skip = 0 } = req.query

// Query filters
const filter = {
// set a filter on attribute "length"
length: {
// $gte (greater than or equal to) matches anything of value at or above specified
$gte: Number(minLength),

// $lte (less than or equal to) matches anything at or below specified value
$lte: Number(maxLength),
},
}
const filter = {}

if (author) {
// Search for quotes by author name (supports "fuzzy" search)
@@ -45,6 +31,10 @@ module.exports = async function listQuotes(req, res, next) {
filter.authorId = authorId
}

if (minLength || maxLength) {
filter.length = getLengthFilter(minLength, maxLength)
}

if (tags) {
filter.tags = getTagsFilter(tags)
}
@@ -62,7 +52,7 @@ module.exports = async function listQuotes(req, res, next) {
.sort({ [sortBy]: sortOrder })
.limit(limit)
.skip(skip)
.select('content author tags length'),
.select('-__v -authorId'),

Quotes.countDocuments(filter),
])
@@ -1,25 +1,20 @@
const createError = require('http-errors')
const Quotes = require('../../models/Quotes')
const getTagsFilter = require('../utils/getTagsFilter')
const getLengthFilter = require('../utils/getLengthFilter')

/**
* Get a single random quote
*/
module.exports = async function getRandomQuote(req, res, next) {
try {
// save our query parameters
const { minLength = 0, maxLength = 99999999, tags } = req.query
const { minLength, maxLength, tags } = req.query

// Query Filters
const filter = {
// set a filter on attribute "length"
length: {
// $gte (greater than or equal to) matches anything of value at or above specified
$gte: Number(minLength),
const filter = {}

// $lte (less than or equal to) matches anything at or below specified value
$lte: Number(maxLength),
},
if (minLength || maxLength) {
filter.length = getLengthFilter(minLength, maxLength)
}

if (tags) {
@@ -31,8 +26,7 @@ module.exports = async function getRandomQuote(req, res, next) {
{ $match: filter },
// Select a random document from the results
{ $sample: { size: 1 } },
// Only include the following the fields
{ $project: { _id: 1, content: 1, author: 1, length: 1, tags: 1 } },
{ $project: { __v: 0, authorId: 0 } },
])

if (!result) {
@@ -0,0 +1,13 @@
/**
* Takes `minLength` and `maxLength` and creates the query filter for quote
* length.
*
* @param {number} [minLength = 0]
* @param {number} [maxLength = 1e4]
*/
module.exports = function getLengthFilter(minLength, maxLength) {
return {
$gte: Number(minLength) || 0,
$lte: Number(maxLength) || 1e4,
}
}

0 comments on commit 64b4c4f

Please sign in to comment.
You can’t perform that action at this time.