-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PORT = 8000 | ||
URI = "mongodb://localhost:27017/BookStoreApp" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Book from "../model/book.model.js"; | ||
|
||
export const getBook= async (req, res)=>{ | ||
try { | ||
const book= await Book.find(); | ||
res.status(200).json(book); | ||
|
||
} catch (error) { | ||
console.log("Error", error); | ||
res.status(500).send(error); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import User from "../model/user.model.js"; | ||
import bcryptjs from "bcryptjs"; | ||
|
||
export const signup =async(req, res)=>{ | ||
try { | ||
const {fullname, email, password} = req.body; | ||
const user = await User.findOne({email}); | ||
if(user){ | ||
return res.status(400).json({message: "User already exists"}) | ||
} | ||
const hashPassword = await bcryptjs.hash(password, 10) | ||
const createUser = new User({ | ||
fullname: fullname, | ||
email: email, | ||
password: hashPassword, | ||
}) | ||
|
||
await createUser.save().then((user) =>{ | ||
res.status(201).json({message: "User created sucessfullly", user:{_id: createUser._id, fullname: createUser.fullname}}) | ||
}) | ||
|
||
} catch (error) { | ||
console.log("Error: " + error.message); | ||
|
||
} | ||
} | ||
// Best way to handle async use Promise instead of await | ||
|
||
|
||
export const login = async (req, res)=>{ | ||
try { | ||
const {email, password ,fullname} = req.body; | ||
|
||
const user = await User.findOne({email}); | ||
console.log("hey " + fullname); | ||
|
||
|
||
const isMatch = await bcryptjs.compare(password, user.password) | ||
if(!isMatch || !user){ | ||
return res.status(400).json({message: error.message}) | ||
} | ||
else{ | ||
res.status(200).json({message: "Login Sucessfully", user: { | ||
_id: user._id, | ||
fullname : user.fullname, | ||
email: user.email, | ||
|
||
}}) | ||
} | ||
} catch (error) { | ||
console.log("Error: "+ error.message); | ||
res.status(500).json({message:" Invalid username or password"}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import express from "express" | ||
import dotenv from "dotenv" | ||
import mongoose from "mongoose" | ||
import cors from "cors"; | ||
|
||
import bookRoute from "./routes/book.route.js"; | ||
import userRoute from "./routes/user.route.js" | ||
|
||
const app = express(); | ||
app.use(cors()); | ||
app.use(express.json()); | ||
|
||
|
||
dotenv.config(); | ||
|
||
|
||
const PORT = process.env.PORT || 8001; | ||
const URI = process.env.URI; | ||
app.get("/", (req, res)=>{ | ||
res.send("Hello Banger!") | ||
}); | ||
|
||
// Connect to MongoDb | ||
|
||
|
||
try { | ||
|
||
mongoose | ||
.connect(URI) | ||
.then(()=>{ | ||
console.log(`Connected to Database`); | ||
app.listen(PORT, console.log(`Server Connected at ${PORT}`)) | ||
}) | ||
} catch (error) { | ||
console.log(error); | ||
|
||
} | ||
|
||
app.use("/book", bookRoute) | ||
app.use("/user", userRoute ) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import mongoose from "mongoose"; | ||
const bookSchema = mongoose.Schema({ | ||
name: String, | ||
price: Number, | ||
category: String, | ||
image: String, | ||
title: String, | ||
}) | ||
|
||
//Book is a collection when data is come | ||
// schema me jis feild me data aayega wo book Collection me store ho jayea | ||
|
||
const Book = mongoose.model("Book", bookSchema); | ||
|
||
export default Book; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const useSchema = mongoose.Schema({ | ||
fullname:{ | ||
type:String, | ||
require: true | ||
}, | ||
email:{ | ||
type:String, | ||
require: true, | ||
unique: true | ||
}, | ||
password:{ | ||
type:String, | ||
require: true | ||
} | ||
|
||
}) | ||
|
||
const User = mongoose.model("User", useSchema); | ||
|
||
export default User; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.