diff --git a/models/userModel.js b/models/userModel.js index 1cfe846..123a3d3 100644 --- a/models/userModel.js +++ b/models/userModel.js @@ -2,26 +2,26 @@ import mongoose from "mongoose"; const userSchema = mongoose.Schema( { - userDetails: { - name: { - type: String, - required: true, - unique: true, - }, - email: { - type: String, - required: true, - }, - password: { - type: String, - required: true, - }, + name: { + type: String, + required: true, + unique: true, + }, + email: { + type: String, + required: true, }, - circleInfo: { - user: [ - { type: mongoose.Schema.Types.ObjectId, required: true, ref: "User" }, - ], + password: { + type: String, + required: true, }, + + circle: [ + { + type: mongoose.Schema.Types.ObjectId, + required: false, + }, + ], // medicalDocs: { // id: // } @@ -30,3 +30,6 @@ const userSchema = mongoose.Schema( timestamps: true, } ); + +const User = mongoose.model("User", userSchema); +export default User; diff --git a/server.js b/server.js index 2aee70f..d28fa7d 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,9 @@ import express from "express"; import dotenv from "dotenv"; import connectDB from "./config/db.js"; +import User from "./models/userModel.js"; +import Circle from "./models/circleModel.js"; +import asyncHandler from "express-async-handler"; dotenv.config(); @@ -8,12 +11,77 @@ connectDB(); const app = express(); +app.use(express.json()); + app.get("/", (req, res) => { res.send("API is running!"); }); +app.get( + "/api/user", + asyncHandler(async (req, res) => { + const user = await User.find({}); + res.json(user); + }) +); + +app.post( + "/api/user", + asyncHandler(async (req, res) => { + const { name, email, password, email2 } = req.body; + + const user2 = await User.findOne({ email: email2 }); + + const user = await User.create({ + name, + email, + password, + circle: [ + { + _id: user2._id, + name: user2.name, + email: user2.email, + }, + ], + }); + + res.json({ + _id: user._id, + name: user.name, + email: user.email, + circle: user.circle, + }); + }) +); + +// app.post( +// "/api/circle", +// asyncHandler(async (req, res) => { +// const { email } = req.body; + +// const user = await User.findOne({ email }) +// .populate("circles") +// .exec((err, circles) => { +// console.log("Populated User" + circles); +// }); +// }) +// ); + const PORT = process.env.PORT || 5000; app.listen( PORT, console.log(`Server running on ${PORT} in ${process.env.NODE_ENV}`) ); + +// const user = await User.create({ +// name, +// email, +// password, +// }); + +// res.json({ +// _id: user._id, +// name: user.name, +// email: user.email, +// circle: user.circle, +// });