Skip to content

Commit

Permalink
Added odometer, added update but no working yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonJohnson97 committed Apr 9, 2024
1 parent cb07d7f commit 8782ece
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 14 deletions.
101 changes: 89 additions & 12 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ app.get('/api/get-vehicle-info', async (req, res) => {
app.get('/api/get-vehicle-history', async (req, res) => {
const history = DATABASE.collection("user_vehicle_info");
const configId = req.query.configId;


const getHistory = await history.aggregate([
{
Expand Down Expand Up @@ -252,11 +253,12 @@ app.delete('/api/delete-maintenance-history', async (req, res) => {
*/
app.get('/api/get-user-vehicles', async (req, res) => {
const garage = DATABASE.collection("user_garage");
const brandon = "[email protected]"

const vehicles = await garage.aggregate([
{
$match: {
email: EMAIL
email: brandon
}
},
{
Expand All @@ -273,7 +275,7 @@ app.get('/api/get-user-vehicles', async (req, res) => {
{
$project: {
_id: 0,
configurations: 1
configurations: 1,
}
}
]).toArray();
Expand All @@ -297,7 +299,7 @@ app.delete('/api/delete-user-vehicle', async (req, res) => {
app.get('/api/get-maintenance', async (req , res) => {
const message = DATABASE.collection("maintenance");

// const docObject = await message.findOne({ config_id: { $eq: 401988727} });
// const docObject = await message.findOne({ config_id: { $eq: 401988727} });

const docObject = await message.findOne({});
await console.log(docObject.schedules[0].tasks);
Expand Down Expand Up @@ -329,7 +331,8 @@ app.get('/api/get-years', async (req, res) => {
const database = client.db("vehicleDB");
const configurations = database.collection("configurations");
const years = await configurations.aggregate([
{ $group: { _id: "$year", years: { $addToSet: "$year" } }}
{ $group: { _id: "$year", years: { $addToSet: "$year" } }},
{ $sort: { _id: 1 }}
]).toArray();
console.log("years ", years);
res.send(years);
Expand Down Expand Up @@ -424,6 +427,7 @@ app.get('/api/get-transmissions', async (req, res) => {
});

app.post('/api/add-vehicle', async (req, res) => {
const userEmail = process.env.EMAIL;
console.log("ADD: Hit add vehicle route");

const email = req.body.email;
Expand All @@ -434,28 +438,101 @@ app.post('/api/add-vehicle', async (req, res) => {

const database = client.db("vehicleDB");
const garage = database.collection("user_garage");
const userVehicle = database.collection("user_vehicle_info");

try{
const exist = await garage.findOne({email});
const exist = await garage.findOne({email: userEmail});
if(exist) {
const result = await garage.updateOne(
{ email },
await garage.updateOne(
{ email: userEmail },
{ $addToSet: { vehicle_config_ids: config_id } }
);
return res.json(result);
await userVehicle.insertOne(
{ email: userEmail, vehicle_config_ids: config_id, odometer: 0, upcoming_maintenance: [], completed_maintenance: [] })
return res.status(200).json({message: "Vehicle added to garage"});
}
else {
const result = await garage.insertOne(
{ email, vehicle_config_ids: [config_id] }
);
return res.json(result);
await garage.insertOne(
{ email: userEmail, vehicle_config_ids: [config_id] }
)
await userVehicle.insertOne(
{ email: userEmail, vehicle_config_ids: config_id, odometer: 0, upcoming_maintenance: [], completed_maintenance: [] })
return res.status(200).json({message: "User Vehicle added to info"});
}
}
catch(err) {
return res.status(500).json(err);
}
});

app.post('/api/update-odometer', async (req, res) => {
console.log("HIT UPDATE ODOMETER")
const userEmail = process.env.EMAIL;
const odometer = req.body.odometer;
const config_id = req.body.config_id;
console.log("UPDATE ODOMETER: "+odometer);
console.log("UPDATE config_id: "+config_id);
console.log("UPDATE EMAIL: "+userEmail)

const database = client.db("vehicleDB");
const userVehicle = database.collection("user_vehicle_info");

try{
await userVehicle.updateOne(
{ email: userEmail, vehicle_config_ids: config_id },
{ $set: { odometer: odometer }}
);
return res.status(200).json({message: "Odometer updated"});
}
catch(err) {
return res.status(500).json(err);
}
});

app.get('/api/get-user-vehicle-odometers', async (req, res) => {
console.log("HIT GET USER VEHICLE ODOMETERS")
const userEmail = process.env.EMAIL;
const database = client.db("vehicleDB");
const garage = database.collection("user_garage");
const userVehicle = database.collection("user_vehicle_info");
const userGarage = await garage.findOne({email: userEmail});
if(!userGarage){
return res.status(404).json({message: "User not found"});
}

const vehicleConfigIds = userGarage.vehicle_config_ids;
if(!vehicleConfigIds){
return res.status(404).json({message: "User has no vehicles"});
}
console.log("VEHICLE CONFIG IDS in get ODOMETERS: "+vehicleConfigIds)

const odometerReadings = await userVehicle.find(
{email: userEmail, vehicle_config_ids: {$in: vehicleConfigIds} },
{_id: 0, vehicle_config_ids: 1, odometer: 1}
).toArray();

console.log("ODOMETER READINGS: "+JSON.stringify(odometerReadings))

const odometerMap = {};
odometerReadings.forEach(reading => {
odometerMap[reading.vehicle_config_ids] = reading.odometer;
});

console.log("ODOMETER MAP: "+JSON.stringify(odometerMap))

const response = vehicleConfigIds.map(configId => ({
vehicle_config_ids: configId,
odometer: odometerMap[configId]}));

console.log("RESPONSE: "+JSON.stringify(response))

response.forEach((item, index) => {
console.log(`${index + 1}. vehicle_config_ids: ${item.vehicle_config_ids}, odometer: ${item.odometer}`);
})

res.status(200).json(response);
});

//TODO: Request to get a list of all engines given a year, make, and model (Non-repeating).

//TODO: Request to get a list of all transmissions given a year, make, model, and engine (Non-repeating).
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const Login = () => {
return (
<div>
{error && <p>{error}</p>}
<h1>Welcome to Vehicle Maintenance Prediction</h1>
<h1>Welcome to DriveLine</h1>
<button disabled={!googleOauthURL} onClick={handleLogin}>Login with Google</button>
</div>
)
Expand Down
26 changes: 26 additions & 0 deletions frontend/src/pages/garage/Garage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ function Garage(props) {
});
}, [refreshData]);

const updateOdometer = async (configID, email) => {
try{
const response = await fetch("/api/update-odometer", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
vehicle_config_ids : configID,
email: email,
//odometer: odometer
})
});
if(response.ok){
setRefreshData(!refreshData);
}
else{
console.error("Failed to update Odometer");
}
}
catch (error){
console.error("Catch Failed to update Odometer");
}
}

let vehicle_count = 0;

return (
Expand All @@ -66,6 +91,7 @@ function Garage(props) {
rows[rows.length - 1].push(
<Col key={vehicle.configurations.name}>
<Vehicle vehicle={vehicle} id={index}/>
{console.log(vehicle)}
</Col>
);
return rows;
Expand Down
71 changes: 70 additions & 1 deletion frontend/src/pages/garage/Vehicle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
import {Badge, Button, Card} from "react-bootstrap";
import {Badge, Button, Card, Form} from "react-bootstrap";
import {Link} from "react-router-dom";
import RemoveVehicle from "./RemoveVehicle";
import {useEffect, useState} from "react";

function Vehicle(props) {
const [odometerValue, setOdometerValue] = useState(0);
const [odometerReading, setOdometerReading] = useState([]);

useEffect(() => {
fetchReadings();
}, []);

const fetchReadings = async () => {
try{
const response = await fetch("/api/get-user-vehicle-odometers");
if(response.ok){
const data = await response.json();
setOdometerReading(data);
}
else{
console.log("Failed to fetch odometer readings");
}
}
catch (e) {
console.error('There has been a problem with your fetch operation:', e);
}
}


const v = props.vehicle;
const handleUpdateOdometer = () => {
console.log("Updating odometer");
try{
const response = fetch("/api/update-odometer", {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
vehicle_config_ids : v.configurations.config_id,
email: "[email protected]",
odometer: odometerValue
})
});
if(response.ok){
console.log("Odometer updated");
fetchReadings();
}
else{
console.log("Odometer update failed");
}
}
catch (e) {
console.error('There has been a problem with your fetch operation:', e);
}
}

const getOdometer = (config) => {
const reading = odometerReading.find(vehicle => vehicle.vehicle_config_ids === config);
return reading ? reading.odometer : -9999;
}

return (
<div>
<Card style={{width: '22rem'}} id={v.configurations.year+v.configurations.make+v.configurations.model}>
Expand All @@ -19,6 +76,18 @@ function Vehicle(props) {
</Card.Text>
<Card.Footer className="d-flex justify-content-around">
<p><Badge bg='secondary' >CONFIG ID</Badge> {v.configurations.config_id}</p>
<p><Badge bg='secondary'>ODOMETER</Badge> {getOdometer(v.configurations.config_id)}</p>
</Card.Footer>
<Card.Footer className="d-flex justify-content-around">
<Form.Group>
<Form.Control
type="number"
placeholder="Enter Odometer Value"
value={odometerValue}
onChange={e => setOdometerValue(e.target.value)}
/>
</Form.Group>
<Button variant="primary" onClick={handleUpdateOdometer}>Update Odometer</Button>
</Card.Footer>
</Card.Body>
</Card>
Expand Down

0 comments on commit 8782ece

Please sign in to comment.