Skip to content

Commit

Permalink
jslint
Browse files Browse the repository at this point in the history
  • Loading branch information
bouhvli committed Jan 8, 2025
1 parent f161bb4 commit f3120f6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 67 deletions.
111 changes: 57 additions & 54 deletions server/database/app.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
/*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');

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' });
}


// 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
Expand All @@ -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' });
Expand All @@ -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' });
}
});
Expand Down
7 changes: 4 additions & 3 deletions server/database/dealership.js
Original file line number Diff line number Diff line change
@@ -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
},
Expand Down
15 changes: 8 additions & 7 deletions server/database/inventory.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions server/database/review.js
Original file line number Diff line number Diff line change
@@ -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
},
Expand Down

0 comments on commit f3120f6

Please sign in to comment.