Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
438 changes: 348 additions & 90 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"type": "module",
"scripts": {
"dev": "concurrently \"cd client && vite\" \"cd server && nodemon server\"",
"start": "cd server && node server",
"build": "cd client && vite build"
"start": "nodemon server/server.js",
"build": "cd client && vite build",
"reset": "node ./server/config/reset.js"
},
"dependencies": {
"@picocss/pico": "^1.5.7",
"concurrently": "^7.6.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"cors": "^2.8.5",
"dotenv": "^16.6.1",
"express": "^4.21.2",
"nodemon": "^2.0.21",
"pg": "^8.10.0",
"react": "^18.2.0",
Expand Down
24 changes: 24 additions & 0 deletions server/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pg from 'pg';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// need this since dotenv tryna look for env inside of config when its actually in the root directory

dotenv.config({ path: path.resolve(__dirname, '../../.env') });

const config = {
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
host: process.env.PGHOST,
port: process.env.PGPORT,
database: process.env.PGDATABASE,
ssl: {
rejectUnauthorized: false
}
}

export const pool = new pg.Pool(config)
78 changes: 78 additions & 0 deletions server/config/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { pool } from './database.js';
import itemData from '../data/items.js';
import testCar from '../data/testCar.js';

const createTables = async () => {
try {
await pool.query(`DROP TABLE IF EXISTS cars`);
await pool.query(`DROP TABLE IF EXISTS customItems`);

await pool.query(`
CREATE TABLE customItems (
id SERIAL PRIMARY KEY,
category VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
pricePoint VARCHAR(10) NOT NULL
)
`);

await pool.query(`
CREATE TABLE cars (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
wheels VARCHAR(255) NOT NULL,
exterior VARCHAR(255) NOT NULL,
interior VARCHAR(255) NOT NULL,
roof VARCHAR(255) NOT NULL,
totalCost VARCHAR(10) NOT NULL
)
`);
}
catch (err) {
console.log(err);
}

};

const seedCustomItemsTable = async () => {
for (const item of itemData) {
await pool.query(
`INSERT INTO customItems (category, name, pricePoint)
VALUES ($1, $2, $3)`,
[
item.category,
item.name,
item.pricePoint
]
);
console.log(`${item.name} added`);
}
};

const seedTestCarTable = async () => {
for (const car of testCar) {
await pool.query(
`INSERT INTO cars (name, wheels, exterior, interior, roof, totalCost)
VALUES ($1, $2, $3, $4, $5, $6)`,
[
car.name,
car.wheels,
car.exterior,
car.interior,
car.roof,
car.totalCost
]
);
console.log(`${car.name} added`);
}
}

const resetDatabase = async () => {
await createTables();
await seedCustomItemsTable();
await seedTestCarTable();
console.log('Database reset!');
await pool.end();
};

resetDatabase();
14 changes: 14 additions & 0 deletions server/controllers/cars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { pool } from '../config/database.js';

export const getCars = async (req, res) => {
let results;

try{
results = pool.query(`SELECT * FROM cars ORDER BY id ASC`);
}
catch (err) {
console.log('Error getting cars: ', err);
}

res.status(200).json(results.rows);
}
22 changes: 22 additions & 0 deletions server/controllers/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { pool } from '../config/database.js';

export const getItems = async (req, res) => {
let results;
const { category } = req.query;

try {
results = pool.query(`SELECT * FROM customItems WHERE category = $1`, [category]);
}
catch (err) {
console.log('Error getting items: ', err);
}

res.status(200).json(results.rows);
};

// front end

// const category = 'Exterior';
// const response = await fetch(`http://localhost:3001/items?category=${category}`);
// const data = await response.json();
// console.log(data);
107 changes: 107 additions & 0 deletions server/data/items.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const itemData = [
{
"category": "Exterior",
"name": "Sport Body Kit",
"pricePoint": 2500
},
{
"category": "Exterior",
"name": "Chrome Trim Package",
"pricePoint": 850
},
{
"category": "Exterior",
"name": "Front Splitter",
"pricePoint": 1200
},
{
"category": "Exterior",
"name": "Rear Diffuser",
"pricePoint": 1400
},
{
"category": "Exterior",
"name": "Side Skirt Extensions",
"pricePoint": 950
},

{
"category": "Interior",
"name": "Premium Leather Package",
"pricePoint": 1800
},
{
"category": "Interior",
"name": "Carbon Fiber Trim Kit",
"pricePoint": 950
},
{
"category": "Interior",
"name": "LED Ambient Lighting",
"pricePoint": 600
},
{
"category": "Interior",
"name": "Custom Steering Wheel",
"pricePoint": 700
},
{
"category": "Interior",
"name": "Racing Bucket Seats",
"pricePoint": 2200
},

{
"category": "Roof",
"name": "Carbon Fiber Roof",
"pricePoint": 3200
},
{
"category": "Roof",
"name": "Panoramic Glass Roof",
"pricePoint": 2800
},
{
"category": "Roof",
"name": "Sunroof Installation",
"pricePoint": 2100
},
{
"category": "Roof",
"name": "Roof Rack System",
"pricePoint": 900
},
{
"category": "Roof",
"name": "Roof Spoiler",
"pricePoint": 750
},

{
"category": "Wheels",
"name": "20-inch Alloy Wheels",
"pricePoint": 1600
},
{
"category": "Wheels",
"name": "18-inch Performance Wheels",
"pricePoint": 1300
},
{
"category": "Wheels",
"name": "Forged Racing Wheels",
"pricePoint": 2400
},
{
"category": "Wheels",
"name": "Matte Black Rims",
"pricePoint": 1500
},
{
"category": "Wheels",
"name": "Off-Road All-Terrain Wheels",
"pricePoint": 2000
}
]

export default itemData;
13 changes: 13 additions & 0 deletions server/data/testCar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const testCar = [
{
"id": 1,
"name": "Project Thunderbolt",
"wheels": "20-inch Alloy Wheels",
"exterior": "Sport Body Kit",
"interior": "Premium Leather Package",
"roof": "Carbon Fiber Roof",
"totalCost": "9100"
}
]

export default testCar;
40 changes: 10 additions & 30 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
import express from 'express'
import path from 'path'
import favicon from 'serve-favicon'
import dotenv from 'dotenv'
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';

// import the router from your routes file
const app = express();

app.use(cors());
app.get('/', (req, res) => {
res.status(200).send('Car Server');
})

dotenv.config()

const PORT = process.env.PORT || 3000

const app = express()

app.use(express.json())

if (process.env.NODE_ENV === 'development') {
app.use(favicon(path.resolve('../', 'client', 'public', 'lightning.png')))
}
else if (process.env.NODE_ENV === 'production') {
app.use(favicon(path.resolve('public', 'lightning.png')))
app.use(express.static('public'))
}

// specify the api path for the server to use


if (process.env.NODE_ENV === 'production') {
app.get('/*', (_, res) =>
res.sendFile(path.resolve('public', 'index.html'))
)
}
const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
console.log(`server listening on http://localhost:${PORT}`)
console.log(`Server listening on http://localhost:${PORT}`);
})