Skip to content

Commit

Permalink
Merge pull request #37 from dheffer/vehicle-history-uploads
Browse files Browse the repository at this point in the history
VehicleHistory add, edit, and delete operations added
  • Loading branch information
BrandonJohnson97 authored Apr 8, 2024
2 parents f5dbe0d + c8542a3 commit e3f2512
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 74 deletions.
119 changes: 81 additions & 38 deletions backend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,36 +193,68 @@ app.get('/api/get-vehicle-history', async (req, res) => {
res.send(getHistory);
});

/***
* This route is used to add maintenance history to the DATABASE
* @param configId - The config_id of the vehicle
* @param type - The type of maintenance
* @param date - The date of the maintenance
* @param maintenance - The maintenance performed
* @param cost - The cost of the maintenance
* @param email - The email of the user
*/
app.post('/api/add-maintenance-history', async (req, res) => {
const garage = DATABASE.collection("user_vehicle_info");
const configId = req.query.configId;
const maintenance = {
type: req.body.type,
date: req.body.date,
maintenance: req.body.maintenance,
cost: parseInt(req.body.cost)
};
const add = await garage.updateOne(
{ email: EMAIL, config_id: parseInt(configId) },
{ $push: { completed_maintenance: maintenance } }
);
return res.json(add.modifiedCount);
});

