From b759fa54b6a85662267437577bf5c9ba2b5e971a Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 6 Jun 2024 23:12:47 -0700 Subject: [PATCH] PLEASE WOKR --- package.json | 2 +- packages/express-backend/app.js | 383 ------------------ packages/react-frontend/package.json | 2 +- .../react-frontend/src/Components/Table.jsx | 2 +- .../react-frontend/src/Views/AddOrders.jsx | 8 +- .../react-frontend/src/Views/EditProfile.jsx | 2 +- .../react-frontend/src/Views/Inventory.jsx | 6 +- .../react-frontend/src/Views/LoginPage.jsx | 2 +- .../react-frontend/src/Views/ManageOrders.jsx | 6 +- .../src/Views/OrderStatistics.jsx | 6 +- .../react-frontend/src/Views/ProductPage.jsx | 4 +- packages/react-frontend/src/Views/Profile.jsx | 12 +- .../react-frontend/src/Views/SignUpPage.jsx | 2 +- 13 files changed, 27 insertions(+), 410 deletions(-) delete mode 100644 packages/express-backend/app.js diff --git a/package.json b/package.json index cdf0aec..40ca38f 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "jest", "build": "cd packages/react-frontend && npm run build", - "start": "cd packages/express-backend && npm start", + "start": "concurrently \"cd packages/express-backend && npm start\" \"cd packages/react-frontend && npm start\"", "dev": "concurrently \"cd packages/express-backend && npm run dev\" \"cd packages/react-frontend && npm run dev\"", "prettier": "npx prettier . --write", "eslint": "npx eslint ." diff --git a/packages/express-backend/app.js b/packages/express-backend/app.js deleted file mode 100644 index ea087c0..0000000 --- a/packages/express-backend/app.js +++ /dev/null @@ -1,383 +0,0 @@ -import cors from "cors"; -import orderService from "./services/order-service.js"; -import orderUnitService from "./services/order-unit-service.js"; -import productService from "./services/product-service.js"; -import userService from "./services/user-service.js"; -import express from "express"; -import multer from "multer"; - -const app = express(); -const port = 8000; - -app.use(cors()); -app.use(express.json()); - -// Routes -const upload = multer({ dest: "uploads/" }); -app.use("/uploads", express.static("uploads")); - -app.post( - "/profile-picture", - upload.single("profilePicture"), - userService.authenticateUser, - async (req, res) => { - const file = req.file; - const id = req.userID; - if (!file) { - res.status(400).send("No file uploaded"); - return; - } - - try { - const result = await userService.uploadProfilePicture(file); - const user = await userService.findUserById(id); - - const pfp = { profilePicture: result._id }; - - fetch(`https://safehaven2.azurewebsites.net/users/${user._id}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(pfp) - }); - res.status(201).send(result._id.toString()); - } catch (error) { - res.status(500).send(error.name); - } - } -); - -app.patch("/users/:id", (req, res) => { - const id = req.params["id"]; - userService - .changeUserProfilePicture(id, req.body.profilePicture) - .then((result) => { - if (result) { - res.send(result); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - console.log(error); - res.status(500).send(error); - }); -}); - -app.get("/profile-picture/:id", (req, res) => { - const id = req.params["id"]; - userService - .findProfilePictureById(id) - .then((result) => { - if (result) { - res.contentType(result.contentType); - res.send(result.data); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.post("/users", (req, res) => { - userService.signupUser(req, res); -}); - -app.post("/users/profile", userService.authenticateUser, (req, res) => { - const { bio, skills } = req.body; // from form - const id = req.userID; - userService.editProfile(id, bio, skills) - .then((result) => { - if (result) { - res.send(result); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - console.log(error); - res.status(500).send(error); - }); -}); - -app.get("/users", userService.authenticateUser, (req, res) => { - const id = req.userID; - userService - .findUserById(id) - .then((result) => { - if (result) { - res.send(result); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.get("/users/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - userService - .findUserById(id) - .then((result) => { - if (result) { - res.send(result); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.post("/login", (req, res) => { - userService.loginUser(req, res); -}); - -app.delete("/users", userService.authenticateUser, (req, res) => { - const id = req.userID; - userService - .removeUser(id) - .then((result) => { - res.status(204).send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.delete("/products/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - const userID = req.userID; - console.log(userID); - productService - .removeProduct(id) - .then((result) => { - console.log("plz"); - userService.removeProductFromUserID(userID, id).then((Result) => { - console.log("hi"); - res.status(204).send(Result); - }); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.post("/products", userService.authenticateUser, (req, res) => { - const productToAdd = req.body; - productService - .addProduct(productToAdd) - .then((result) => { - const UserID = req.userID; - userService.addProductToUser(UserID, result.id); - console.log(result); - res.status(201).send(result); - }) - .catch((error) => { - console.log(error); - res.status(500).send(error.name); - }); -}); - -app.get("/products/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - productService - .findProductById(id) - .then((result) => { - if (result) { - res.send(result); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.patch("/products/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - const productChanges = req.body; - productService - .changeProductById(id, productChanges) - .then((result) => { - if (result) { - res.send(result); - } else { - res.status(404).send(`Not found: ${id}`); - } - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.get("/products", userService.authenticateUser, async (req, res) => { - // const product = req.query.product; - // const quantity = req.query.quantity; - const UserID = req.userID; - try { - const user = await userService.findUserById(UserID); - productService.findProductsByIds(user.products).then((result) => { - if (result) { - res.send(result); - } else { - res.status(500).send("Error retrieving user products"); - } - }); - } catch (error) { - res.status(500).send("Error retrieving user products"); - } -}); - -app.get("/", (req, res) => { - res.send("Hello World!"); -}); - -//add_orders routes -app.get("/orders", userService.authenticateUser, async (req, res) => { - //const id = req.query.id; - //const product = req.query.product; - //const quantity = req.query.quantity; - const search = req.query.search; - const UserID = req.userID; - const user = await userService.findUserById(UserID); - console.log(search); - let srch ={"items.product" : { $regex: search, $options: "i" }}; - console.log(srch); - if(search === undefined){ - console.log("Normal"); - orderService - .findOrdersByIds(user.orders) - .then((result) => { - console.log(result); - res.send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); - } else { - console.log("Searching"); - orderService - .search(user.orders, srch) - .then((result) => { - console.log(result); - res.send(result); - }) - .catch((error) => { - console.log(error); - res.status(500).send(error.name); - }); - } -}); - - -app.get("/orders/:find", userService.authenticateUser, (req, res) => { - const find = req.params["find"]; - console.log(find); - let srch = "{$or:{_id:{$regex:\"" + find + "\"}},{product:{$regex:\"" + find + "\"}},{quantity:{$regex:\"" + find + "\"}}]"; - console.log("HERE"); - orderService - .find({srch}) - .then((result) => { - res.send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.post("/orders", userService.authenticateUser, (req, res) => { - //console.log(req.body); - const orderToAdd = req.body; - orderService - .addOrder(orderToAdd) - .then((result) => { - const UserID = req.userID; - userService.addOrderToUser(UserID, result.id); - console.log(result); - res.status(201).send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.delete("/orders/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - const userID = req.userID; - orderService - .removeOrder(id) - .then((result) => { - userService.removeOrderFromUserID(userID, id).then((Result) => { - console.log("hi"); - res.status(204).send(Result); - }); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - - -//order-units routes -app.get("/order-units", userService.authenticateUser, (req, res) => { - const id = req.query.id; - const product = req.query.product; - const quantity = req.query.quantity; - orderUnitService - .getOrder(id, product, quantity) - .then((result) => { - res.send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.get("/order-units/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - orderUnitService - .findOrderById(id) - .then((result) => { - res.send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.post("/order-units", userService.authenticateUser, (req, res) => { - const orderToAdd = req.body; - //console.log("POST: ", req.body); - orderUnitService - .addOrder(orderToAdd) - .then((result) => { - res.status(201).send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.delete("/order-units/:id", userService.authenticateUser, (req, res) => { - const id = req.params["id"]; - orderUnitService - .removeOrder(id) - .then((result) => { - res.status(204).send(result); - }) - .catch((error) => { - res.status(500).send(error.name); - }); -}); - -app.listen(port, () => { - console.log(`Example app listening at safehaven307.azurewebsites.net:${port}`); -}); diff --git a/packages/react-frontend/package.json b/packages/react-frontend/package.json index 0b32495..3398f04 100644 --- a/packages/react-frontend/package.json +++ b/packages/react-frontend/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "dev": "vite", - "start": "vite --host", + "start": "vite --host --port 80", "build": "vite build", "lint": "npx eslint .", "preview": "vite preview" diff --git a/packages/react-frontend/src/Components/Table.jsx b/packages/react-frontend/src/Components/Table.jsx index 264bde5..b1db279 100644 --- a/packages/react-frontend/src/Components/Table.jsx +++ b/packages/react-frontend/src/Components/Table.jsx @@ -31,7 +31,7 @@ function TableBody(props) { - + diff --git a/packages/react-frontend/src/Views/AddOrders.jsx b/packages/react-frontend/src/Views/AddOrders.jsx index 4c2eba1..e8ae935 100644 --- a/packages/react-frontend/src/Views/AddOrders.jsx +++ b/packages/react-frontend/src/Views/AddOrders.jsx @@ -76,13 +76,13 @@ function AddOrders() { } function fetchOrders() { - return fetch("https://safehaven2.azurewebsites.net/order-units", { + return fetch("https://safehavenapp.azurewebsites.net/order-units", { headers: addAuthHeader() }); } function postOrderUnit(order) { - return fetch("https://safehaven2.azurewebsites.net/order-units", { + return fetch("https://safehavenapp.azurewebsites.net/order-units", { method: "POST", headers: addAuthHeader({ "Content-Type": "application/json" @@ -92,7 +92,7 @@ function AddOrders() { } function deleteOrder(id) { - const uri = `https://safehaven2.azurewebsites.net/order-units/${id}`; + const uri = `https://safehavenapp.azurewebsites.net/order-units/${id}`; return fetch(uri, { method: "DELETE", headers: addAuthHeader({ @@ -102,7 +102,7 @@ function AddOrders() { } function runPost(order_str) { - return fetch("https://safehaven2.azurewebsites.net/orders", { + return fetch("https://safehavenapp.azurewebsites.net/orders", { method: "POST", headers: addAuthHeader({ "Content-Type": "application/json" diff --git a/packages/react-frontend/src/Views/EditProfile.jsx b/packages/react-frontend/src/Views/EditProfile.jsx index b3e766b..744efcc 100644 --- a/packages/react-frontend/src/Views/EditProfile.jsx +++ b/packages/react-frontend/src/Views/EditProfile.jsx @@ -8,7 +8,7 @@ import { useNavigate } from "react-router-dom"; function EditProfile() { const navigate = useNavigate(); function changeProfile(profile){ - fetch("https://safehaven2.azurewebsites.net/users/profile", { + fetch("https://safehavenapp.azurewebsites.net/users/profile", { method: "POST", headers: addAuthHeader({ "Content-Type": "application/json" diff --git a/packages/react-frontend/src/Views/Inventory.jsx b/packages/react-frontend/src/Views/Inventory.jsx index ecbe48e..f166377 100644 --- a/packages/react-frontend/src/Views/Inventory.jsx +++ b/packages/react-frontend/src/Views/Inventory.jsx @@ -71,7 +71,7 @@ function Inventory() { } function fetchProducts() { - const promise = fetch("https://safehaven2.azurewebsites.net/products", { + const promise = fetch("https://safehavenapp.azurewebsites.net/products", { headers: addAuthHeader() }); return promise; @@ -79,7 +79,7 @@ function Inventory() { function postProduct(product) { console.log("posting:", product); - return fetch("https://safehaven2.azurewebsites.net/products", { + return fetch("https://safehavenapp.azurewebsites.net/products", { method: "POST", headers: addAuthHeader({ "Content-Type": "application/json" @@ -89,7 +89,7 @@ function Inventory() { } function deleteProduct(id) { - const uri = `https://safehaven2.azurewebsites.net/products/${id}`; + const uri = `https://safehavenapp.azurewebsites.net/products/${id}`; return fetch(uri, { method: "DELETE", headers: addAuthHeader({ diff --git a/packages/react-frontend/src/Views/LoginPage.jsx b/packages/react-frontend/src/Views/LoginPage.jsx index 06814cf..823af14 100644 --- a/packages/react-frontend/src/Views/LoginPage.jsx +++ b/packages/react-frontend/src/Views/LoginPage.jsx @@ -9,7 +9,7 @@ function Login() { const navigate = useNavigate(); function authenticateUser(user) { console.log(user); - fetch("https://safehaven2.azurewebsites.net/login", { + fetch("https://safehavenapp.azurewebsites.net/login", { method: "POST", headers: { "Content-Type": "application/json" diff --git a/packages/react-frontend/src/Views/ManageOrders.jsx b/packages/react-frontend/src/Views/ManageOrders.jsx index 1c29873..36c53de 100644 --- a/packages/react-frontend/src/Views/ManageOrders.jsx +++ b/packages/react-frontend/src/Views/ManageOrders.jsx @@ -42,14 +42,14 @@ function ManageOrders() { }; function fetchOrders() { - return fetch("https://safehaven2.azurewebsites.net/orders", { + return fetch("https://safehavenapp.azurewebsites.net/orders", { headers: addAuthHeader() }); } function fetchSearch(search) { search = "search=" + search; - const uri = `https://safehaven2.azurewebsites.net/orders/?${search}`; + const uri = `https://safehavenapp.azurewebsites.net/orders/?${search}`; console.log(uri); return fetch(uri, { headers: addAuthHeader() @@ -57,7 +57,7 @@ function ManageOrders() { } function deleteOrder(id) { - const uri = `https://safehaven2.azurewebsites.net/orders/${id}`; + const uri = `https://safehavenapp.azurewebsites.net/orders/${id}`; return fetch(uri, { method: "DELETE", headers: addAuthHeader({ diff --git a/packages/react-frontend/src/Views/OrderStatistics.jsx b/packages/react-frontend/src/Views/OrderStatistics.jsx index e1dfd0d..46e5a3f 100644 --- a/packages/react-frontend/src/Views/OrderStatistics.jsx +++ b/packages/react-frontend/src/Views/OrderStatistics.jsx @@ -64,13 +64,13 @@ function Inventory() { } function fetchProducts() { - return fetch("https://safehaven2.azurewebsites.net/products", { + return fetch("https://safehavenapp.azurewebsites.net/products", { headers: addAuthHeader() }); } function postProduct(product) { - return fetch("https://safehaven2.azurewebsites.net/products", { + return fetch("https://safehavenapp.azurewebsites.net/products", { method: "POST", headers: addAuthHeader({ "Content-Type": "application/json" @@ -80,7 +80,7 @@ function Inventory() { } function deleteProduct(id) { - const uri = `https://safehaven2.azurewebsites.net/products/${id}`; + const uri = `https://safehavenapp.azurewebsites.net/products/${id}`; return fetch(uri, { method: "DELETE", headers: addAuthHeader({ diff --git a/packages/react-frontend/src/Views/ProductPage.jsx b/packages/react-frontend/src/Views/ProductPage.jsx index 84760c2..7a1bb82 100644 --- a/packages/react-frontend/src/Views/ProductPage.jsx +++ b/packages/react-frontend/src/Views/ProductPage.jsx @@ -33,7 +33,7 @@ function ProductPage() { function fetchProduct(id) { console.log(`Fetching product with id: ${id}`); - return fetch(`https://safehaven2.azurewebsites.net/products/${id}`, { + return fetch(`https://safehavenapp.azurewebsites.net/products/${id}`, { headers: addAuthHeader() }) .then((res) => { @@ -76,7 +76,7 @@ function ProductPage() { function patchProduct(product) { console.log("patching: ", product); - return fetch(`https://safehaven2.azurewebsites.net/products/${id}`, { + return fetch(`https://safehavenapp.azurewebsites.net/products/${id}`, { method: "PATCH", headers: addAuthHeader({ "Content-Type": "application/json" diff --git a/packages/react-frontend/src/Views/Profile.jsx b/packages/react-frontend/src/Views/Profile.jsx index 8e31f8c..b2ce5dd 100644 --- a/packages/react-frontend/src/Views/Profile.jsx +++ b/packages/react-frontend/src/Views/Profile.jsx @@ -13,19 +13,19 @@ function Profile() { useEffect(() => { async function getUser() { - const user_details = await axios.get(`https://safehaven2.azurewebsites.net/users`, { + const user_details = await axios.get(`https://safehavenapp.azurewebsites.net/users`, { headers: addAuthHeader() }); const user = user_details.data; setUser(user); - const imageUrl = `https://safehaven2.azurewebsites.net/profile-picture/${user.profilePicture}`; + const imageUrl = `https://safehavenapp.azurewebsites.net/profile-picture/${user.profilePicture}`; setProfilePicture(imageUrl); } async function fetchProfilePicture() { try { - const imageUrl = `https://safehaven2.azurewebsites.net/profile-picture/${user.profilePicture}`; + const imageUrl = `https://safehavenapp.azurewebsites.net/profile-picture/${user.profilePicture}`; setProfilePicture(imageUrl); } catch (error) { console.error("Failed to load user profile picture", error); @@ -49,14 +49,14 @@ function Profile() { try { // Send the file to the server for processing and storage in MongoDB - const response = await axios.post("https://safehaven2.azurewebsites.net/profile-picture", formData, { + const response = await axios.post("https://safehavenapp.azurewebsites.net/profile-picture", formData, { headers: addAuthHeader({ "Content-Type": "multipart/form-data" }) }); console.log(response); - const imageUrl = `https://safehaven2.azurewebsites.net/profile-picture/${response.data}`; + const imageUrl = `https://safehavenapp.azurewebsites.net/profile-picture/${response.data}`; setProfilePicture(imageUrl); @@ -68,7 +68,7 @@ function Profile() { }; function deleteProfile() { - fetch("https://safehaven2.azurewebsites.net/users", { + fetch("https://safehavenapp.azurewebsites.net/users", { method: "DELETE", headers: addAuthHeader({ "Content-Type": "application/json" diff --git a/packages/react-frontend/src/Views/SignUpPage.jsx b/packages/react-frontend/src/Views/SignUpPage.jsx index b1e63a1..2c23a06 100644 --- a/packages/react-frontend/src/Views/SignUpPage.jsx +++ b/packages/react-frontend/src/Views/SignUpPage.jsx @@ -8,7 +8,7 @@ function Signup() { const navigate = useNavigate(); function createUser(user) { console.log(user); - fetch("https://safehaven2.azurewebsites.net/users", { + fetch("https://safehavenapp.azurewebsites.net/users", { method: "POST", headers: { "Content-Type": "application/json"