menambahkan fitur unutk barang saya
This commit is contained in:
+49
-1
@@ -271,7 +271,7 @@ app.get("/api/auth/me", auth, async (req, res, next) => {
|
||||
app.get("/api/products", async (req, res, next) => {
|
||||
try {
|
||||
const [rows] = await pool.query(
|
||||
"SELECT id, user_id, title, category, price, `condition`, location, seller, whatsapp, image, description, status, created_at FROM products ORDER BY created_at DESC, id DESC"
|
||||
"SELECT id, user_id, title, category, price, `condition`, location, seller, whatsapp, image, description, status, created_at FROM products WHERE status IS NULL OR status = 'Tersedia' ORDER BY created_at DESC, id DESC"
|
||||
)
|
||||
res.json({ products: await attachProductImages(rows) })
|
||||
} catch (error) {
|
||||
@@ -365,6 +365,54 @@ app.patch("/api/products/:id/status", auth, async (req, res, next) => {
|
||||
}
|
||||
})
|
||||
|
||||
app.put(
|
||||
"/api/products/:id",
|
||||
auth,
|
||||
upload.fields([
|
||||
{ name: "images", maxCount: maxImageCount },
|
||||
{ name: "image", maxCount: maxImageCount },
|
||||
{ name: "photos", maxCount: maxImageCount }
|
||||
]),
|
||||
async (req, res, next) => {
|
||||
try {
|
||||
const product = await findProductById(req.params.id)
|
||||
if (!product || product.user_id !== req.user.id) {
|
||||
res.status(404).json({ message: "Produk milik kamu tidak ditemukan." })
|
||||
return
|
||||
}
|
||||
|
||||
const { title, category, price, condition, location, seller, whatsapp, description } = req.body
|
||||
|
||||
if (!title || !category || !price || !condition || !location || !seller || !whatsapp || !description) {
|
||||
res.status(400).json({ message: "Semua field produk wajib diisi." })
|
||||
return
|
||||
}
|
||||
|
||||
await pool.query(
|
||||
"UPDATE products SET title = ?, category = ?, price = ?, `condition` = ?, location = ?, seller = ?, whatsapp = ?, description = ? WHERE id = ? AND user_id = ?",
|
||||
[title, category, Number(price), condition, location, seller, whatsapp, description, req.params.id, req.user.id]
|
||||
)
|
||||
|
||||
const uploadedImages = getUploadedImages(req)
|
||||
if (uploadedImages.length) {
|
||||
await deleteUploadedImages(product)
|
||||
await pool.query("DELETE FROM product_images WHERE product_id = ?", [req.params.id])
|
||||
|
||||
const mainImagePath = `/uploads/${uploadedImages[0].filename}`
|
||||
await pool.query("UPDATE products SET image = ? WHERE id = ?", [mainImagePath, req.params.id])
|
||||
|
||||
const imageRows = uploadedImages.map((file, index) => [req.params.id, `/uploads/${file.filename}`, index])
|
||||
await pool.query("INSERT INTO product_images (product_id, image, sort_order) VALUES ?", [imageRows])
|
||||
}
|
||||
|
||||
const updatedProduct = await findProductById(req.params.id)
|
||||
res.json({ product: updatedProduct })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
app.delete("/api/products/:id", auth, async (req, res, next) => {
|
||||
try {
|
||||
const product = await findProductById(req.params.id)
|
||||
|
||||
Reference in New Issue
Block a user