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) => {
|
||||
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" });
|
||||
// 1) Determine imagePath:
|
||||
let imagePath;
|
||||
|
||||
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;
|
||||
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
|
||||
// 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
|
||||
// 3) Create the Car document
|
||||
const newCar = await Car.create({
|
||||
image: imagePath,
|
||||
make: make.trim(),
|
||||
@ -29,32 +34,25 @@ exports.createCar = async (req, res) => {
|
||||
modifications: modifications.trim(),
|
||||
});
|
||||
|
||||
// 5) Determine userId from req.user
|
||||
// Your JWT payload probably has something like { userId: "...", ... }
|
||||
// or { id: "...", ... }. Check whichever you actually signed.
|
||||
// 4) Find the user ID 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." });
|
||||
return res.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 });
|
||||
|
||||
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
|
||||
// 6) Return success
|
||||
return res.status(201).json({
|
||||
message: "Car added successfully",
|
||||
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