From cfb058357a6d286d16a3a0e97958f253a4305d21 Mon Sep 17 00:00:00 2001 From: UdeeshaPrabhashana Date: Fri, 2 Feb 2024 12:01:39 +0530 Subject: [PATCH] Add session restrion --- backend/dist/Controllers/sessionReg.js | 2 + backend/dist/Controllers/team.js | 123 +++++++++++++++++++ backend/dist/Model/session.js | 12 +- backend/dist/Model/team.js | 156 +++++++++++++++++++++++++ backend/dist/Routes/sessionReg.js | 2 +- backend/dist/Routes/team.js | 12 ++ backend/dist/index.js | 19 ++- backend/src/Routes/sessionReg.ts | 2 +- 8 files changed, 319 insertions(+), 9 deletions(-) create mode 100644 backend/dist/Controllers/team.js create mode 100644 backend/dist/Model/team.js create mode 100644 backend/dist/Routes/team.js diff --git a/backend/dist/Controllers/sessionReg.js b/backend/dist/Controllers/sessionReg.js index dadba28..8b2560c 100644 --- a/backend/dist/Controllers/sessionReg.js +++ b/backend/dist/Controllers/sessionReg.js @@ -23,6 +23,8 @@ const register = (req, res) => __awaiter(void 0, void 0, void 0, function* () { number: req.body.number, university: req.body.university, year: req.body.year, + platform: req.body.platform, + design: req.body.design, }); yield newSession.save(); res.status(200).json({ diff --git a/backend/dist/Controllers/team.js b/backend/dist/Controllers/team.js new file mode 100644 index 0000000..8d20242 --- /dev/null +++ b/backend/dist/Controllers/team.js @@ -0,0 +1,123 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.getTeams = exports.count = exports.register = void 0; +const team_1 = __importDefault(require("../Model/team")); +const dotenv_1 = __importDefault(require("dotenv")); +dotenv_1.default.config(); +const AUTH_KEY = process.env.AUTH_KEY || ""; +const expectTeamCount = 65; +const teamCount = () => __awaiter(void 0, void 0, void 0, function* () { + try { + const count = yield team_1.default.countDocuments({}); + console.log("Number of teams:", count); + return count; + } + catch (err) { + console.error('Error counting teams:', err); + throw err; + } +}); +const register = (req, res) => __awaiter(void 0, void 0, void 0, function* () { + // count teams and return if 50 teams are already registered + const count = yield teamCount(); + if (count >= expectTeamCount) { + res.status(200).json({ + success: false, + message: "Registration is currently closed because the maximum number of teams has been reached.", + }); + return; + } + else { + try { + const newSession = new team_1.default({ + teamName: req.body.teamName, + university: req.body.university, + other: req.body.other, + leaderName: req.body.leaderName, + leaderYear: req.body.leaderYear, + leaderWhatsapp: req.body.leaderWhatsapp, + leaderEmail: req.body.leaderEmail, + leaderNIC: req.body.leaderNIC, + member1Name: req.body.member1Name, + member1Year: req.body.member1Year, + member1Whatsapp: req.body.member1Whatsapp, + member1Email: req.body.member1Email, + member1NIC: req.body.member1NIC, + member2Name: req.body.member2Name, + member2Year: req.body.member2Year, + member2Whatsapp: req.body.member2Whatsapp, + member2Email: req.body.member2Email, + member2NIC: req.body.member2NIC, + member3Name: req.body.member3Name, + member3Year: req.body.member3Year, + member3Whatsapp: req.body.member3Whatsapp, + member3Email: req.body.member3Email, + member3NIC: req.body.member3NIC, + }); + yield newSession.save(); + res.status(200).json({ + success: true, + message: "Registered successfully", + data: newSession, + }); + } + catch (err) { + res.status(500).json({ + success: false, + message: "Registration failed", + data: err, + }); + console.log(err); + } + } +}); +exports.register = register; +const count = (req, res) => { + team_1.default.countDocuments({}).then(count => { + console.log("Number of teams:", count); + res.status(200).json({ teamCount: expectTeamCount - count }); + }).catch(err => { + console.error('Error counting teams:', err); + res.status(500).send('Error counting teams'); + }); +}; +exports.count = count; +// get teams , autherization key is required +const getTeams = (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const authKey = req.headers.authorization; + if (authKey !== AUTH_KEY) { + res.status(401).json({ + success: false, + message: "Unauthorized", + }); + return; + } + try { + const teams = yield team_1.default.find({}); + res.status(200).json({ + success: true, + message: "Teams fetched successfully", + data: teams, + }); + } + catch (err) { + res.status(500).json({ + success: false, + message: "Error fetching teams", + data: err, + }); + } +}); +exports.getTeams = getTeams; diff --git a/backend/dist/Model/session.js b/backend/dist/Model/session.js index 767d450..70b5eff 100644 --- a/backend/dist/Model/session.js +++ b/backend/dist/Model/session.js @@ -24,7 +24,15 @@ const sessionSchema = new mongoose_1.default.Schema({ year: { type: String, required: true - } + }, + platform: { + type: String, + required: true + }, + design: { + type: String, + required: true + }, }); // interface ISession extends Document { // email: string; @@ -33,4 +41,4 @@ const sessionSchema = new mongoose_1.default.Schema({ // university: string; // year: string; // } -exports.default = mongoose_1.default.model('Session', sessionSchema); +exports.default = mongoose_1.default.model('AppDesginSession', sessionSchema); diff --git a/backend/dist/Model/team.js b/backend/dist/Model/team.js new file mode 100644 index 0000000..67ca1d3 --- /dev/null +++ b/backend/dist/Model/team.js @@ -0,0 +1,156 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const mongoose_1 = __importDefault(require("mongoose")); +// const sessionSchema = new mongoose.Schema({ +// email: { +// type: String, +// required: true +// }, +// name: { +// type: String, +// required: true +// }, +// number:{ +// type: String, +// required: true +// }, +// university:{ +// type: String, +// required: true +// }, +// year:{ +// type: String, +// required: true +// }, +// platform:{ +// type: String, +// required: true +// }, +// design:{ +// type: String, +// required: true +// }, +// }); +// //{ +// teamName: 'Megane Frami', +// university: 'University of Ruhuna', +// other: 'Culpa sapiente animi provident quod voluptas.', +// leaderName: 'Josue.Crooks', +// leaderYear: '2nd year', +// leaderWhatsapp: '369-359-1569', +// leaderEmail: 'your.email+fakedata32244@gmail.com', +// leaderNIC: 'Facere hic eius laboriosam sint cumque rerum ad cum.', +// member1Name: 'Caitlyn45', +// member1Year: '4th year', +// member1Whatsapp: '685-528-3764', +// member1Email: 'your.email+fakedata16577@gmail.com', +// member1NIC: '540', +// member2Name: 'Sally4', +// member2Year: '2nd year', +// member2Whatsapp: '007-837-0355', +// member2Email: 'your.email+fakedata26192@gmail.com', +// member2NIC: '13', +// member3Name: 'Lula.Bradtke', +// member3Year: '3rd year', +// member3Whatsapp: '971-866-6326', +// member3Email: 'your.email+fakedata19068@gmail.com', +// member3NIC: '522' +// } +const teamSchema = new mongoose_1.default.Schema({ + teamName: { + type: String, + required: true + }, + university: { + type: String, + required: true + }, + other: { + type: String, + required: false + }, + leaderName: { + type: String, + required: true + }, + leaderYear: { + type: String, + required: true + }, + leaderWhatsapp: { + type: String, + required: true + }, + leaderEmail: { + type: String, + required: true + }, + leaderNIC: { + type: String, + required: true + }, + member1Name: { + type: String, + required: true + }, + member1Year: { + type: String, + required: true + }, + member1Whatsapp: { + type: String, + required: true + }, + member1Email: { + type: String, + required: true + }, + member1NIC: { + type: String, + required: true + }, + member2Name: { + type: String, + required: true + }, + member2Year: { + type: String, + required: true + }, + member2Whatsapp: { + type: String, + required: true + }, + member2Email: { + type: String, + required: true + }, + member2NIC: { + type: String, + required: true + }, + member3Name: { + type: String, + required: false + }, + member3Year: { + type: String, + required: false + }, + member3Whatsapp: { + type: String, + required: false + }, + member3Email: { + type: String, + required: false + }, + member3NIC: { + type: String, + required: false + }, +}); +exports.default = mongoose_1.default.model('Team', teamSchema); diff --git a/backend/dist/Routes/sessionReg.js b/backend/dist/Routes/sessionReg.js index 3b8d41f..70528b4 100644 --- a/backend/dist/Routes/sessionReg.js +++ b/backend/dist/Routes/sessionReg.js @@ -6,5 +6,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = __importDefault(require("express")); const sessionReg_1 = require("../Controllers/sessionReg"); const router = express_1.default.Router(); -router.post("/", sessionReg_1.register); +router.post("/register", sessionReg_1.register); exports.default = router; diff --git a/backend/dist/Routes/team.js b/backend/dist/Routes/team.js new file mode 100644 index 0000000..9febf80 --- /dev/null +++ b/backend/dist/Routes/team.js @@ -0,0 +1,12 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const express_1 = __importDefault(require("express")); +const team_1 = require("../Controllers/team"); +const router = express_1.default.Router(); +router.post("/register", team_1.register); +router.get("/count", team_1.count); +router.get("/getTeams", team_1.getTeams); +exports.default = router; diff --git a/backend/dist/index.js b/backend/dist/index.js index d1d2950..9ecabb2 100644 --- a/backend/dist/index.js +++ b/backend/dist/index.js @@ -16,13 +16,17 @@ const express_1 = __importDefault(require("express")); const cors_1 = __importDefault(require("cors")); const mongoose_1 = __importDefault(require("mongoose")); const dotenv_1 = __importDefault(require("dotenv")); +const team_1 = __importDefault(require("./Routes/team")); const sessionReg_1 = __importDefault(require("./Routes/sessionReg")); dotenv_1.default.config(); -const URL = process.env.MONGO || ""; +const URL = process.env.MONGO || "mongodb://localhost:27017/"; +const ORIGIN = process.env.ORIGIN || "http://localhost:3000"; +const PORT = process.env.PORT || 4000; +const AUTH_KEY = process.env.AUTH_KEY || "123456"; const app = (0, express_1.default)(); app.use(express_1.default.json()); app.use((0, cors_1.default)({ - origin: "http://localhost:3000", + origin: ORIGIN, methods: ["GET", "POST", "PUT", "DELETE"] })); // app.post("/api/register", (req: Request, res: Response) => { @@ -33,7 +37,12 @@ app.use((0, cors_1.default)({ // console.log(response); // res.send(response); // }); -app.use("/api/register", sessionReg_1.default); +// Api is running +app.get("/", (req, res) => { + res.send("MADHACK API is running"); +}); +app.use("/api/team", team_1.default); +app.use("/api/session", sessionReg_1.default); const connect = () => __awaiter(void 0, void 0, void 0, function* () { try { yield mongoose_1.default.connect(URL); @@ -46,7 +55,7 @@ const connect = () => __awaiter(void 0, void 0, void 0, function* () { mongoose_1.default.connection.on("disconnected", () => { console.log("mongoDB disconnected!"); }); -app.listen(4011, () => { +app.listen(PORT, () => { connect(); - console.log("Server running on port 4011"); + console.log("Server running on port " + PORT); }); diff --git a/backend/src/Routes/sessionReg.ts b/backend/src/Routes/sessionReg.ts index f084206..7920f98 100644 --- a/backend/src/Routes/sessionReg.ts +++ b/backend/src/Routes/sessionReg.ts @@ -3,6 +3,6 @@ import { register } from "../Controllers/sessionReg"; const router = express.Router(); -router.post("/session", register); +router.post("/register", register); export default router;