Skip to content

Commit b99da9c

Browse files
committed
content routes added, postman test ok
1 parent 67fa078 commit b99da9c

13 files changed

+299
-7
lines changed

app.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
require("dotenv").config()
2-
// require("./db") // waiting for database
2+
require("./db") // waiting for database
33

44
const express = require("express")
55
const app = express()
66

7-
// require("./config")(app) // waiting for config
7+
require("./config")(app) // waiting for config
88

99
const indexRoutes = require("./routes/index.routes")
1010
app.use("/api", indexRoutes)

config/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// COPY & PASTE FROM CLASS EXAMPLE !!!
2+
3+
// We reuse this import in order to have access to the `body` property in requests
4+
const express = require("express");
5+
6+
// ℹ️ Responsible for the messages you see in the terminal as requests are coming in
7+
// https://www.npmjs.com/package/morgan
8+
const logger = require("morgan");
9+
10+
// ℹ️ Needed when we deal with cookies (we will when dealing with authentication)
11+
// https://www.npmjs.com/package/cookie-parser
12+
const cookieParser = require("cookie-parser");
13+
14+
// ℹ️ Needed to accept from requests from 'the outside'. CORS stands for cross origin resource sharing
15+
// unless the request if from the same domain, by default express wont accept POST requests
16+
const cors = require("cors");
17+
18+
const FRONTEND_URL = process.env.ORIGIN || "http://localhost:5173";
19+
20+
// Middleware configuration
21+
module.exports = (app) => {
22+
// Because this is a server that will accept requests from outside and it will be hosted ona server with a `proxy`, express needs to know that it should trust that setting.
23+
// Services like heroku use something called a proxy and you need to add this to your server
24+
app.set("trust proxy", 1);
25+
26+
// controls a very specific header to pass headers from the frontend
27+
app.use(
28+
cors({
29+
origin: [FRONTEND_URL]
30+
})
31+
);
32+
33+
// In development environment the app logs
34+
app.use(logger("dev"));
35+
36+
// To have access to `body` property in the request
37+
app.use(express.json());
38+
app.use(express.urlencoded({ extended: false }));
39+
app.use(cookieParser());
40+
};

db/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const mongoose = require("mongoose")
2+
3+
// mongodb will create a db with this name if it doesn't exist when running the app...
4+
const MONGO_URI = process.env.MONGO_URI || "mongodb://127.0.0.1:27017/rec-up-db"
5+
6+
mongoose
7+
.connect(MONGO_URI)
8+
.then((x) => {
9+
const dbName = x.connections[0].name
10+
console.log(`connected to mongo, yay, db name: ${dbName}`)
11+
})
12+
.catch((err) => {
13+
console.lerror("error connecting to mongo: ")
14+
})

models/content.model.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const mongoose = require("mongoose");
2+
3+
const contentSchema = new mongoose.Schema({
4+
category: {
5+
type: String,
6+
enum: [
7+
"book",
8+
"comic",
9+
"song",
10+
"film",
11+
"series / tv show",
12+
"videogame",
13+
"podcast"
14+
// "gig / concert", // add coma(,) above if used
15+
// "theater",
16+
// "exhibition",
17+
],
18+
required: true,
19+
},
20+
title: { type: String, required: true },
21+
author: { type: String, required: true },
22+
keywords: [String],
23+
mediaUrl: String
24+
// totalRecommendations to be added -- virtual ?
25+
})
26+
27+
const Content = mongoose.model("Content", contentSchema)
28+
29+
module.exports = Content

models/recommendation.model.js

Whitespace-only changes.

models/user.model.js

Whitespace-only changes.

package-lock.json

