|
| 1 | +/*jshint esversion: 8 */ |
| 2 | + |
| 3 | +const express = require('express'); |
| 4 | +const mongoose = require('mongoose'); |
| 5 | +const fs = require('fs'); |
| 6 | +const cors = require('cors'); |
| 7 | + |
| 8 | +const app = express(); |
| 9 | +const port = 3030; |
| 10 | + |
| 11 | +app.use(cors()); |
| 12 | +app.use(express.urlencoded({ extended: false })); |
| 13 | + |
| 14 | +const reviewsData = JSON.parse(fs.readFileSync('reviews.json', 'utf8')); |
| 15 | +const dealershipsData = JSON.parse(fs.readFileSync('dealerships.json', 'utf8')); |
| 16 | +const carsData = JSON.parse(fs.readFileSync('car_records.json', 'utf8')); |
| 17 | + |
| 18 | +mongoose.connect('mongodb://mongo_db:27017/', { dbName: 'dealershipsDB' }); |
| 19 | + |
| 20 | +const Reviews = require('./review'); |
| 21 | +const Dealerships = require('./dealership'); |
| 22 | +const Cars = require('./inventory'); |
| 23 | + |
| 24 | +try { |
| 25 | + Reviews.deleteMany({}).then(() => { |
| 26 | + Reviews.insertMany(reviewsData.reviews); |
| 27 | + }); |
| 28 | + Dealerships.deleteMany({}).then(() => { |
| 29 | + Dealerships.insertMany(dealershipsData.dealerships); |
| 30 | + }); |
| 31 | + Cars.deleteMany({}).then(() => { |
| 32 | + Cars.insertMany(carsData.cars); |
| 33 | + }); |
| 34 | +} catch (error) { |
| 35 | + console.error(error); |
| 36 | + // Handle errors properly here |
| 37 | +} |
| 38 | + |
| 39 | +app.get('/', async (req, res) => { |
| 40 | + res.send('Welcome to the Mongoose API'); |
| 41 | +}); |
| 42 | + |
| 43 | +app.get('/fetchReviews', async (req, res) => { |
| 44 | + try { |
| 45 | + const documents = await Reviews.find(); |
| 46 | + res.json(documents); |
| 47 | + } catch (error) { |
| 48 | + res.status(500).json({ error: 'Error fetching reviews' }); |
| 49 | + } |
| 50 | +}); |
| 51 | + |
| 52 | + |
| 53 | +app.get('/fetchDealers', async (req, res) => { |
| 54 | + try { |
| 55 | + const documents = await Dealerships.find(); |
| 56 | + res.json(documents); |
| 57 | + } catch (error) { |
| 58 | + res.status(500).json({ error: 'Error fetching dealerships' }); |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +app.get('/fetchDealers/:state', async (req, res) => { |
| 63 | + try { |
| 64 | + const documents = await Dealerships.find({ state: req.params.state }); |
| 65 | + res.json(documents); |
| 66 | + } catch (error) { |
| 67 | + console.log(error); |
| 68 | + res.status(500).json({ error: error }); |
| 69 | + } |
| 70 | +}); |
| 71 | + |
| 72 | +app.get('/fetchReviews/dealer/:id', async (req, res) => { |
| 73 | + try { |
| 74 | + const documents = await Reviews.find({ dealership: req.params.id }); |
| 75 | + res.json(documents); |
| 76 | + } catch (error) { |
| 77 | + res.status(500).json({ error: 'Error fetching reviews by dealer ID' }); |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +app.get('/fetchReviews/car/:carmake', async (req, res) => { |
| 82 | + try { |
| 83 | + const documents = await Reviews.find({ car_make: req.params.carmake }); |
| 84 | + res.json(documents); |
| 85 | + } catch (error) { |
| 86 | + res.status(500).json({ error: 'Error fetching reviews by car make' }); |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +app.get('/fetchReviews/car/:carmake/:model', async (req, res) => { |
| 91 | + try { |
| 92 | + const documents = await Reviews.find({ car_make: req.params.carmake, car_model: req.params.model }); |
| 93 | + res.json(documents); |
| 94 | + } catch (error) { |
| 95 | + res.status(500).json({ error: 'Error fetching reviews by car make and model' }); |
| 96 | + } |
| 97 | +}); |
| 98 | + |
| 99 | +app.get('/fetchDealer/:id', async (req, res) => { |
| 100 | + try { |
| 101 | + const documents = await Dealerships.find({ id: req.params.id }); |
| 102 | + res.json(documents); |
| 103 | + } catch (error) { |
| 104 | + res.status(500).json({ error: 'Error fetching dealers by ID' }); |
| 105 | + } |
| 106 | +}); |
| 107 | + |
| 108 | +app.post('/insert_review', express.raw({ type: '*/*' }), async (req, res) => { |
| 109 | + data = JSON.parse(req.body); |
| 110 | + const documents = await Reviews.find().sort({ id: -1 }); |
| 111 | + let new_id = documents[0].id + 1; |
| 112 | + |
| 113 | + const review = new Reviews({ |
| 114 | + id: new_id, |
| 115 | + name: data.name, |
| 116 | + dealership: data.dealership, |
| 117 | + review: data.review, |
| 118 | + purchase: data.purchase, |
| 119 | + purchase_date: data.purchase_date, |
| 120 | + car_make: data.car_make, |
| 121 | + car_model: data.car_model, |
| 122 | + car_year: data.car_year, |
| 123 | + }); |
| 124 | + |
| 125 | + try { |
| 126 | + const savedReview = await review.save(); |
| 127 | + res.json(savedReview); |
| 128 | + } catch (error) { |
| 129 | + console.log(error); |
| 130 | + res.status(500).json({ error: 'Error inserting review' }); |
| 131 | + } |
| 132 | +}); |
| 133 | + |
| 134 | +app.get('/cars/:id', async (req, res) => { |
| 135 | + try { |
| 136 | + const documents = await Cars.find({dealer_id: req.params.id}); |
| 137 | + res.json(documents); |
| 138 | + } catch (error) { |
| 139 | + res.status(500).json({ error: 'Error fetching reviews' }); |
| 140 | + } |
| 141 | +}); |
| 142 | + |
| 143 | +app.get('/carsbymake/:id/:make', async (req, res) => { |
| 144 | + try { |
| 145 | + const documents = await Cars.find({dealer_id: req.params.id, make: req.params.make}); |
| 146 | + res.json(documents); |
| 147 | + } catch (error) { |
| 148 | + res.status(500).json({ error: 'Error fetching reviews by car make and model' }); |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +app.get('/carsbymodel/:id/:model', async (req, res) => { |
| 153 | + try { |
| 154 | + const documents = await Cars.find({ dealer_id: req.params.id, model: req.params.model }); |
| 155 | + res.json(documents); |
| 156 | + } catch (error) { |
| 157 | + res.status(500).json({ error: 'Error fetching dealers by ID' }); |
| 158 | + } |
| 159 | +}); |
| 160 | + |
| 161 | +app.get('/carsbymaxmileage/:id/:mileage', async (req, res) => { |
| 162 | + try { |
| 163 | + const documents = await Cars.find({ dealer_id: req.params.id, mileage : { $lte : req.params.mileage} }); |
| 164 | + res.json(documents); |
| 165 | + } catch (error) { |
| 166 | + res.status(500).json({ error: 'Error fetching dealers by ID' }); |
| 167 | + } |
| 168 | +}); |
| 169 | + |
| 170 | +app.get('/carsbyyear/:id/:year', async (req, res) => { |
| 171 | + try { |
| 172 | + const documents = await Cars.find({ dealer_id: req.params.id, year : { $gte :req.params.year }}); |
| 173 | + res.json(documents); |
| 174 | + } catch (error) { |
| 175 | + res.status(500).json({ error: 'Error fetching dealers by ID' }); |
| 176 | + } |
| 177 | +}); |
| 178 | + |
| 179 | +app.listen(port, () => { |
| 180 | + console.log(`Server is running on http://localhost:${port}`); |
| 181 | +}); |
0 commit comments