diff --git a/.gitignore b/.gitignore index 6704566..2ea5e5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Logs +/.idea logs *.log npm-debug.log* @@ -69,7 +70,7 @@ typings/ .yarn-integrity # dotenv environment variables file -.env +backend/.env .env.test # parcel-bundler cache (https://parceljs.org/) diff --git a/backend/config/endpoints.js b/backend/config/endpoints.js index ede4e35..10f3ee8 100644 --- a/backend/config/endpoints.js +++ b/backend/config/endpoints.js @@ -58,6 +58,18 @@ const endpoints = [ name: "Single Location", path: "/location/", }, + { + type: "GET", + name: "All Quotes", + path: "/quote", + queryParams: [ + { + optional: true, + name: "Sort By", + values: ["asc", "desc"], + }, + ], + }, ] module.exports = endpoints.map((endpoint) => { diff --git a/backend/controllers/quote.api.js b/backend/controllers/quote.api.js new file mode 100644 index 0000000..ff8e8e2 --- /dev/null +++ b/backend/controllers/quote.api.js @@ -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 } diff --git a/backend/models/quote.model.js b/backend/models/quote.model.js new file mode 100644 index 0000000..5e990ff --- /dev/null +++ b/backend/models/quote.model.js @@ -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 diff --git a/backend/routes/routes.js b/backend/routes/routes.js index be1b293..4008fa3 100644 --- a/backend/routes/routes.js +++ b/backend/routes/routes.js @@ -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) @@ -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) => {