From f3120f6ae52b7db1122cca3f7497c26e18a99aab Mon Sep 17 00:00:00 2001 From: bouhvli Date: Wed, 8 Jan 2025 13:22:45 +0100 Subject: [PATCH] jslint --- server/database/app.js | 111 +++++++++++++++++----------------- server/database/dealership.js | 7 ++- server/database/inventory.js | 15 ++--- server/database/review.js | 7 ++- 4 files changed, 73 insertions(+), 67 deletions(-) diff --git a/server/database/app.js b/server/database/app.js index a40357074c..39114e6c71 100644 --- a/server/database/app.js +++ b/server/database/app.js @@ -1,17 +1,20 @@ +/*jshint esversion: 6 */ +/*jshint sub:true*/ +/* jshint esversion: 8 */ const express = require('express'); const mongoose = require('mongoose'); const fs = require('fs'); -const cors = require('cors') -const app = express() +const cors = require('cors'); +const app = express(); const port = 3030; -app.use(cors()) +app.use(cors()); app.use(require('body-parser').urlencoded({ extended: false })); const reviews_data = JSON.parse(fs.readFileSync("reviews.json", 'utf8')); const dealerships_data = JSON.parse(fs.readFileSync("dealerships.json", 'utf8')); -mongoose.connect("mongodb://mongo_db:27017/",{'dbName':'dealershipsDB'}); +mongoose.connect("mongodb://mongo_db:27017/", { 'dbName': 'dealershipsDB' }); const Reviews = require('./review'); @@ -19,13 +22,13 @@ const Reviews = require('./review'); const Dealerships = require('./dealership'); try { - Reviews.deleteMany({}).then(()=>{ + Reviews.deleteMany({}).then(() => { Reviews.insertMany(reviews_data['reviews']); }); - Dealerships.deleteMany({}).then(()=>{ + Dealerships.deleteMany({}).then(() => { Dealerships.insertMany(dealerships_data['dealerships']); }); - + } catch (error) { res.status(500).json({ error: 'Error fetching documents' }); } @@ -33,7 +36,7 @@ try { // Express route to home app.get('/', async (req, res) => { - res.send("Welcome to the Mongoose API") + res.send("Welcome to the Mongoose API"); }); // Express route to fetch all reviews @@ -49,7 +52,7 @@ app.get('/fetchReviews', async (req, res) => { // Express route to fetch reviews by a particular dealer app.get('/fetchReviews/dealer/:id', async (req, res) => { try { - const documents = await Reviews.find({dealership: req.params.id}); + const documents = await Reviews.find({ dealership: req.params.id }); res.json(documents); } catch (error) { res.status(500).json({ error: 'Error fetching documents' }); @@ -58,63 +61,63 @@ app.get('/fetchReviews/dealer/:id', async (req, res) => { // Express route to fetch all dealerships app.get('/fetchDealers', async (req, res) => { - try { - const documents = await Dealerships.find(); // Fetch all dealerships - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealerships' }); - } - }); - - // Express route to fetch dealers by a particular state - app.get('/fetchDealers/:state', async (req, res) => { - try { - const state = req.params.state; // Extract state from route parameters - const documents = await Dealerships.find({ state: state }); // Fetch dealerships matching the state - res.json(documents); - } catch (error) { - res.status(500).json({ error: 'Error fetching dealerships by state' }); - } - }); - - // Express route to fetch dealer by a particular id - app.get('/fetchDealer/:id', async (req, res) => { - try { - const id = req.params.id; // Extract dealer ID from route parameters - const document = await Dealerships.findOne({ id: parseInt(id) }); // Fetch dealership by ID - if (document) { - res.json(document); - } else { - res.status(404).json({ error: 'Dealer not found' }); - } - } catch (error) { - res.status(500).json({ error: 'Error fetching dealership by ID' }); + try { + const documents = await Dealerships.find(); // Fetch all dealerships + res.json(documents); + } catch (error) { + res.status(500).json({ error: 'Error fetching dealerships' }); + } +}); + +// Express route to fetch dealers by a particular state +app.get('/fetchDealers/:state', async (req, res) => { + try { + const state = req.params.state; // Extract state from route parameters + const documents = await Dealerships.find({ state: state }); // Fetch dealerships matching the state + res.json(documents); + } catch (error) { + res.status(500).json({ error: 'Error fetching dealerships by state' }); + } +}); + +// Express route to fetch dealer by a particular id +app.get('/fetchDealer/:id', async (req, res) => { + try { + const id = req.params.id; // Extract dealer ID from route parameters + const document = await Dealerships.findOne({ id: parseInt(id) }); // Fetch dealership by ID + if (document) { + res.json(document); + } else { + res.status(404).json({ error: 'Dealer not found' }); } - }); + } catch (error) { + res.status(500).json({ error: 'Error fetching dealership by ID' }); + } +}); //Express route to insert review 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 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'], - }); + "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); + console.log(error); res.status(500).json({ error: 'Error inserting review' }); } }); diff --git a/server/database/dealership.js b/server/database/dealership.js index b10d6b4730..d0a6f3a8b9 100644 --- a/server/database/dealership.js +++ b/server/database/dealership.js @@ -1,13 +1,14 @@ +/*jshint esversion: 6 */ const mongoose = require('mongoose'); const Schema = mongoose.Schema; const dealerships = new Schema({ - id: { + id: { type: Number, required: true, - }, - city: { + }, + city: { type: String, required: true }, diff --git a/server/database/inventory.js b/server/database/inventory.js index 2c22fd092c..6be6b7f078 100644 --- a/server/database/inventory.js +++ b/server/database/inventory.js @@ -1,30 +1,31 @@ +/*jshint esversion: 6 */ const { Int32 } = require('mongodb'); const mongoose = require('mongoose'); const Schema = mongoose.Schema; const cars = new Schema({ -dealer_id: { + dealer_id: { type: Number, required: true -}, -make: { + }, + make: { type: String, required: true }, -model: { + model: { type: String, required: true }, -bodyType: { + bodyType: { type: String, required: true }, -year: { + year: { type: Number, required: true }, -mileage: { + mileage: { type: Number, required: true } diff --git a/server/database/review.js b/server/database/review.js index 4759725a3a..e8af04115b 100644 --- a/server/database/review.js +++ b/server/database/review.js @@ -1,13 +1,14 @@ +/*jshint esversion: 6 */ const mongoose = require('mongoose'); const Schema = mongoose.Schema; const reviews = new Schema({ - id: { + id: { type: Number, required: true, - }, - name: { + }, + name: { type: String, required: true },