-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored schemas and folder structure
- Loading branch information
1 parent
a3d4d09
commit 366b103
Showing
26 changed files
with
391 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
*node_modules* | ||
*.env* | ||
*node_modules | ||
*.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
PORT= | ||
MONGODB_URI= | ||
CLIENT_URL= | ||
|
||
USER_ACCESS_TOKEN_SECRET= | ||
USER_ACCESS_TOKEN_EXPIRY= | ||
ADMIN_ACCESS_TOKEN_SECRET= | ||
ADMIN_ACCESS_TOKEN_EXPIRY= | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
import cookieParser from "cookie-parser"; | ||
|
||
const app = express(); | ||
|
||
app.use( | ||
cors({ | ||
origin: process.env.CLIENT_URL, | ||
credentials: true, | ||
}) | ||
); | ||
|
||
app.use( | ||
express.json({ | ||
limit: "150mb", | ||
}) | ||
); | ||
|
||
app.use( | ||
express.urlencoded({ | ||
extended: true, | ||
limit: "150mb", | ||
}) | ||
); | ||
|
||
app.use(cookieParser()); | ||
|
||
app.get("/", (req, res) => { | ||
res.json({ message: "Welcome to LearnSpace API" }); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import mongoose from "mongoose"; | ||
|
||
export const connectDB = async () => { | ||
try { | ||
await mongoose.connect(process.env.MONGODB_URI); | ||
console.log("Connected to MongoDB"); | ||
} catch (error) { | ||
console.log("MongoDB connection failed", error.message); | ||
process.exit(1); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import dotenv from "dotenv"; | ||
import { connectDB } from "./db/index.js"; | ||
import app from "./app.js"; | ||
|
||
dotenv.config({ | ||
path: "./.env", | ||
}); | ||
|
||
connectDB() | ||
.then(() => { | ||
const PORT = process.env.PORT || 5000; | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server running on port ${PORT}`); | ||
}); | ||
}) | ||
.catch((error) => console.log("MongoDB connection failed", error)); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import mongoose, { Schema } from "mongoose"; | ||
import validator from "validator"; | ||
import bcrypt from "bcrypt"; | ||
import jwt from "jsonwebtoken"; | ||
|
||
const adminSchema = new Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: [true, "Please enter your name"], | ||
trim: true, | ||
min: [3, "Your name should be atleast 3 characters long"], | ||
max: [30, "Your name cannot exceed 30 characters"], | ||
}, | ||
email: { | ||
type: String, | ||
unique: true, | ||
required: [true, "Please enter your email"], | ||
validate: { | ||
validator: function (value) { | ||
return /^[\w.-]+@vvit\.net$/.test(value); | ||
}, | ||
message: | ||
"Please enter a valid college email address (e.g., [email protected])", | ||
}, | ||
max: [50, "Your email cannot exceed 50 characters"], | ||
}, | ||
password: { | ||
type: String, | ||
required: [true, "Please enter your password"], | ||
min: [6, "Your password should be atleast 6 characters long"], | ||
select: false, | ||
}, | ||
contact: { | ||
type: String, | ||
unique: true, | ||
required: [true, "Please enter your contact number"], | ||
validate: { | ||
validator: function (value) { | ||
return /^[6-9]\d{9}$/.test(value); | ||
}, | ||
message: "Please enter a valid 10 digit contact number", | ||
}, | ||
}, | ||
avatar: { | ||
type: String, | ||
required: true, | ||
default: "https://learn-space.s3.ap-south-1.amazonaws.com/male.png", | ||
validate: { | ||
validator: validator.isURL, | ||
message: "Invalid URL", | ||
}, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
adminSchema.pre("save", async function (next) { | ||
if (!this.isModified("password")) return next(); | ||
|
||
this.password = await bcrypt.hash(this.password, 10); | ||
|
||
next(); | ||
}); | ||
|
||
adminSchema.methods.isPasswordCorrect = async function (password) { | ||
return await bcrypt.compare(password, this.password); | ||
}; | ||
|
||
adminSchema.methods.generateAccessToken = function () { | ||
return jwt.sign( | ||
{ | ||
_id: this._id, | ||
role: "admin", | ||
}, | ||
process.env.ADMIN_ACCESS_TOKEN_SECRET, | ||
{ | ||
expiresIn: process.env.ADMIN_ACCESS_TOKEN_EXPIRY, | ||
} | ||
); | ||
}; | ||
|
||
const Admin = mongoose.model("Admin", adminSchema); | ||
|
||
export default Admin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.