Skip to content
Merged
55 changes: 31 additions & 24 deletions backend/src/controllers/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import mongoose from "mongoose";
import { bucket } from "src/config/firebase"; // Import Firebase bucket
import { getStorage, ref, getDownloadURL } from "firebase/storage";
import { v4 as uuidv4 } from "uuid"; // For unique filenames
import { initializeApp } from "firebase/app";
import { getApp, getApps, initializeApp } from "firebase/app";
import { firebaseConfig } from "src/config/firebaseConfig";
import multer from "multer";

Expand Down Expand Up @@ -69,30 +69,30 @@ export const addProduct = [
upload,
async (req: AuthenticatedRequest, res: Response) => {
try {
const { name, price, description } = req.body;
const { name, price, description, year, category, condition} = req.body;
if (!req.user) return res.status(404).json({ message: "User not found" });
const userId = req.user._id;
const userEmail = req.user.userEmail;
if (!name || !price || !userEmail) {
return res.status(400).json({ message: "Name, price, and userEmail are required." });
if (!name || !price || !userEmail || !year || !category || !condition) {
return res.status(400).json({ message: "Name, price, userEmail, year, category, condition, are required." });
}

const images: string[] = [];
if (req.files && Array.isArray(req.files)) {
const app = initializeApp(firebaseConfig);
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
const storage = getStorage(app);

for (const file of req.files as Express.Multer.File[]) {
const fileName = `${uuidv4()}-${file.originalname}`;
const firebaseFile = bucket.file(fileName);

await firebaseFile.save(file.buffer, {
metadata: { contentType: file.mimetype },
});

const imageUrl = await getDownloadURL(ref(storage, fileName));
images.push(imageUrl);
}
const urls = await Promise.all(
(req.files as Express.Multer.File[]).map(async (file) => {
const fileName = `${uuidv4()}-${file.originalname}`;
const firebaseFile = bucket.file(fileName);
await firebaseFile.save(file.buffer, {
metadata: { contentType: file.mimetype },
});
return await getDownloadURL(ref(storage, fileName));
}),
);
images.push(...urls);
}

const newProduct = new ProductModel({
Expand All @@ -101,6 +101,9 @@ export const addProduct = [
description,
userEmail,
images,
year,
category,
condition,
timeCreated: new Date(),
timeUpdated: new Date(),
});
Expand Down Expand Up @@ -171,15 +174,16 @@ export const updateProductById = [
let existing = req.body.existingImages || [];
if (!Array.isArray(existing)) existing = [existing];

const newUrls: string[] = [];
const app = initializeApp(firebaseConfig);
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
const storage = getStorage(app);
for (const file of req.files as Express.Multer.File[]) {
const name = `${uuidv4()}-${file.originalname}`;
const bucketFile = bucket.file(name);
await bucketFile.save(file.buffer, { metadata: { contentType: file.mimetype } });
newUrls.push(await getDownloadURL(ref(storage, name)));
}
const newUrls = await Promise.all(
(req.files as Express.Multer.File[]).map(async (file) => {
const name = `${uuidv4()}-${file.originalname}`;
const bucketFile = bucket.file(name);
await bucketFile.save(file.buffer, { metadata: { contentType: file.mimetype } });
return await getDownloadURL(ref(storage, name));
}),
);

const finalImages = [...existing, ...newUrls];

Expand All @@ -189,6 +193,9 @@ export const updateProductById = [
name: req.body.name,
price: req.body.price,
description: req.body.description,
year: req.body.year,
category: req.body.category,
condition: req.body.condition,
images: finalImages,
timeUpdated: new Date(),
},
Expand Down
12 changes: 12 additions & 0 deletions backend/src/models/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const productSchema = new Schema({
description: {
type: String,
},
year: {
type: Number,
required: true,
},
category: {
type: String,
required: true,
},
condition: {
type: String,
required: true,
},
timeCreated: {
type: Date,
required: true,
Expand Down
Binary file added frontend/public/bg-light-white-trident.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions frontend/src/components/Product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface Props {
productImages: string[];
productName: string;
productPrice: number;
productYear: number;
productCategory: string;
productCondition: string;
productLocation: string;
isSaved?: boolean;
onSaveToggle?: (productId: string, newSavedStatus: boolean) => void;
}
Expand Down
Loading