From a71c0a93a0f9ec145e1c529a6c60d9233e7c787e Mon Sep 17 00:00:00 2001 From: Brian Luo Date: Wed, 23 Feb 2022 13:34:09 -0500 Subject: [PATCH] Update routes and customer endpoints --- backend/package-lock.json | 1 + backend/package.json | 6 ++-- backend/src/app.ts | 9 +++-- backend/src/models/customer.ts | 2 +- backend/src/routes/customer.ts | 66 +++++++++++++++++++--------------- backend/src/routes/employee.ts | 6 ++-- 6 files changed, 53 insertions(+), 37 deletions(-) diff --git a/backend/package-lock.json b/backend/package-lock.json index c906b24..53d91fa 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "body-parser": "^1.19.2", "cors": "^2.8.5", "express": "^4.17.3", "mongoose": "^6.2.3", diff --git a/backend/package.json b/backend/package.json index 10b7191..35f1e12 100644 --- a/backend/package.json +++ b/backend/package.json @@ -19,11 +19,13 @@ "license": "ISC", "bugs": { "url": "https://github.com/CMU-17-356/dronuts2022-group-5/issues" - }, "homepage": "https://github.com/CMU-17-356/dronuts2022-group-5#readme", + }, + "homepage": "https://github.com/CMU-17-356/dronuts2022-group-5#readme", "dependencies": { + "body-parser": "^1.19.2", + "cors": "^2.8.5", "express": "^4.17.3", "mongoose": "^6.2.3", - "cors": "^2.8.5", "mongoose-unique-validator": "^3.0.0", "pkg": "^5.5.2" }, diff --git a/backend/src/app.ts b/backend/src/app.ts index 8c8a010..71fb661 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -1,4 +1,5 @@ import express from 'express'; +import bodyParser from 'body-parser'; import mongoose from 'mongoose'; import config from '../config.json'; @@ -6,9 +7,13 @@ import router from "./routes"; const app = express(); +const jsonParser = bodyParser.json(); +// const urlencoded = bodyParser.urlencoded({extended: false}); +app.use(jsonParser); + app.use(router); -app.use(express.json()) -app.use(express.urlencoded({ extended: false })); +// app.use(express.json()) +// app.use(express.urlencoded({ extended: false })); app.use(express.static(__dirname + '/public')); mongoose.connect(config.db, () => { diff --git a/backend/src/models/customer.ts b/backend/src/models/customer.ts index f3c34b8..ac184bb 100644 --- a/backend/src/models/customer.ts +++ b/backend/src/models/customer.ts @@ -53,7 +53,7 @@ const customerSchema: Schema = new Schema({ customerSchema.methods.toJSON = function () { return { - userName: this.userName, + username: this.username, password: this.password, phoneNumber: this.phoneNumber, emailAddress: this.emailAddress, diff --git a/backend/src/routes/customer.ts b/backend/src/routes/customer.ts index 7e4db44..b4e80c3 100644 --- a/backend/src/routes/customer.ts +++ b/backend/src/routes/customer.ts @@ -1,60 +1,68 @@ import {Request, Response, Router} from "express"; -import mongoose from "mongoose"; + import {CustomerModel} from "../models/customer"; import {OrderInterface, OrderModel,} from "../models/order"; const customerRouter = Router(); -customerRouter.get('/profile', function (req, res) { - const custId = req.query.custId; - try { - if (!custId) { - res.sendStatus(500) - } - const custRes = CustomerModel.findById(custId) - res.send(custRes); - } catch (err) { - console.log(err); - res.status(500).send(err); - } +customerRouter.get('/profile', async function (req, res) { + const custId = req.query.custId; + if (!custId) { + res.sendStatus(500); + return; + } + try { + const custRes = await CustomerModel.findById(custId).exec(); + res.send(custRes); + } catch (err) { + console.log(err); + res.status(500).send(err); } -) +}) customerRouter.post('/confirm', [], async (req: Request, res: Response) => { + const custId = req.query.custId; + if (!custId || !req.body) { + res.sendStatus(500); + return; + } try { - const customer = CustomerModel.findById(req.query.custId); - if (customer == null) { + const customer = await CustomerModel.findById(custId).exec(); + if (!customer) { res.status(500).send("no customer found"); } - const orderData: OrderInterface = JSON.parse(req.body); + const orderData: OrderInterface = new OrderModel(req.body); const orderStore = new OrderModel(orderData); - await orderStore.save() + await orderStore.save(); } catch (err) { console.log(err); res.status(500).send(err); } - res.send("success") + res.send("success"); }) -customerRouter.post('/order', function (req, res, next) { +customerRouter.post('/order', async function (req, res) { + const custId = req.query.custId; + if (!custId || !req.body) { + res.sendStatus(500); + return; + } try { - const customer = CustomerModel.findById(req.query.custId); - const orderData: OrderInterface = JSON.parse(req.body); - if (customer == null) { + const customer = await CustomerModel.findById(custId).exec(); + const orderData: OrderInterface = new OrderModel(req.body); + if (!customer) { res.status(500).send("no customer found"); } orderData.tax = 0.1; orderData.deliveryFee = 5; orderData.serviceFee = 0.4; - orderData.rating = 0.3 - res.send(orderData) + orderData.rating = 0.3; + res.send(orderData); } catch (err) { console.log(err); res.status(500).send(err); } +}); - -}) - -export default customerRouter \ No newline at end of file +export default customerRouter diff --git a/backend/src/routes/employee.ts b/backend/src/routes/employee.ts index 7dbba85..fd36888 100644 --- a/backend/src/routes/employee.ts +++ b/backend/src/routes/employee.ts @@ -6,7 +6,7 @@ import { OrderModel } from '../models/order'; const router = express.Router(); -router.get('/employee/test', [], async (req: Request, res: Response) => { +router.get('/test', [], async (req: Request, res: Response) => { // Temporary way of adding orders try { const donut = await DonutModel.findOne(); @@ -22,7 +22,7 @@ router.get('/employee/test', [], async (req: Request, res: Response) => { res.send('success'); }); -router.get('/employee/orders', [], async (req: Request, res: Response) => { +router.get('/orders', [], async (req: Request, res: Response) => { const orderId = req.query.orderId; try { if (!orderId) { @@ -41,7 +41,7 @@ router.get('/employee/orders', [], async (req: Request, res: Response) => { } }); -router.post('/employee/confirm', async (req: Request, res: Response) => { +router.post('/confirm', async (req: Request, res: Response) => { const orderId = req.query.orderId; if (!orderId) { // Missing orderId