From 79589afb2bc83cb2d2cdf53c481eb40087b943b5 Mon Sep 17 00:00:00 2001 From: charlotteconze <85513920+charlotteconze@users.noreply.github.com> Date: Thu, 24 Oct 2024 15:03:59 -0500 Subject: [PATCH] adding backend logic --- api/src/config/{dbConnect.ts => db.ts} | 0 api/src/controllers/userController.ts | 0 api/src/index.ts | 36 ----------------------- api/src/routes/index.ts | 40 ++++++++++++++++++++++++++ api/src/routes/user.ts | 33 +++++++++++++++++++++ 5 files changed, 73 insertions(+), 36 deletions(-) rename api/src/config/{dbConnect.ts => db.ts} (100%) create mode 100644 api/src/controllers/userController.ts delete mode 100644 api/src/index.ts create mode 100644 api/src/routes/index.ts create mode 100644 api/src/routes/user.ts diff --git a/api/src/config/dbConnect.ts b/api/src/config/db.ts similarity index 100% rename from api/src/config/dbConnect.ts rename to api/src/config/db.ts diff --git a/api/src/controllers/userController.ts b/api/src/controllers/userController.ts new file mode 100644 index 0000000..e69de29 diff --git a/api/src/index.ts b/api/src/index.ts deleted file mode 100644 index 438c44c..0000000 --- a/api/src/index.ts +++ /dev/null @@ -1,36 +0,0 @@ -import express, { Request, Response, Application } from "express" -import cors from "cors" -import dbConnect from "./config/dbConnect" -import mongoose from "mongoose" - -import { Group, IGroup } from "./model/Group" -import { User, IUser } from "./model/User" -import { Expense } from "./model/Expense" -import { populate } from "dotenv" - -dbConnect() - -const app: Application = express() -app.use(cors()) -app.use(express.json()) // To parse JSON bodies -const port = process.env.PORT || 8000 - -// Test POST endpoint -app.post("/test", (req: Request, res: Response) => { - console.log(req.body) // Log the request body to check what you are receiving - - const { key } = req.body // Extract "key" from the request body - if (!key) { - return res.status(400).json({ message: "No key provided" }) - } - - return res.status(200).json({ - message: "Data received successfully", - receivedKey: key, - }) -}) - -// Start the server -app.listen(port, () => { - console.log(`Server running at http://localhost:${port}`) -}) diff --git a/api/src/routes/index.ts b/api/src/routes/index.ts new file mode 100644 index 0000000..b6bf883 --- /dev/null +++ b/api/src/routes/index.ts @@ -0,0 +1,40 @@ +import user from "./user" + +export { user } + +// import express, { Request, Response, Application } from "express" +// import cors from "cors" +// import dbConnect from "./config/dbConnect" +// import mongoose from "mongoose" + +// import { Group, IGroup } from "./model/Group" +// import { User, IUser } from "./model/User" +// import { Expense } from "./model/Expense" +// import { populate } from "dotenv" + +// dbConnect() + +// const app: Application = express() +// app.use(cors()) +// app.use(express.json()) // To parse JSON bodies +// const port = process.env.PORT || 8000 + +// // Test POST endpoint +// app.post("/test", (req: Request, res: Response) => { +// console.log(req.body) // Log the request body to check what you are receiving + +// const { key } = req.body // Extract "key" from the request body +// if (!key) { +// return res.status(400).json({ message: "No key provided" }) +// } + +// return res.status(200).json({ +// message: "Data received successfully", +// receivedKey: key, +// }) +// }) + +// // Start the server +// app.listen(port, () => { +// console.log(`Server running at http://localhost:${port}`) +// }) diff --git a/api/src/routes/user.ts b/api/src/routes/user.ts new file mode 100644 index 0000000..1c8b311 --- /dev/null +++ b/api/src/routes/user.ts @@ -0,0 +1,33 @@ +import express from "express" + +const router = express.Router() +const axios = require("axios").default + +router.post("/create-group", async (req, res) => { + const { groupName, groupMembers } = req.body + + console.log("Received group data:", req.body) + + // Validate request body + if (!groupName || !groupMembers) { + return res.status(400).json({ + message: "Invalid data. Please provide group name and group members.", + }) + } + + // Log the received data for debugging + console.log("Received group data:", { + groupName, + groupMembers, + }) + + // Create a new group + const response = await axios.post("http://localhost:3001/api/group", { + groupName, + groupMembers, + }) + + return res.status(response.status).json(response.data) +}) + +export default router