Skip to content

Commit

Permalink
Finished User model with circle functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
abhudaym committed Feb 27, 2021
1 parent 9d3cd53 commit 4186708
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 18 deletions.
39 changes: 21 additions & 18 deletions models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// }
Expand All @@ -30,3 +30,6 @@ const userSchema = mongoose.Schema(
timestamps: true,
}
);

const User = mongoose.model("User", userSchema);
export default User;
68 changes: 68 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,87 @@
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();

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,
// });

0 comments on commit 4186708

Please sign in to comment.