From d1f2c5f2893584ef6c8ebd24b2c17d9581130a62 Mon Sep 17 00:00:00 2001 From: lavskillup <73978832+lavskillup@users.noreply.github.com> Date: Thu, 18 Jan 2024 20:11:16 +1100 Subject: [PATCH] Delete server/database/app.js.new --- server/database/app.js.new | 181 ------------------------------------- 1 file changed, 181 deletions(-) delete mode 100644 server/database/app.js.new diff --git a/server/database/app.js.new b/server/database/app.js.new deleted file mode 100644 index 432e9071c4..0000000000 --- a/server/database/app.js.new +++ /dev/null @@ -1,181 +0,0 @@ -/*jshint esversion: 8 */ - -const express = require('express'); -const mongoose = require('mongoose'); -const fs = require('fs'); -const cors = require('cors'); - -const app = express(); -const port = 3030; - -app.use(cors()); -app.use(express.urlencoded({ extended: false })); - -const reviewsData = JSON.parse(fs.readFileSync('reviews.json', 'utf8')); -const dealershipsData = JSON.parse(fs.readFileSync('dealerships.json', 'utf8')); -const carsData = JSON.parse(fs.readFileSync('car_records.json', 'utf8')); - -mongoose.connect('mongodb://mongo_db:27017/', { dbName: 'dealershipsDB' }); - -const Reviews = require('./review'); -const Dealerships = require('./dealership'); -const Cars = require('./inventory'); - -try { - Reviews.deleteMany({}).then(() => { - Reviews.insertMany(reviewsData.reviews); - }); - Dealerships.deleteMany({}).then(() => { - Dealerships.insertMany(dealershipsData.dealerships); - }); - Cars.deleteMany({}).then(() => { - Cars.insertMany(carsData.cars); - }); -} catch (error) { - console.error(error); - // Handle errors properly here -} - -app.get('/', async (req, res) => { - res.send('Welcome to the Mongoose API'); -}); - -app.get('/fetchReviews', async (req, res) => { - try { - const documents = await Reviews.find(); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching reviews' }); - } -}); - - -app.get('/fetchDealers', async (req, res) => { - try { - const documents = await Dealerships.find(); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealerships' }); - } -}); - -app.get('/fetchDealers/:state', async (req, res) => { - try { - const documents = await Dealerships.find({ state: req.params.state }); - res.json(documents); - } catch (error) { - console.log(error); - res.status(500).json({ error: error }); - } -}); - -app.get('/fetchReviews/dealer/:id', async (req, res) => { - try { - const documents = await Reviews.find({ dealership: req.params.id }); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching reviews by dealer ID' }); - } -}); - -app.get('/fetchReviews/car/:carmake', async (req, res) => { - try { - const documents = await Reviews.find({ car_make: req.params.carmake }); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching reviews by car make' }); - } -}); - -app.get('/fetchReviews/car/:carmake/:model', async (req, res) => { - try { - const documents = await Reviews.find({ car_make: req.params.carmake, car_model: req.params.model }); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching reviews by car make and model' }); - } -}); - -app.get('/fetchDealer/:id', async (req, res) => { - try { - const documents = await Dealerships.find({ id: req.params.id }); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealers by ID' }); - } -}); - -app.post('/insert_review', express.raw({ type: '*/*' }), async (req, res) => { - data = JSON.parse(req.body); - const documents = await Reviews.find().sort({ id: -1 }); - let new_id = documents[0].id + 1; - - const review = new Reviews({ - id: new_id, - name: data.name, - dealership: data.dealership, - review: data.review, - purchase: data.purchase, - purchase_date: data.purchase_date, - car_make: data.car_make, - car_model: data.car_model, - car_year: data.car_year, - }); - - try { - const savedReview = await review.save(); - res.json(savedReview); - } catch (error) { - console.log(error); - res.status(500).json({ error: 'Error inserting review' }); - } -}); - -app.get('/cars/:id', async (req, res) => { - try { - const documents = await Cars.find({dealer_id: req.params.id}); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching reviews' }); - } -}); - -app.get('/carsbymake/:id/:make', async (req, res) => { - try { - const documents = await Cars.find({dealer_id: req.params.id, make: req.params.make}); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching reviews by car make and model' }); - } -}); - -app.get('/carsbymodel/:id/:model', async (req, res) => { - try { - const documents = await Cars.find({ dealer_id: req.params.id, model: req.params.model }); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealers by ID' }); - } -}); - -app.get('/carsbymaxmileage/:id/:mileage', async (req, res) => { - try { - const documents = await Cars.find({ dealer_id: req.params.id, mileage : { $lte : req.params.mileage} }); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealers by ID' }); - } -}); - -app.get('/carsbyyear/:id/:year', async (req, res) => { - try { - const documents = await Cars.find({ dealer_id: req.params.id, year : { $gte :req.params.year }}); - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealers by ID' }); - } -}); - -app.listen(port, () => { - console.log(`Server is running on http://localhost:${port}`); -});