Skip to content

Commit

Permalink
refactored schemas and folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
shaikahmadnawaz committed Feb 4, 2024
1 parent a3d4d09 commit 366b103
Show file tree
Hide file tree
Showing 26 changed files with 391 additions and 213 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*node_modules*
*.env*
*node_modules
*.env
9 changes: 9 additions & 0 deletions server/.env.example
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=

6 changes: 0 additions & 6 deletions server/db/connectDB.js

This file was deleted.

31 changes: 0 additions & 31 deletions server/models/Review.js

This file was deleted.

100 changes: 0 additions & 100 deletions server/models/User.js

This file was deleted.

21 changes: 21 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
"name": "server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js"
"start": "node src/index.js",
"dev": "nodemon src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand Down
23 changes: 0 additions & 23 deletions server/server.js

This file was deleted.

33 changes: 33 additions & 0 deletions server/src/app.js
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;
11 changes: 11 additions & 0 deletions server/src/db/index.js
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);
}
};
17 changes: 17 additions & 0 deletions server/src/index.js
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.
87 changes: 87 additions & 0 deletions server/src/models/admin.model.js
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;
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ const discussionSchema = new mongoose.Schema(
// foreignField: "_id",
// });

export default mongoose.model("Discussion", discussionSchema);
const Discussion = mongoose.model("Discussion", discussionSchema);

export default Discussion;
Loading

0 comments on commit 366b103

Please sign in to comment.