import express from "express"; const router = express.Router(); import { Pool } from "pg"; const pool = new Pool({ user: process.env.DB_USER, host: process.env.DB_HOST, database: process.env.DB_NAME, password: process.env.DB_PASSWORD_D, port: process.env.DB_PORT, }); // Get all listings router.get("/", async (req, res) => { const speciesId = req.query.species_id; const breedId = req.query.breed_id; const state = req.query.state; const district = req.query.district; const minPrice = req.query.min_price; const listingType = req.query.listing_type; let baseQuery = "SELECT * FROM listings WHERE 1=1"; const queryParams = []; let paramIndex = 1; if (speciesId) { baseQuery += ` AND species_id = $${paramIndex}`; queryParams.push(speciesId); paramIndex++; } if (breedId) { baseQuery += ` AND breed_id = $${paramIndex}`; queryParams.push(breedId); paramIndex++; } if (state) { baseQuery += ` AND state = $${paramIndex}`; queryParams.push(state); paramIndex++; } if (district) { baseQuery += ` AND district = $${paramIndex}`; queryParams.push(district); paramIndex++; } if (minPrice) { baseQuery += ` AND price >= $${paramIndex}`; queryParams.push(minPrice); paramIndex++; } if (listingType) { baseQuery += ` AND listing_type = $${paramIndex}`; queryParams.push(listingType); paramIndex++; } try { const listingsResult = await pool.query(baseQuery, queryParams); res.status(200).json(listingsResult.rows); } catch (error) { res.status(500).json({ error: "Internal Server Error in fetching listings", }); } }); // Get listing by ID router.get("/:id", async (req, res) => { const listingId = req.params.id; try { const listingResult = await pool.query( "SELECT * FROM listings WHERE id = $1", [listingId] ); if (listingResult.rows.length === 0) { return res.status(404).json({ error: "Listing not found" }); } res.status(200).json(listingResult.rows[0]); } catch (error) { res.status(500).json({ error: `Internal Server Error in fetching the specified ${id} listing`, }); } }); // Update listing by ID router.put("/:id", async (req, res) => { const listingId = req.params.id; const { title, description, price } = req.body; try { const updateResult = await pool.query( "UPDATE listings SET title = $1, description = $2, price = $3 WHERE id = $5 RETURNING *", [title, description, price, listingId] ); if (updateResult.rows.length === 0) { return res .status(404) .json({ error: "Listing not found for update" }); } res.status(200).json(updateResult.rows[0]); } catch (error) { res.status(500).json({ error: `Internal Server Error in updating the specified ${id} listing`, }); } }); // Submit a new listing router.post("/", async (req, res) => { const client = await pool.connect(); try { await client.query("BEGIN"); const { seller_id, title, price, currency, is_negotiable, listing_type, animal, } = req.body; // Add a new location if provided if (!animal.location_id && req.body.new_location) { animal.location_id = addNewLocation(req.body.new_location); } const animalId = await addAnimalToListing(animal); const listingInsertQuery = "INSERT INTO listings (seller_id, animal_id, title, price, currency, is_negotiable, listing_type, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), NOW()) RETURNING *"; const listingValues = [ seller_id, animalId, title, price, currency, is_negotiable, listing_type, ]; const listingResult = await client.query( listingInsertQuery, listingValues ); await client.query("COMMIT"); res.status(201).json(listingResult.rows[0]); } catch (error) { await client.query("ROLLBACK"); res.status(500).json({ error: "Internal Server Error in creating new listing", }); } finally { client.release(); } }); const addAnimalToListing = async (animal) => { try { const animalInsertQuery = "INSERT INTO animals (species_id, breed_id, sex, age_months, weight_kg, color_markings, quantity, purpose, health_status, vaccinated, dewormed, previous_pregnancies_count, pregnancy_status, milk_yield_litre_per_day, ear_tag_no, description, suggested_care, location_id, created_from, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, NOW(), NOW()) RETURNING id"; const animalValues = [ animal.species_id, animal.breed_id, animal.sex, animal.age_months, animal.weight_kg, animal.color_markings, animal.quantity, animal.purpose, animal.health_status, animal.vaccinated, animal.dewormed, animal.previous_pregnancies_count, animal.pregnancy_status, animal.milk_yield_litre_per_day, animal.ear_tag_no, animal.description, animal.suggested_care, animal.location_id, ]; const animalResult = await client.query( animalInsertQuery, animalValues ); const animalId = animalResult.rows[0].id; return animalId; } catch (error) { throw new Error("Error adding animal to listing: " + error.message); } }; const addNewLocation = async (location) => { try { const locationInsertQuery = "INSERT INTO locations (user_id, is_saved_address, location_type, country, state, district, city_village, pincode, lat, lng, source_type, source_confidence, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, NOW(), NOW()) RETURNING id"; const locationValues = [ location.user_id, location.is_saved_address, location.location_type, location.country, location.state, location.district, location.city_village, location.pincode, location.lat, location.lng, location.source_type, location.source_confidence, ]; const locationResult = await client.query( locationInsertQuery, locationValues ); return locationResult.rows[0].id; } catch (error) { throw new Error("Error adding new location: " + error.message); } }; export default router;