+94-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
"license": "ISC",
1313
"dependencies": {
1414
"bcryptjs": "^2.4.3",
15+
"cookie-parser": "^1.4.7",
16+
"cors": "^2.8.5",
1517
"dotenv": "^16.4.5",
1618
"express": "^4.21.1",
1719
"jsonwebtoken": "^9.0.2",
18-
"mongoose": "^8.7.1"
20+
"mongoose": "^8.7.1",
21+
"morgan": "^1.10.0"
1922
},
2023
"devDependencies": {
2124
"nodemon": "^3.1.7"

routes/auth.routes.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// const router = require("express").Router()
2+
3+
4+
5+
// module.exports = router

routes/content.routes.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const router = require("express").Router()
2+
const Content = require("../models/content.model")
3+
4+
5+
// | POST | `/api/contents` | Create new content (e.g., books, movies) |
6+
router.post("/", async (req, res, next) => {
7+
try {
8+
const newContent = await Content.create({
9+
category: req.body.category,
10+
title: req.body.title,
11+
author: req.body.author,
12+
keywords: req.body.keywords,
13+
mediaUrl: req.body.mediaUrl
14+
})
15+
res.status(201).json(newContent)
16+
console.log(newContent)
17+
} catch (error) {
18+
next(error)
19+
// // check this syntax after error-handling setup !!!
20+
// next(new Error(`error creating new content: ${error}`))
21+
}
22+
})
23+
24+
25+
// | GET | `/api/contents` | Read all content |
26+
router.get("/", async (req, res, next) => {
27+
try {
28+
const allContents = await Content.find()
29+
res.status(200).json(allContents)
30+
} catch (error) {
31+
next(error)
32+
}
33+
})
34+
35+
36+
// | GET | `/api/contents/:contentId` | Read specific content |
37+
router.get("/:contentId", async (req, res, next) => {
38+
try {
39+
const specificContent = await Content.findById(req.params.contentId)
40+
res.status(200).json(specificContent)
41+
} catch (error) {
42+
next(error)
43+
}
44+
})
45+
46+
47+
// | PUT | `/api/contents/:contentId` | Update specific content |
48+
router.put("/:contentId", async (req, res, next) => {
49+
try {
50+
const updatedContent = Content.findByIdAndUpdate(
51+
req.params.contentId,
52+
{
53+
category: req.body.category,
54+
title: req.body.title,
55+
author: req.body.author,
56+
keywords: req.body.keywords,
57+
mediaUrl: req.body.mediaUrl
58+
// add more fields if you change the model !!!
59+
},
60+
{ new: true}
61+
)
62+
} catch (error) {
63+
next(error)
64+
}
65+
})
66+
67+
68+
69+
// | DELETE | `/api/contents/:contentId` | Delete specific content
70+
router.delete("/:contentId", async (req, res, next) => {
71+
try {
72+
await Content.findByIdAndDelete(req.params.contentId)
73+
res.sendStatus(204)
74+
} catch (error) {
75+
next(error)
76+
}
77+
})
78+
79+
80+
81+
module.exports = router

routes/index.routes.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
const router = require("express").Router()
2+
// // the line above is the same that the following 2...
3+
// const express = require('express');
4+
// const router = express.Router()
5+
6+
// const authRoutes = require("./auth.routes")
7+
const contentRoutes = require("./content.routes")
8+
// const recommendationRoutes = require("./recommendation.routes")
9+
// const userRoutes = require("./user.routes")
10+
11+
// // plural -> convention !!!
12+
// router.use("/auth", authRoutes)
13+
router.use("/contents", contentRoutes)
14+
// router.use("/recommendations", recommendationRoutes)
15+
// router.use("/users", userRoutes)
16+
17+
18+
// // TEST
19+
// router.get("/", (req, res, next) => {
20+
// res.json(`hey there this is still rec-up-back-end`)
21+
// })
222

3-
router.get("/", (req, res, next) => {
4-
res.json(`hey there this is still rec-up-back-end`)
5-
})
623

724
module.exports = router

routes/recommendation.routes.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// const router = require("express").Router()
2+
3+
4+
5+
// module.exports = router

routes/user.routes.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// const router = require("express").Router()
2+
3+
4+
5+
// module.exports = router

0 commit comments

Comments
 (0)