collection added
This commit is contained in:
parent
f64e1f1f56
commit
2642c95082
4
app.js
4
app.js
@ -4,6 +4,8 @@ const cors = require('cors');
|
||||
const userRoutes = require('./src/routes/v1/userRoutes');
|
||||
const loginRoutes = require('./src/routes/v1/authRoutes');
|
||||
const carsRoutes = require('./src/routes/v1/carsRoutes');
|
||||
const collectionRoutes = require('./src/routes/v1/collectionRoutes');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
|
||||
@ -13,6 +15,7 @@ const port = process.env.PORT || 5000;
|
||||
const connectDB = require('./src/db/connectDB');
|
||||
connectDB();
|
||||
|
||||
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
|
||||
app.use(express.json());
|
||||
|
||||
app.use(cors());
|
||||
@ -24,6 +27,7 @@ app.get('/', (req, res) => {
|
||||
app.use('/api/v1/users', userRoutes);
|
||||
app.use('/api/v1/login', loginRoutes);
|
||||
app.use('/api/v1/cars', carsRoutes);
|
||||
app.use('/api/v1/collections', collectionRoutes);
|
||||
|
||||
app.use((req, res, next) => {
|
||||
res.status(404).json({ message: 'Route not found' });
|
||||
|
||||
119
package-lock.json
generated
119
package-lock.json
generated
@ -16,6 +16,7 @@
|
||||
"express": "^4.21.2",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"mongoose": "^8.10.0",
|
||||
"multer": "^2.0.1",
|
||||
"nodemon": "^3.1.9"
|
||||
}
|
||||
},
|
||||
@ -64,6 +65,11 @@
|
||||
"node": ">= 8"
|
||||
}
|
||||
},
|
||||
"node_modules/append-field": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz",
|
||||
"integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw=="
|
||||
},
|
||||
"node_modules/array-flatten": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||
@ -158,6 +164,22 @@
|
||||
"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==",
|
||||
"license": "BSD-3-Clause"
|
||||
},
|
||||
"node_modules/buffer-from": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz",
|
||||
"integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ=="
|
||||
},
|
||||
"node_modules/busboy": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz",
|
||||
"integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==",
|
||||
"dependencies": {
|
||||
"streamsearch": "^1.1.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.16.0"
|
||||
}
|
||||
},
|
||||
"node_modules/bytes": {
|
||||
"version": "3.1.2",
|
||||
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
|
||||
@ -221,6 +243,20 @@
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
|
||||
},
|
||||
"node_modules/concat-stream": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-2.0.0.tgz",
|
||||
"integrity": "sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==",
|
||||
"engines": [
|
||||
"node >= 6.0"
|
||||
],
|
||||
"dependencies": {
|
||||
"buffer-from": "^1.0.0",
|
||||
"inherits": "^2.0.3",
|
||||
"readable-stream": "^3.0.2",
|
||||
"typedarray": "^0.0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/content-disposition": {
|
||||
"version": "0.5.4",
|
||||
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
|
||||
@ -833,6 +869,25 @@
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/minimist": {
|
||||
"version": "1.2.8",
|
||||
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
|
||||
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/ljharb"
|
||||
}
|
||||
},
|
||||
"node_modules/mkdirp": {
|
||||
"version": "0.5.6",
|
||||
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz",
|
||||
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
|
||||
"dependencies": {
|
||||
"minimist": "^1.2.6"
|
||||
},
|
||||
"bin": {
|
||||
"mkdirp": "bin/cmd.js"
|
||||
}
|
||||
},
|
||||
"node_modules/mongodb": {
|
||||
"version": "6.13.0",
|
||||
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.13.0.tgz",
|
||||
@ -958,6 +1013,23 @@
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
|
||||
},
|
||||
"node_modules/multer": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/multer/-/multer-2.0.1.tgz",
|
||||
"integrity": "sha512-Ug8bXeTIUlxurg8xLTEskKShvcKDZALo1THEX5E41pYCD2sCVub5/kIRIGqWNoqV6szyLyQKV6mD4QUrWE5GCQ==",
|
||||
"dependencies": {
|
||||
"append-field": "^1.0.0",
|
||||
"busboy": "^1.6.0",
|
||||
"concat-stream": "^2.0.0",
|
||||
"mkdirp": "^0.5.6",
|
||||
"object-assign": "^4.1.1",
|
||||
"type-is": "^1.6.18",
|
||||
"xtend": "^4.0.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 10.16.0"
|
||||
}
|
||||
},
|
||||
"node_modules/negotiator": {
|
||||
"version": "0.6.3",
|
||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
|
||||
@ -1138,6 +1210,19 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/readable-stream": {
|
||||
"version": "3.6.2",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
|
||||
"integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==",
|
||||
"dependencies": {
|
||||
"inherits": "^2.0.3",
|
||||
"string_decoder": "^1.1.1",
|
||||
"util-deprecate": "^1.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/readdirp": {
|
||||
"version": "3.6.0",
|
||||
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",
|
||||
@ -1339,6 +1424,22 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/streamsearch": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz",
|
||||
"integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/string_decoder": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
|
||||
"integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
|
||||
"dependencies": {
|
||||
"safe-buffer": "~5.2.0"
|
||||
}
|
||||
},
|
||||
"node_modules/supports-color": {
|
||||
"version": "5.5.0",
|
||||
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
|
||||
@ -1400,6 +1501,11 @@
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/typedarray": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
|
||||
"integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA=="
|
||||
},
|
||||
"node_modules/undefsafe": {
|
||||
"version": "2.0.5",
|
||||
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz",
|
||||
@ -1413,6 +1519,11 @@
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/util-deprecate": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
|
||||
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
|
||||
},
|
||||
"node_modules/utils-merge": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
|
||||
@ -1448,6 +1559,14 @@
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/xtend": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
|
||||
"engines": {
|
||||
"node": ">=0.4"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
"express": "^4.21.2",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"mongoose": "^8.10.0",
|
||||
"multer": "^2.0.1",
|
||||
"nodemon": "^3.1.9"
|
||||
}
|
||||
}
|
||||
|
||||
122
src/controllers/collectionController.js
Normal file
122
src/controllers/collectionController.js
Normal file
@ -0,0 +1,122 @@
|
||||
const Car = require('../models/cars');
|
||||
const Collection = require('../models/collection');
|
||||
const path = require("path");
|
||||
const user = require('../models/user');
|
||||
|
||||
exports.createCar = async (req, res) => {
|
||||
try {
|
||||
// 1) Multer must have placed the file info in req.file
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ message: "Image file is required" });
|
||||
}
|
||||
|
||||
// 2) Validate text fields from req.body
|
||||
const { make, model, year, modifications } = req.body;
|
||||
if (!make || !model || !year || !modifications) {
|
||||
return res.status(400).json({ message: "All fields are required" });
|
||||
}
|
||||
|
||||
// 3) Build the image path
|
||||
// If you're serving /uploads statically in app.js, it should be "/uploads/..."
|
||||
const imagePath = `/uploads/cars/${req.file.filename}`;
|
||||
|
||||
// 4) Create the Car document
|
||||
const newCar = await Car.create({
|
||||
image: imagePath,
|
||||
make: make.trim(),
|
||||
model: model.trim(),
|
||||
year: Number(year),
|
||||
modifications: modifications.trim(),
|
||||
});
|
||||
|
||||
// 5) Determine userId from req.user
|
||||
// Your JWT payload probably has something like { userId: "...", ... }
|
||||
// or { id: "...", ... }. Check whichever you actually signed.
|
||||
const userId = req.user.userId || req.user.id || req.user._id;
|
||||
if (!userId) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "User ID not found in token payload." });
|
||||
}
|
||||
|
||||
// 6) Find or create the user's Collection
|
||||
let userCollection = await Collection.findOne({ user_id: userId });
|
||||
|
||||
if (!userCollection) {
|
||||
// If no collection exists for this user, create it
|
||||
userCollection = await Collection.create({
|
||||
user_id: userId,
|
||||
cars: [newCar._id],
|
||||
});
|
||||
} else {
|
||||
// Otherwise, append the new car's ObjectId
|
||||
userCollection.cars.push(newCar._id);
|
||||
await userCollection.save();
|
||||
}
|
||||
|
||||
// 7) Respond with the newly created Car
|
||||
return res.status(201).json({
|
||||
message: "Car added successfully",
|
||||
car: newCar,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error creating car:", error);
|
||||
return res.status(500).json({
|
||||
message: "Server error while adding car",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
exports.getCollection = async (req, res) => {
|
||||
try {
|
||||
// 1) Get userId from req.user
|
||||
const userId = req.user.userId || req.user.id || req.user._id;
|
||||
if (!userId) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "User ID not found in token payload." });
|
||||
}
|
||||
|
||||
// 2) Find the user's collection, populate car details
|
||||
const collection = await Collection.findOne({ user_id: userId })
|
||||
.populate("cars")
|
||||
.exec();
|
||||
|
||||
if (!collection) {
|
||||
return res.status(404).json({ message: "Collection not found" });
|
||||
}
|
||||
|
||||
const host = req.get("host");
|
||||
const protocol = req.protocol;
|
||||
|
||||
const carsWithFullUrl = collection.cars.map((car) => {
|
||||
// car.image is something like "/uploads/cars/<filename>"
|
||||
const fullImageUrl = `${protocol}://${host}${car.image}`;
|
||||
|
||||
return {
|
||||
_id: car._id,
|
||||
make: car.make,
|
||||
model: car.model,
|
||||
year: car.year,
|
||||
modifications: car.modifications,
|
||||
// return the fully qualified URL instead of the raw path
|
||||
image: fullImageUrl,
|
||||
createdAt: car.createdAt,
|
||||
updatedAt: car.updatedAt,
|
||||
};
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
_id: collection._id,
|
||||
user_id: collection.user_id,
|
||||
cars: carsWithFullUrl,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error retrieving collection:", error);
|
||||
return res.status(500).json({
|
||||
message: "Server error while retrieving collection",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -1,19 +1,35 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
module.exports = (req, res, next) => {
|
||||
console.log(req.headers);
|
||||
const authHeader = req.headers['authorization'];
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return res.status(401).json({ message: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const token = authHeader.split(' ')[1];
|
||||
|
||||
jwt.verify(token, process.env.JWT_SECRET || 'your_jwt_secret', (err, decoded) => {
|
||||
// 1) Check for Authorization: Bearer <token>
|
||||
const authHeader = req.headers["authorization"];
|
||||
if (authHeader && authHeader.startsWith("Bearer ")) {
|
||||
const token = authHeader.split(" ")[1];
|
||||
return jwt.verify(
|
||||
token,
|
||||
process.env.JWT_SECRET || "your_jwt_secret",
|
||||
(err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(403).json({ message: 'Invalid or expired token' });
|
||||
return res
|
||||
.status(403)
|
||||
.json({ message: "Invalid or expired token" });
|
||||
}
|
||||
req.user = decoded; // Attaching the decoded user data to the request object
|
||||
next();
|
||||
});
|
||||
|
||||
req.user = { userId: decoded.userId };
|
||||
return next();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// 2) Fallback: check x-user-id if no Bearer token
|
||||
const userId = req.headers["x-user-id"];
|
||||
if (userId) {
|
||||
req.user = { userId: userId };
|
||||
return next();
|
||||
}
|
||||
|
||||
// 3) Neither a valid Bearer token nor x-user-id was provided
|
||||
return res
|
||||
.status(401)
|
||||
.json({ message: "Unauthorized: no token or user ID provided." });
|
||||
};
|
||||
14
src/models/cars.js
Normal file
14
src/models/cars.js
Normal file
@ -0,0 +1,14 @@
|
||||
const { Schema, model } = require('mongoose');
|
||||
|
||||
const carsSchema = new Schema(
|
||||
{
|
||||
image: { type: String, required: true },
|
||||
make: { type: String, required: true },
|
||||
model: { type: String, required: true },
|
||||
year: { type: Number, required: true },
|
||||
modifications: { type: String, required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
module.exports = model('Cars', carsSchema, 'cars');
|
||||
21
src/models/collection.js
Normal file
21
src/models/collection.js
Normal file
@ -0,0 +1,21 @@
|
||||
const { Schema, model } = require('mongoose');
|
||||
|
||||
const collectionSchema = new Schema(
|
||||
{
|
||||
user_id: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'User',
|
||||
required: true
|
||||
},
|
||||
cars: [
|
||||
{
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'Cars',
|
||||
required: true
|
||||
}
|
||||
],
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
module.exports = model('Collection', collectionSchema, 'collections');
|
||||
39
src/routes/v1/collectionRoutes.js
Normal file
39
src/routes/v1/collectionRoutes.js
Normal file
@ -0,0 +1,39 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const {createCar, getCollection} = require('../../controllers/collectionController');
|
||||
const multer = require("multer");
|
||||
const path = require("path");
|
||||
const authMiddleware = require('../../middleware/authMiddleware');
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
// Make sure this folder exists on disk: ./uploads/cars
|
||||
cb(null, path.join(__dirname, "../../../uploads/cars"));
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
// Prepend a timestamp to avoid name collisions
|
||||
const uniqueName = `${Date.now()}-${file.originalname}`;
|
||||
cb(null, uniqueName);
|
||||
},
|
||||
});
|
||||
|
||||
const upload = multer({
|
||||
storage: storage,
|
||||
limits: {
|
||||
fileSize: 5 * 1024 * 1024, // max 5 MB
|
||||
},
|
||||
fileFilter: (req, file, cb) => {
|
||||
const ext = path.extname(file.originalname).toLowerCase();
|
||||
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
|
||||
return cb(new Error("Only JPG/JPEG/PNG images are allowed"), false);
|
||||
}
|
||||
cb(null, true);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
router.post('/addCar', authMiddleware, upload.single("image"), createCar);
|
||||
|
||||
router.get('/getCollection', authMiddleware, getCollection);
|
||||
|
||||
module.exports = router;
|
||||
BIN
uploads/cars/1749189239726-GcpCbUYaoAAkDZH.jpg
Normal file
BIN
uploads/cars/1749189239726-GcpCbUYaoAAkDZH.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 154 KiB |
BIN
uploads/cars/1749189654270-GcpCbUYaoAAkDZH.jpg
Normal file
BIN
uploads/cars/1749189654270-GcpCbUYaoAAkDZH.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 154 KiB |
Loading…
x
Reference in New Issue
Block a user