Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from markmacw/3-backend-A-express-mongo
Browse files Browse the repository at this point in the history
3 backend a express mongo and also Django models
  • Loading branch information
markmacw authored Oct 16, 2024
2 parents d4361ae + 89c2f66 commit 259f103
Show file tree
Hide file tree
Showing 14 changed files with 1,233 additions and 72 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Node Modules
node_modules/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Django Shell",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": ["shell"],
"django": true,
"console": "integratedTerminal"
}
]
}
112 changes: 63 additions & 49 deletions server/database/app.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,114 @@
const express = require('express');
const mongoose = require('mongoose');
const fs = require('fs');
const cors = require('cors')
const app = express()
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(require('body-parser').urlencoded({ extended: false }));
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'));
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');
const Reviews = require("./review");
const Dealerships = require("./dealership");

try {
Reviews.deleteMany({}).then(()=>{
Reviews.insertMany(reviews_data['reviews']);
Reviews.deleteMany({}).then(() => {
Reviews.insertMany(reviews_data["reviews"]);
});
Dealerships.deleteMany({}).then(()=>{
Dealerships.insertMany(dealerships_data['dealerships']);
Dealerships.deleteMany({}).then(() => {
Dealerships.insertMany(dealerships_data["dealerships"]);
});

} catch (error) {
res.status(500).json({ error: 'Error fetching documents' });
res.status(500).json({ error: "Error fetching documents" });
}


// Express route to home
app.get('/', async (req, res) => {
res.send("Welcome to the Mongoose API")
app.get("/", async (req, res) => {
res.send("Welcome to the Mongoose API");
});

// Express route to fetch all reviews
app.get('/fetchReviews', async (req, res) => {
app.get("/fetchReviews", async (req, res) => {
try {
const documents = await Reviews.find();
res.json(documents);
} catch (error) {
res.status(500).json({ error: 'Error fetching documents' });
res.status(500).json({ error: "Error fetching documents" });
}
});

// Express route to fetch reviews by a particular dealer
app.get('/fetchReviews/dealer/:id', async (req, res) => {
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' });
res.status(500).json({ error: "Error fetching documents" });
}
});

// Express route to fetch all dealerships
app.get('/fetchDealers', async (req, res) => {
//Write your code here
app.get("/fetchDealers", async (req, res) => {
try {
const documents = await Dealerships.find();
res.json(documents);
} catch (error) {
res.status(500).json({ error: "Error fetching documents" });
}
});

// Express route to fetch Dealers by a particular state
app.get('/fetchDealers/:state', async (req, res) => {
//Write your code here
app.get("/fetchDealers/:state", async (req, res) => {
//Write your code here
try {
const documents = await Dealerships.find({ state: req.params.state });
res.json(documents);
} catch (error) {
res.statu(500).json({ error: "Error fetching documents" });
}
});

// Express route to fetch dealer by a particular id
app.get('/fetchDealer/:id', async (req, res) => {
//Write your code here
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 documents" });
}
});

//Express route to insert review
app.post('/insert_review', express.raw({ type: '*/*' }), async (req, res) => {
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);
res.status(500).json({ error: 'Error inserting review' });
console.log(error);
res.status(500).json({ error: "Error inserting review" });
}
});

Expand Down
Loading

0 comments on commit 259f103

Please sign in to comment.