/***
* This route is used to update maintenance history in the DATABASE
* @param configId - The config_id of the vehicle
* @param old_type - The old type of maintenance
* @param old_date - The old date of the maintenance
* @param old_maintenance - The old maintenance performed
* @param old_cost - The old cost of the maintenance
* @param new_type - The new type of maintenance
* @param new_date - The new date of the maintenance
* @param new_maintenance - The new maintenance performed
* @param new_cost - The new cost of the maintenance
* @param email - The email of the user
*/
app.post('/api/update-maintenance-history', async (req, res) => {
try {
const garage = DATABASE.collection("user_vehicle_info");

const {
old_type, old_date, old_maintenance, old_cost,
new_type, new_date, new_maintenance, new_cost
} = req.body;
const update = await garage.updateOne(
{
email: EMAIL,
"completed_maintenance.type": old_type,
"completed_maintenance.date": old_date,
"completed_maintenance.maintenance": old_maintenance,
"completed_maintenance.cost": parseInt(old_cost)
},
{
$set: {
"completed_maintenance.$.type": new_type,
"completed_maintenance.$.date": new_date,
"completed_maintenance.$.maintenance": new_maintenance,
"completed_maintenance.$.cost": parseInt(new_cost)
}
const garage = DATABASE.collection("user_vehicle_info");
const {
old_type, old_date, old_maintenance, old_cost,
new_type, new_date, new_maintenance, new_cost
} = req.body;
const update = await garage.updateOne(
{
email: EMAIL,
config_id: parseInt(req.query.configId),
"completed_maintenance.type": old_type,
"completed_maintenance.date": old_date,
"completed_maintenance.maintenance": old_maintenance,
"completed_maintenance.cost": parseInt(old_cost)
},
{
$set: {
"completed_maintenance.$.type": new_type,
"completed_maintenance.$.date": new_date,
"completed_maintenance.$.maintenance": new_maintenance,
"completed_maintenance.$.cost": parseInt(new_cost)
}
);
return res.json(update.modifiedCount);
} catch (error) {
return res.status(500).json({message: error.message});
}

});
return res.json(update.modifiedCount);
});

app.delete('/api/delete-maintenance-history', async (req, res) => {
Expand All @@ -231,7 +263,8 @@ app.delete('/api/delete-maintenance-history', async (req, res) => {
const { type, date, maintenance, cost } = req.body;
const deletion = await garage.updateOne(
{
email: EMAIL
email: EMAIL,
config_id: parseInt(req.query.configId)
},
{$pull:
{
Expand Down Expand Up @@ -281,13 +314,17 @@ app.get('/api/get-user-vehicles', async (req, res) => {
});
app.delete('/api/delete-user-vehicle', async (req, res) => {
const garage = DATABASE.collection("user_garage");
const vehicInfo = DATABASE.collection("user_vehicle_info");

const { config_id } = req.body;
const deletion = await garage.updateOne(
{email: EMAIL},
{$pull: { vehicle_config_ids: config_id } }
);
return res.json(deletion.modifiedCount);
const deletionTwo = await vehicInfo.deleteOne(
{email: EMAIL, config_id: config_id}
);
return res.json(deletion.modifiedCount+deletionTwo.deletedCount);
});


Expand Down Expand Up @@ -424,6 +461,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,21 +472,26 @@ 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, config_id: 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, config_id: config_id, odometer: 0, upcoming_maintenance: [], completed_maintenance: [] })
return res.status(200).json({message: "User Vehicle added to info"});
}
}
catch(err) {
Expand Down
110 changes: 100 additions & 10 deletions frontend/src/pages/maintenance-history/ManualVehicleHistory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,108 @@
import Button from "react-bootstrap/Button";
import {Link} from "react-router-dom";
import {Link, useLocation} from "react-router-dom";
import { useNavigate} from "react-router-dom";
import Modal from "react-bootstrap/Modal";
import {Alert, Col, Form, InputGroup, Row} from "react-bootstrap";
import {useRef, useState} from "react";

function ManualVehicleHistory() {
let navigate = useNavigate();
function ManualVehicleHistory(props) {

const [refreshData, setRefreshData] = useState(false);
const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);

const typeText = useRef();
const dateText = useRef();
const maintenanceText = useRef();
const costText = useRef();

const submit = (e) => {
e.preventDefault();
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
const raw = JSON.stringify({
"type": typeText.current.value,
"date": dateText.current.value,
"maintenance": maintenanceText.current.value,
"cost": parseInt(costText.current.value)
});
const reqOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

fetch("/api/add-maintenance-history?configId="+props.configId, reqOptions)
.then((res) => res.json())
.then((result) => {
console.log(result);
setRefreshData(!refreshData);
})
.catch((error) => console.error(error));
handleClose();
}

return (
<div>
<h1>Manual Vehicle History</h1>
<p>Uh oh! This page isn't finished yet!</p>
<Button onClick={() => navigate(-1)}>
Go Back
</Button>
</div>
<>
<Button onClick={handleShow}>Manual</Button>
<Modal show={show} onHide={handleClose}>
<Modal.Dialog>
<Modal.Header closeButton>
<Modal.Title>Manual Maintenance History</Modal.Title>
</Modal.Header>
<Modal.Body>
<Form>
<Form.Group>
<Row>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Type</InputGroup.Text>
<Form.Select type="as" ref={typeText} required>
<option value="Maintenance">Maintenance</option>
<option value="Repair">Repair</option>
<option value="Inspection">Inspection</option>
<option value="Replacement">Replacement</option>
<option value="Other">Other</option>
</Form.Select>
</InputGroup>
</Col>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Date</InputGroup.Text>
<Form.Control type="date" defaultValue={"yyyy-mm-dd"}
ref={dateText} required/>
</InputGroup>
</Col>
</Row>
<Row>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Service</InputGroup.Text>
<Form.Control type="text" placeholder={""}
ref={maintenanceText} required/>
</InputGroup>
</Col>
<Col>
<InputGroup hasValidation>
<InputGroup.Text>Cost</InputGroup.Text>
<Form.Control type="number" placeholder={"0.00"}
ref={costText} required/>
</InputGroup>
</Col>
</Row>

</Form.Group>
</Form>
</Modal.Body>
<Modal.Footer className={'mx-auto'}>
<Button variant="secondary" onClick={handleClose}>Close</Button>
<Button variant="success" onClick={submit}>Upload</Button>
</Modal.Footer>
</Modal.Dialog>
</Modal>
</>
)
}

Expand Down
14 changes: 7 additions & 7 deletions frontend/src/pages/maintenance-history/UploadNavbar.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {Link} from "react-router-dom";
import Button from "react-bootstrap/Button";
import Manipulator from "bootstrap/js/src/dom/manipulator";
import ManualVehicleHistory from "./ManualVehicleHistory";

function UploadNavbar() {
function UploadNavbar(props) {
return (
<div className="container">
<div className="row">
<div className="col-md-1 order-md-1"/>
<div className="col-md-10 order-md-2">
<div className="d-flex justify-content-center">
<Link to={"/garage/vehicle-history/upload"} className="me-3">
<Button variant="primary">Upload</Button>
</Link>

<Link to={"/garage/vehicle-history/manual"} className="ml-3">
<Button variant="primary">Manual</Button>
<Link to={"/garage/vehicle-history/upload"} className="me-3"
onClick={(e) => e.preventDefault()}>
<Button variant="primary" disabled>Upload</Button>
</Link>
<ManualVehicleHistory configId={props.configId}/>
</div>
</div>
<div className="col-md-1 order-md-3"/>
Expand Down
Loading

0 comments on commit e3f2512

Please sign in to comment.