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

Feat(#143 & #142) quote endpoint 1 #144

Merged
merged 3 commits into from
Nov 18, 2020
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Logs
/.idea
logs
*.log
npm-debug.log*
Expand Down Expand Up @@ -69,7 +70,7 @@ typings/
.yarn-integrity

# dotenv environment variables file
.env
backend/.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
Expand Down
12 changes: 12 additions & 0 deletions backend/config/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ const endpoints = [
name: "Single Location",
path: "/location/<id>",
},
{
type: "GET",
name: "All Quotes",
path: "/quote",
queryParams: [
{
optional: true,
name: "Sort By",
values: ["asc", "desc"],
},
],
},
]

module.exports = endpoints.map((endpoint) => {
Expand Down
17 changes: 17 additions & 0 deletions backend/controllers/quote.api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Quote = require("../models/quote.model")
const options = require("../helpers/options")

const getAllQuotes = async (req, res) => {
await Quote.find({}, null, options(req.query), (err, quotes) => {
if (err) {
return res.status(500).json({
success: false,
message: "Something went wrong.",
})
}
res.set("Cache-Control", "public, max-age=315576")
return res.json(Quote.structure(quotes))
})
}

module.exports = { getAllQuotes }
48 changes: 48 additions & 0 deletions backend/models/quote.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const mongoose = require("mongoose")

const quoteSchema = new mongoose.Schema(
{
id: {
type: Number,
unique: true,
},
quote: {
type: String,
default: "",
},
by: {
type: String,
default: "",
},
character: {
type: String,
default: "",
},
image: {
type: String,
default: "",
},
},
{
collection: "quotes",
},
{
timestamps: true,
},
)

quoteSchema.statics.structure = (res) => {
const sortSchema = ({ id, quote, by, character, image }) => ({
id,
quote,
by,
character,
image,
})

return Array.isArray(res) ? res.map(sortSchema) : sortSchema(res)
}

const Quote = mongoose.model("quotes", quoteSchema)

module.exports = Quote
4 changes: 4 additions & 0 deletions backend/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const router = express.Router()
const characterController = require("../controllers/character.api.js")
const episodeController = require("../controllers/episode.api.js")
const locationController = require("../controllers/location.api.js")
const quoteController = require("../controllers/quote.api")
const apiController = require("../controllers/api.js")

const { getAllCharacters, getSingleCharacter } = characterController
const { getAllEpisodes, getSingleEpisode } = episodeController
const { getAllLocations, getSingleLocation } = locationController
const { getAllQuotes } = quoteController

router.route("/character").get(getAllCharacters)
router.route("/character/:id").get(getSingleCharacter)
Expand All @@ -18,6 +20,8 @@ router.route("/episode/:id").get(getSingleEpisode)
router.route("/location").get(getAllLocations)
router.route("/location/:id").get(getSingleLocation)

router.route("/quote").get(getAllQuotes)

router.route("/").get(apiController.getAPI)

router.route("*").get((req, res) => {
Expand Down