collection added
This commit is contained in:
parent
2642c95082
commit
9224594a28
@ -5,22 +5,27 @@ const user = require('../models/user');
|
|||||||
|
|
||||||
exports.createCar = async (req, res) => {
|
exports.createCar = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// 1) Multer must have placed the file info in req.file
|
// 1) Determine imagePath:
|
||||||
if (!req.file) {
|
let imagePath;
|
||||||
return res.status(400).json({ message: "Image file is required" });
|
|
||||||
|
if (req.file) {
|
||||||
|
// Native flow (Android/iOS) → Multerput the file under /uploads/cars/<filename>
|
||||||
|
imagePath = `/uploads/cars/${req.file.filename}`;
|
||||||
|
} else if (req.body.image) {
|
||||||
|
// Web flow → Client sent a JSON { image: "<uri>" }
|
||||||
|
// We trust that URI (could be a data URL or blob URL) and save it directly
|
||||||
|
imagePath = req.body.image;
|
||||||
|
} else {
|
||||||
|
return res.status(400).json({ message: "Image file (or image URL) is required." });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) Validate text fields from req.body
|
// 2) Extract other fields (make, model, year, modifications)
|
||||||
const { make, model, year, modifications } = req.body;
|
const { make, model, year, modifications } = req.body;
|
||||||
if (!make || !model || !year || !modifications) {
|
if (!make || !model || !year || !modifications) {
|
||||||
return res.status(400).json({ message: "All fields are required" });
|
return res.status(400).json({ message: "All fields are required." });
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3) Build the image path
|
// 3) Create the Car document
|
||||||
// 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({
|
const newCar = await Car.create({
|
||||||
image: imagePath,
|
image: imagePath,
|
||||||
make: make.trim(),
|
make: make.trim(),
|
||||||
@ -29,32 +34,25 @@ exports.createCar = async (req, res) => {
|
|||||||
modifications: modifications.trim(),
|
modifications: modifications.trim(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// 5) Determine userId from req.user
|
// 4) Find the user ID 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;
|
const userId = req.user.userId || req.user.id || req.user._id;
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
return res
|
return res.status(400).json({ message: "User ID not found in token payload." });
|
||||||
.status(400)
|
|
||||||
.json({ message: "User ID not found in token payload." });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6) Find or create the user's Collection
|
// 5) Find or create the user’s Collection
|
||||||
let userCollection = await Collection.findOne({ user_id: userId });
|
let userCollection = await Collection.findOne({ user_id: userId });
|
||||||
|
|
||||||
if (!userCollection) {
|
if (!userCollection) {
|
||||||
// If no collection exists for this user, create it
|
|
||||||
userCollection = await Collection.create({
|
userCollection = await Collection.create({
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
cars: [newCar._id],
|
cars: [newCar._id],
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, append the new car's ObjectId
|
|
||||||
userCollection.cars.push(newCar._id);
|
userCollection.cars.push(newCar._id);
|
||||||
await userCollection.save();
|
await userCollection.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7) Respond with the newly created Car
|
// 6) Return success
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: "Car added successfully",
|
message: "Car added successfully",
|
||||||
car: newCar,
|
car: newCar,
|
||||||
|
|||||||
BIN
uploads/cars/1749193406704-GosYFbuawAAHOne.jpg
Normal file
BIN
uploads/cars/1749193406704-GosYFbuawAAHOne.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 175 KiB |
Loading…
x
Reference in New Issue
Block a user