Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implemented post api for user details #238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
URI="mongodb://localhost:27017/carbonops"
URI="mongodb://127.0.0.1:27017/carbonops"
31 changes: 16 additions & 15 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
const express = require('express');
const app = express()
const bodyParser = require('body-parser')
const connectDB = require('./config/db.config');
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const connectDB = require("./config/db.config");
const cors = require("cors");
const UseRouter = require("./routers/example.routes")
const UseRouter = require("./routers/example.routes");

connectDB();
app.use(express.json());
app.use(bodyParser.json())
app.use(cors())
app.use("/ping",UseRouter)
app.use(bodyParser.json());
app.use(cors());
app.use("/ping", UseRouter);

app.get('/',(req,res) => {
res.send('hello world')
app.get("/", (req, res) => {
res.send("hello world");
});

app.use("/users", require("./routers/userDetails"));
app.use(express.json());
app.use("/ping",UseRouter)
app.listen(3000,()=>{
console.log('server is running');

})
app.use("/ping", UseRouter);
app.listen(3000, () => {
console.log("server is running");
});
37 changes: 37 additions & 0 deletions backend/controllers/userDetails.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const UserModel = require("../models/allUsers");
const bcrypt = require("bcryptjs");

const userDetailsController = async (req, res) => {
try {
const { role, email, contact, dob, password } = req.body;

if (!role || !email || !contact || !dob || !password) {
res.status(400).json({ message: "All fields are required!" });
} else {
const existingUser = await UserModel.findOne({ email });

if (existingUser) {
res.status(409).json({ message: "User already exists!" });
} else {
const hashedPassword = await bcrypt.hash(password, 10);

const newUser = new UserModel({
role,
email,
contact,
dob,
password: hashedPassword,
});

const savedUser = await newUser.save();
res
.status(201)
.json({ message: "User created successfully!", user: savedUser });
}
}
} catch (error) {
console.log(error);
}
};

module.exports = { userDetailsController };
53 changes: 27 additions & 26 deletions backend/controllers/userOperations.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
const dbMethods = require("../config/DBMethods")
const bcrypt = require("bcrypt");
const dbMethods = require("../config/DBMethods");
const bcrypt = require("bcryptjs");
const salt = 8;
const createUser = async (req,res)=>{
const {email, contact, dob, password, role} = req.body;
const createUser = async (req, res) => {
const { email, contact, dob, password, role } = req.body;

try{
try {
const isUserExist = await dbMethods.findUserByEmail(email);
if(isUserExist.length > 0){
return res.status(404).json({error: "User Already Exists."});
}
bcrypt.hash(password, salt, async (err, hashedPassword)=>{
if(!err){
const data = {email, password: hashedPassword, contact, role, dob}
if (isUserExist.length > 0) {
return res.status(404).json({ error: "User Already Exists." });
}
bcrypt.hash(password, salt, async (err, hashedPassword) => {
if (!err) {
const data = { email, password: hashedPassword, contact, role, dob };
const user = await dbMethods.createUser(data);
console.log(user)
if(user.status === "error"){
return res.status(404).json({error: "Something went wrong!"})
console.log(user);
if (user.status === "error") {
return res.status(404).json({ error: "Something went wrong!" });
}
return res.status(200).json({message: "User created successfully", user})
}else{
return res.status(404).json({error: "Something went wrong!"})
return res
.status(200)
.json({ message: "User created successfully", user });
} else {
return res.status(404).json({ error: "Something went wrong!" });
}
})
}catch(err){
return res.status(500).json({error: "Something went wrong!"})
}

}
});
} catch (err) {
return res.status(500).json({ error: "Something went wrong!" });
}
};

const userControllers = {
createUser
}
createUser,
};

module.exports= userControllers;
module.exports = userControllers;
12 changes: 6 additions & 6 deletions backend/models/allUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ const mongoose = require("mongoose");
const signupSchema = mongoose.Schema({
role: {
type: String,
require: true
require: true,
},
email: {
type: String,
require: true
require: true,
},
contact: {
type: String,
require: true,
},
dob: {
type: Date,
require: true
type: String,
require: true,
},
password: {
type: String,
require: true,
}
})
},
});

const userModel = mongoose.model("Users", signupSchema);

Expand Down
Loading
Loading