From 23361644cbf32533a5cc20c6a5d1b213aaa7a400 Mon Sep 17 00:00:00 2001 From: naimyasirozcan Date: Mon, 17 Nov 2025 17:48:32 +0100 Subject: [PATCH 1/8] endpoints created --- server/app.js | 20 +++ server/package-lock.json | 349 +-------------------------------------- server/package.json | 7 +- 3 files changed, 24 insertions(+), 352 deletions(-) diff --git a/server/app.js b/server/app.js index dfe51f49..06323f50 100644 --- a/server/app.js +++ b/server/app.js @@ -7,6 +7,10 @@ const PORT = 5005; // Devs Team - Import the provided files with JSON data of students and cohorts here: // ... +// data import / assignment +const cohorts = require("./cohorts.json") +const students = require("./students.json") + // INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express const app = express(); @@ -15,6 +19,12 @@ const app = express(); // MIDDLEWARE // Research Team - Set up CORS middleware here: // ... + +app.use( + cors({ + origin: "http://localhost:5173" + }) +) app.use(express.json()); app.use(morgan("dev")); app.use(express.static("public")); @@ -29,6 +39,16 @@ app.get("/docs", (req, res) => { res.sendFile(__dirname + "/views/docs.html"); }); +// data-endopoints + +app.get("/api/students", (req, res) => { + res.json(students) +}) + +app.get("/api/cohorts", (req, res) => { + res.json(cohorts) +}) + // START SERVER app.listen(PORT, () => { diff --git a/server/package-lock.json b/server/package-lock.json index 23e2f78a..dff9603f 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -11,15 +11,9 @@ "dependencies": { "cookie-parser": "^1.4.6", "express": "^4.18.2", - "morgan": "^1.10.0", - "nodemon": "^3.0.1" + "morgan": "^1.10.0" } }, - "node_modules/abbrev": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", - "integrity": "sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==" - }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -32,28 +26,11 @@ "node": ">= 0.6" } }, - "node_modules/anymatch": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", - "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", - "dependencies": { - "normalize-path": "^3.0.0", - "picomatch": "^2.0.4" - }, - "engines": { - "node": ">= 8" - } - }, "node_modules/array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, - "node_modules/balanced-match": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" - }, "node_modules/basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -70,14 +47,6 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, - "node_modules/binary-extensions": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", - "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", - "engines": { - "node": ">=8" - } - }, "node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", @@ -101,26 +70,6 @@ "npm": "1.2.8000 || >= 1.4.16" } }, - "node_modules/brace-expansion": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", - "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dependencies": { - "balanced-match": "^1.0.0", - "concat-map": "0.0.1" - } - }, - "node_modules/braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", - "dependencies": { - "fill-range": "^7.0.1" - }, - "engines": { - "node": ">=8" - } - }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -141,37 +90,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/chokidar": { - "version": "3.5.3", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", - "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", - "funding": [ - { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - ], - "dependencies": { - "anymatch": "~3.1.2", - "braces": "~3.0.2", - "glob-parent": "~5.1.2", - "is-binary-path": "~2.1.0", - "is-glob": "~4.0.1", - "normalize-path": "~3.0.0", - "readdirp": "~3.6.0" - }, - "engines": { - "node": ">= 8.10.0" - }, - "optionalDependencies": { - "fsevents": "~2.3.2" - } - }, - "node_modules/concat-map": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" - }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -316,17 +234,6 @@ "node": ">= 0.6" } }, - "node_modules/fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", - "dependencies": { - "to-regex-range": "^5.0.1" - }, - "engines": { - "node": ">=8" - } - }, "node_modules/finalhandler": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.2.0.tgz", @@ -360,19 +267,6 @@ "node": ">= 0.6" } }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -392,17 +286,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/glob-parent": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", - "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", - "dependencies": { - "is-glob": "^4.0.1" - }, - "engines": { - "node": ">= 6" - } - }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -414,14 +297,6 @@ "node": ">= 0.4.0" } }, - "node_modules/has-flag": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha512-sKJf1+ceQBr4SMkvQnBDNDtf4TXpVhVGateu0t918bl30FnbE2m4vNLX+VWe/dpjlb+HugGYzW7uQXH98HPEYw==", - "engines": { - "node": ">=4" - } - }, "node_modules/has-proto": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", @@ -470,11 +345,6 @@ "node": ">=0.10.0" } }, - "node_modules/ignore-by-default": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz", - "integrity": "sha512-Ius2VYcGNk7T90CppJqcIkS5ooHUZyIQK+ClZfMfMNFEF9VSE73Fq+906u/CWu92x4gzZMWOwfFYckPObzdEbA==" - }, "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", @@ -488,55 +358,6 @@ "node": ">= 0.10" } }, - "node_modules/is-binary-path": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", - "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", - "dependencies": { - "binary-extensions": "^2.0.0" - }, - "engines": { - "node": ">=8" - } - }, - "node_modules/is-extglob": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", - "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/is-glob": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", - "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", - "dependencies": { - "is-extglob": "^2.1.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/is-number": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", - "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", - "engines": { - "node": ">=0.12.0" - } - }, - "node_modules/lru-cache": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", - "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "dependencies": { - "yallist": "^4.0.0" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -588,17 +409,6 @@ "node": ">= 0.6" } }, - "node_modules/minimatch": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", - "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", - "dependencies": { - "brace-expansion": "^1.1.7" - }, - "engines": { - "node": "*" - } - }, "node_modules/morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -638,68 +448,6 @@ "node": ">= 0.6" } }, - "node_modules/nodemon": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", - "integrity": "sha512-g9AZ7HmkhQkqXkRc20w+ZfQ73cHLbE8hnPbtaFbFtCumZsjyMhKk9LajQ07U5Ux28lvFjZ5X7HvWR1xzU8jHVw==", - "dependencies": { - "chokidar": "^3.5.2", - "debug": "^3.2.7", - "ignore-by-default": "^1.0.1", - "minimatch": "^3.1.2", - "pstree.remy": "^1.1.8", - "semver": "^7.5.3", - "simple-update-notifier": "^2.0.0", - "supports-color": "^5.5.0", - "touch": "^3.1.0", - "undefsafe": "^2.0.5" - }, - "bin": { - "nodemon": "bin/nodemon.js" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/nodemon" - } - }, - "node_modules/nodemon/node_modules/debug": { - "version": "3.2.7", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz", - "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==", - "dependencies": { - "ms": "^2.1.1" - } - }, - "node_modules/nodemon/node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" - }, - "node_modules/nopt": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/nopt/-/nopt-1.0.10.tgz", - "integrity": "sha512-NWmpvLSqUrgrAC9HCuxEvb+PSloHpqVu+FqcO4eeF2h5qYRhA7ev6KvelyQAKtegUbC6RypJnlEOhd8vloNKYg==", - "dependencies": { - "abbrev": "1" - }, - "bin": { - "nopt": "bin/nopt.js" - }, - "engines": { - "node": "*" - } - }, - "node_modules/normalize-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", @@ -740,17 +488,6 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha512-5DFkuoqlv1uYQKxy8omFBeJPQcdoE07Kv2sferDCrAq1ohOU+MSDswDIbnx3YAM60qIOnYa53wBhXW0EbMonrQ==" }, - "node_modules/picomatch": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", - "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", - "engines": { - "node": ">=8.6" - }, - "funding": { - "url": "https://github.com/sponsors/jonschlinkert" - } - }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -763,11 +500,6 @@ "node": ">= 0.10" } }, - "node_modules/pstree.remy": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", - "integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==" - }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -804,17 +536,6 @@ "node": ">= 0.8" } }, - "node_modules/readdirp": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", - "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", - "dependencies": { - "picomatch": "^2.2.1" - }, - "engines": { - "node": ">=8.10.0" - } - }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -839,20 +560,6 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, - "node_modules/semver": { - "version": "7.5.4", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", - "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dependencies": { - "lru-cache": "^6.0.0" - }, - "bin": { - "semver": "bin/semver.js" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/send": { "version": "0.18.0", "resolved": "https://registry.npmjs.org/send/-/send-0.18.0.tgz", @@ -913,17 +620,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/simple-update-notifier": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/simple-update-notifier/-/simple-update-notifier-2.0.0.tgz", - "integrity": "sha512-a2B9Y0KlNXl9u/vsW6sTIu9vGEpfKu2wRV6l1H3XEas/0gUIzGzBoP/IouTcUQbm9JWZLH3COxyn03TYlFax6w==", - "dependencies": { - "semver": "^7.5.3" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -932,28 +628,6 @@ "node": ">= 0.8" } }, - "node_modules/supports-color": { - "version": "5.5.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", - "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", - "dependencies": { - "has-flag": "^3.0.0" - }, - "engines": { - "node": ">=4" - } - }, - "node_modules/to-regex-range": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", - "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", - "dependencies": { - "is-number": "^7.0.0" - }, - "engines": { - "node": ">=8.0" - } - }, "node_modules/toidentifier": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz", @@ -962,17 +636,6 @@ "node": ">=0.6" } }, - "node_modules/touch": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/touch/-/touch-3.1.0.tgz", - "integrity": "sha512-WBx8Uy5TLtOSRtIq+M03/sKDrXCLHxwDcquSP2c43Le03/9serjQBIztjRz6FkJez9D/hleyAXTBGLwwZUw9lA==", - "dependencies": { - "nopt": "~1.0.10" - }, - "bin": { - "nodetouch": "bin/nodetouch.js" - } - }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -985,11 +648,6 @@ "node": ">= 0.6" } }, - "node_modules/undefsafe": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", - "integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==" - }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -1013,11 +671,6 @@ "engines": { "node": ">= 0.8" } - }, - "node_modules/yallist": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } } diff --git a/server/package.json b/server/package.json index 7aa0847c..ff1dff4a 100644 --- a/server/package.json +++ b/server/package.json @@ -4,8 +4,8 @@ "description": "ExpressJS API for the Cohort Tools app", "main": "app.js", "scripts": { - "dev": "nodemon app.js", - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node app.js", + "dev": "node --watch app.js" }, "repository": { "type": "git", @@ -20,7 +20,6 @@ "dependencies": { "cookie-parser": "^1.4.6", "express": "^4.18.2", - "morgan": "^1.10.0", - "nodemon": "^3.0.1" + "morgan": "^1.10.0" } } From 4200752701199519c8629c9de502b8ee752861ba Mon Sep 17 00:00:00 2001 From: naimyasirozcan Date: Mon, 17 Nov 2025 17:56:56 +0100 Subject: [PATCH 2/8] first days CORSassignment done with the team. routes created, packs installed --- server/app.js | 2 ++ server/package-lock.json | 23 +++++++++++++++++++++++ server/package.json | 1 + 3 files changed, 26 insertions(+) diff --git a/server/app.js b/server/app.js index 06323f50..cbc0ef36 100644 --- a/server/app.js +++ b/server/app.js @@ -10,6 +10,7 @@ const PORT = 5005; // data import / assignment const cohorts = require("./cohorts.json") const students = require("./students.json") +const cors = require("cors") // INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express @@ -25,6 +26,7 @@ app.use( origin: "http://localhost:5173" }) ) + app.use(express.json()); app.use(morgan("dev")); app.use(express.static("public")); diff --git a/server/package-lock.json b/server/package-lock.json index dff9603f..e0989be6 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0" } @@ -134,6 +135,19 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "license": "MIT", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -448,6 +462,15 @@ "node": ">= 0.6" } }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/object-inspect": { "version": "1.12.3", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz", diff --git a/server/package.json b/server/package.json index ff1dff4a..2d9d1f18 100644 --- a/server/package.json +++ b/server/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/ironhack-labs/cohort-tools-project#readme", "dependencies": { "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "express": "^4.18.2", "morgan": "^1.10.0" } From cd68027e00a63e1cbbda7545205ad498161e99c5 Mon Sep 17 00:00:00 2001 From: Mateo Sancho Date: Tue, 18 Nov 2025 18:26:15 +0100 Subject: [PATCH 3/8] Day 2 --- server/app.js | 33 ++++- server/cohorts.json | 57 -------- server/models/Cohort.model.js | 21 +++ server/models/Student.model.js | 22 +++ server/package-lock.json | 235 +++++++++++++++++++++++++++++++++ server/package.json | 1 + server/students.json | 86 ------------ 7 files changed, 308 insertions(+), 147 deletions(-) delete mode 100644 server/cohorts.json create mode 100644 server/models/Cohort.model.js create mode 100644 server/models/Student.model.js delete mode 100644 server/students.json diff --git a/server/app.js b/server/app.js index cbc0ef36..c75157f4 100644 --- a/server/app.js +++ b/server/app.js @@ -2,16 +2,25 @@ const express = require("express"); const morgan = require("morgan"); const cookieParser = require("cookie-parser"); const PORT = 5005; +const mongoose = require("mongoose") +const Cohort = require("./models/Cohort.model") +const Student = require("./models/Student.model") // STATIC DATA // Devs Team - Import the provided files with JSON data of students and cohorts here: // ... // data import / assignment -const cohorts = require("./cohorts.json") -const students = require("./students.json") const cors = require("cors") +mongoose +.connect("mongodb://127.0.0.1:27017/cohort-tools-api") +.then(() => { + console.log("data base connected") +}) +.catch((error) => { + console.log(error) +}) // INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express const app = express(); @@ -44,11 +53,27 @@ app.get("/docs", (req, res) => { // data-endopoints app.get("/api/students", (req, res) => { - res.json(students) + Student.find({}) + .then((students) => { + console.log("Retrieved students: ", students) + res.json(students) + }) + .catch((error) => { + console.log(error) + res.status(500).json({error: "failed to retreive students"}) + }) }) app.get("/api/cohorts", (req, res) => { - res.json(cohorts) + Cohort.find({}) + .then((cohorts) => { + console.log("Retrieved cohorts: ", cohorts) + res.json(cohorts) + }) + .catch((error) => { + console.log(error) + res.status(500).json({error: "failed to retreive cohorts"}) + }) }) diff --git a/server/cohorts.json b/server/cohorts.json deleted file mode 100644 index 3c2bef26..00000000 --- a/server/cohorts.json +++ /dev/null @@ -1,57 +0,0 @@ -[ - { - "_id": 1, - "inProgress": false, - "cohortSlug": "ft-wd-paris-2023-07-03", - "cohortName": "FT WD PARIS 2023 07", - "program": "Web Dev", - "campus": "Paris", - "startDate": "2023-07-03T00:00:00.000Z", - "endDate": "2023-09-08T00:00:00.000Z", - "programManager": "Sally Daher", - "leadTeacher": "Florian Aube", - "totalHours": 360 - }, - { - "_id": 2, - "cohortSlug": "pt-ux-berlin-2023-02-06", - "cohortName": "PT UX BERLIN 2023 02", - "program": "UX/UI", - "format": "Part Time", - "campus": "BERLIN", - "startDate": "2023-02-06T00:00:00.000Z", - "endDate": "2023-08-06T00:00:00.000Z", - "inProgress": false, - "programManager": "Alice Williams", - "leadTeacher": "Bob Johnson", - "totalHours": 360 - }, - { - "_id": 3, - "cohortSlug": "ft-da-miami-2023-03-06", - "cohortName": "FT DA MIAMI 2023 03", - "program": "Data Analytics", - "format": "Full Time", - "campus": "Miami", - "startDate": "2023-03-06T00:00:00.000Z", - "endDate": "2023-05-15T00:00:00.000Z", - "inProgress": true, - "programManager": "Charlie Brown", - "leadTeacher": "Eva Edwards", - "totalHours": 360 - }, - { - "_id": 4, - "cohortSlug": "pt-cy-paris-2023-04-03", - "cohortName": "PT CY PARIS 2023 04", - "program": "Cybersecurity", - "format": "Part Time", - "campus": "Paris", - "startDate": "2023-04-03T00:00:00.000Z", - "endDate": "2023-10-03T00:00:00.000Z", - "inProgress": false, - "programManager": "Eva Edwards", - "leadTeacher": "Frank Foster", - "totalHours": 360 - } -] diff --git a/server/models/Cohort.model.js b/server/models/Cohort.model.js new file mode 100644 index 00000000..1f7015ca --- /dev/null +++ b/server/models/Cohort.model.js @@ -0,0 +1,21 @@ +const mongoose = require("mongoose") + +const Schema = mongoose.Schema + +const cohortSchema = new Schema({ + _id: String, + inProgress: {type: Boolean, default: false}, + cohortSlug: String, + cohortName: String, + program: String, + campus: String, + startDate: String, + endDate: String, + programManager: String, + leadTeacher: String, + totalHours: Number +}) + +const Cohort = mongoose.model("Cohort", cohortSchema) + +module.exports = Cohort diff --git a/server/models/Student.model.js b/server/models/Student.model.js new file mode 100644 index 00000000..9be826c1 --- /dev/null +++ b/server/models/Student.model.js @@ -0,0 +1,22 @@ +const mongoose = require("mongoose") + +const Schema = mongoose.Schema + +const studentSchema = new Schema({ + _id: String, + firstName: String, + lastName: String, + email: String, + phone: String, + linkedinUrl: String, + languages: [String], + program: String, + background: String, + image: String, + projects: [String], + cohort: String, +}) + +const Student = mongoose.model("Student", studentSchema) + +module.exports = Student diff --git a/server/package-lock.json b/server/package-lock.json index e0989be6..bc04ec39 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -12,9 +12,34 @@ "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", + "mongoose": "^8.20.0", "morgan": "^1.10.0" } }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.3.2.tgz", + "integrity": "sha512-QgA5AySqB27cGTXBFmnpifAi7HxoGUeezwo6p9dI03MuDB6Pp33zgclqVb6oVK3j6I9Vesg0+oojW2XxB59SGg==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, + "node_modules/@types/webidl-conversions": { + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==", + "license": "MIT" + }, + "node_modules/@types/whatwg-url": { + "version": "11.0.5", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz", + "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==", + "license": "MIT", + "dependencies": { + "@types/webidl-conversions": "*" + } + }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -71,6 +96,15 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/bson": { + "version": "6.10.4", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.10.4.tgz", + "integrity": "sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==", + "license": "Apache-2.0", + "engines": { + "node": ">=16.20.1" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -372,6 +406,15 @@ "node": ">= 0.10" } }, + "node_modules/kareem": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz", + "integrity": "sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==", + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -380,6 +423,12 @@ "node": ">= 0.6" } }, + "node_modules/memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "license": "MIT" + }, "node_modules/merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -423,6 +472,90 @@ "node": ">= 0.6" } }, + "node_modules/mongodb": { + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.20.0.tgz", + "integrity": "sha512-Tl6MEIU3K4Rq3TSHd+sZQqRBoGlFsOgNrH5ltAcFBV62Re3Fd+FcaVf8uSEQFOJ51SDowDVttBTONMfoYWrWlQ==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.3.0", + "bson": "^6.10.4", + "mongodb-connection-string-url": "^3.0.2" + }, + "engines": { + "node": ">=16.20.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0 || ^2.0.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.3.2", + "socks": "^2.7.1" + }, + "peerDependenciesMeta": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { + "optional": true + }, + "gcp-metadata": { + "optional": true + }, + "kerberos": { + "optional": true + }, + "mongodb-client-encryption": { + "optional": true + }, + "snappy": { + "optional": true + }, + "socks": { + "optional": true + } + } + }, + "node_modules/mongodb-connection-string-url": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.2.tgz", + "integrity": "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^14.1.0 || ^13.0.0" + } + }, + "node_modules/mongoose": { + "version": "8.20.0", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.20.0.tgz", + "integrity": "sha512-SxqNb8yx+VOjIOx2l7HqkGvYuLC/T85d+jPvqGDdUbKJFz/5PVSsVxQzypQsX7chenYvq5bd8jIr4LtunedE7g==", + "license": "MIT", + "dependencies": { + "bson": "^6.10.4", + "kareem": "2.6.3", + "mongodb": "~6.20.0", + "mpath": "0.9.0", + "mquery": "5.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "engines": { + "node": ">=16.20.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mongoose" + } + }, + "node_modules/mongoose/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, "node_modules/morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -449,6 +582,50 @@ "node": ">= 0.8" } }, + "node_modules/mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/mquery": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz", + "integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==", + "license": "MIT", + "dependencies": { + "debug": "4.x" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/mquery/node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/mquery/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -523,6 +700,15 @@ "node": ">= 0.10" } }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -643,6 +829,21 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/sift": { + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz", + "integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==", + "license": "MIT" + }, + "node_modules/sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "license": "MIT", + "dependencies": { + "memory-pager": "^1.0.2" + } + }, "node_modules/statuses": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", @@ -659,6 +860,18 @@ "node": ">=0.6" } }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", @@ -694,6 +907,28 @@ "engines": { "node": ">= 0.8" } + }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } } } } diff --git a/server/package.json b/server/package.json index 2d9d1f18..beedaa3a 100644 --- a/server/package.json +++ b/server/package.json @@ -21,6 +21,7 @@ "cookie-parser": "^1.4.6", "cors": "^2.8.5", "express": "^4.18.2", + "mongoose": "^8.20.0", "morgan": "^1.10.0" } } diff --git a/server/students.json b/server/students.json deleted file mode 100644 index ffd7e34e..00000000 --- a/server/students.json +++ /dev/null @@ -1,86 +0,0 @@ -[ - { - "_id": 1, - "firstName": "Christine", - "lastName": "Clayton", - "email": "christine.clayton@example.com", - "phone": "567-890-1234", - "linkedinUrl": "https://linkedin.com/in/christineclaytonexample", - "languages": ["English", "Dutch"], - "program": "Web Dev", - "background": "Computer Engineering", - "image": "https://i.imgur.com/r8bo8u7.png", - "cohort": 1, - "projects": [] - }, - { - "_id": 2, - "firstName": "Grace", - "lastName": "Green", - "email": "grace.green@example.com", - "phone": "901-234-5678", - "linkedinUrl": "https://linkedin.com/in/gracegreenexample", - "languages": ["English", "Portuguese"], - "program": "Web Dev", - "background": "Software Development", - "image": "https://i.imgur.com/r8bo8u7.png", - "cohort": 1, - "projects": [] - }, - { - "_id": 3, - "firstName": "Jack", - "lastName": "Johnson", - "email": "jack.johnson@example.com", - "phone": "234-567-8901", - "linkedinUrl": "https://linkedin.com/in/jackjohnsonexample", - "languages": ["English", "French"], - "program": "Web Dev", - "background": "System Administration", - "image": "https://i.imgur.com/r8bo8u7.png", - "cohort": 1, - "projects": [] - }, - { - "_id": 4, - "firstName": "John", - "lastName": "Doe", - "email": "john.doe@example.com", - "phone": "123-456-7890", - "linkedinUrl": "https://linkedin.com/in/johndoeexample", - "languages": ["English", "Spanish"], - "program": "UX/UI", - "background": "Computer Science", - "image": "https://i.imgur.com/r8bo8u7.png", - "cohort": 2, - "projects": [] - }, - { - "_id": 5, - "firstName": "Katie", - "lastName": "King", - "email": "katie.king@example.com", - "phone": "345-678-9012", - "linkedinUrl": "https://linkedin.com/in/katiekingexample", - "languages": ["English", "German"], - "program": "UX/UI", - "background": "Information Systems", - "image": "https://i.imgur.com/r8bo8u7.png", - "cohort": 2, - "projects": [] - }, - { - "_id": 6, - "firstName": "Irene", - "lastName": "Iverson", - "email": "irene.iverson@example.com", - "phone": "123-456-7890", - "linkedinUrl": "https://linkedin.com/in/ireneiversonexample", - "languages": ["English", "Spanish"], - "program": "Data Analytics", - "background": "Economics", - "image": "https://i.imgur.com/r8bo8u7.png", - "cohort": 3, - "projects": [] - } -] From ab950d1bfcbca76a07a868df0bdec73cdb30832b Mon Sep 17 00:00:00 2001 From: Igor Montovani Date: Wed, 19 Nov 2025 09:27:28 +0100 Subject: [PATCH 4/8] delete _id from models because we don't need --- package-lock.json | 6 ++++++ server/app.js | 10 +++++----- server/models/Cohort.model.js | 1 - server/models/Student.model.js | 3 +-- 4 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..68060633 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "cohort-tools-project", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/server/app.js b/server/app.js index c75157f4..c1b9f6c0 100644 --- a/server/app.js +++ b/server/app.js @@ -14,7 +14,7 @@ const Student = require("./models/Student.model") const cors = require("cors") mongoose -.connect("mongodb://127.0.0.1:27017/cohort-tools-api") +.connect("mongodb://127.0.0.1:27017/cohort-tools-api") // 127.0.0.1 same as localhost .then(() => { console.log("data base connected") }) @@ -40,7 +40,7 @@ app.use(express.json()); app.use(morgan("dev")); app.use(express.static("public")); app.use(express.urlencoded({ extended: false })); -app.use(cookieParser()); +app.use(cookieParser()); // Not used in this project. // ROUTES - https://expressjs.com/en/starter/basic-routing.html @@ -53,7 +53,7 @@ app.get("/docs", (req, res) => { // data-endopoints app.get("/api/students", (req, res) => { - Student.find({}) + Student.find() .then((students) => { console.log("Retrieved students: ", students) res.json(students) @@ -65,7 +65,7 @@ app.get("/api/students", (req, res) => { }) app.get("/api/cohorts", (req, res) => { - Cohort.find({}) + Cohort.find() .then((cohorts) => { console.log("Retrieved cohorts: ", cohorts) res.json(cohorts) @@ -80,4 +80,4 @@ app.get("/api/cohorts", (req, res) => { // START SERVER app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); -}); \ No newline at end of file +}); diff --git a/server/models/Cohort.model.js b/server/models/Cohort.model.js index 1f7015ca..2a13110c 100644 --- a/server/models/Cohort.model.js +++ b/server/models/Cohort.model.js @@ -3,7 +3,6 @@ const mongoose = require("mongoose") const Schema = mongoose.Schema const cohortSchema = new Schema({ - _id: String, inProgress: {type: Boolean, default: false}, cohortSlug: String, cohortName: String, diff --git a/server/models/Student.model.js b/server/models/Student.model.js index 9be826c1..5bd68e0c 100644 --- a/server/models/Student.model.js +++ b/server/models/Student.model.js @@ -3,7 +3,6 @@ const mongoose = require("mongoose") const Schema = mongoose.Schema const studentSchema = new Schema({ - _id: String, firstName: String, lastName: String, email: String, @@ -14,7 +13,7 @@ const studentSchema = new Schema({ background: String, image: String, projects: [String], - cohort: String, + cohort: String, // Change this based in the lecture today. }) const Student = mongoose.model("Student", studentSchema) From 13f888707278bac9251cf358bde87d61b1c2c070 Mon Sep 17 00:00:00 2001 From: Igor Montovani Date: Wed, 19 Nov 2025 17:39:53 +0100 Subject: [PATCH 5/8] Add the routes for Students and changed Models with the ObjectId --- server/app.js | 102 ++++++++++++++++++++++++++++++--- server/models/Student.model.js | 5 +- 2 files changed, 98 insertions(+), 9 deletions(-) diff --git a/server/app.js b/server/app.js index c1b9f6c0..d7bbe711 100644 --- a/server/app.js +++ b/server/app.js @@ -52,8 +52,27 @@ app.get("/docs", (req, res) => { // data-endopoints +//Cohort Routes + +app.get("/api/cohorts", (req, res) => { + Cohort.find() + .then((cohorts) => { + console.log("Retrieved cohorts: ", cohorts) + res.json(cohorts) + }) + .catch((error) => { + console.log(error) + res.status(500).json({error: "failed to retreive cohorts"}) + }) +}) + + + +// Student Routes + app.get("/api/students", (req, res) => { - Student.find() + Student.find(req.query) + .populate('cohort') .then((students) => { console.log("Retrieved students: ", students) res.json(students) @@ -64,18 +83,85 @@ app.get("/api/students", (req, res) => { }) }) -app.get("/api/cohorts", (req, res) => { - Cohort.find() - .then((cohorts) => { - console.log("Retrieved cohorts: ", cohorts) - res.json(cohorts) +app.post('/api/students',(req,res)=>{ + console.log('received request: ') + Student.create({ + firstName: req.body.firstName, + lastName: req.body.lastName, + email: req.body.email, + phone: req.body.phone, + linkedinUrl: req.body.linkedinUrl, + languages: req.body.languages, + program: req.body.program, + background: req.body.background, + image: req.body.image, + projects: req.body.projects, + cohort: req.body.cohort }) - .catch((error) => { + .then(()=>{ + res.send('Student created sucessfully') + }) + .catch((error)=>{ + console.log(error) + }) + +}) + +app.get('/api/students/cohort/:cohortId', (req,res)=>{ + console.log(req.params.cohortId) + Student.find({cohort: req.params.cohortId}) + .then((students)=>{ + console.log(students) + res.json(students) + }) + .catch((error)=>{ + console.log(error) + }) +}) + +app.get('/api/students/:studentId',(req,res)=>{ + Student.find({_id: req.params.studentId}) + .populate('cohort') + .then((student)=>{ + res.json(student) + res.send('success') + }) + .catch((error)=>{ console.log(error) - res.status(500).json({error: "failed to retreive cohorts"}) }) }) +app.put('/api/students/:studentId', (req,res)=>{ + Student.findByIdAndUpdate(req.params.studentId,{ + firstName: req.body.firstName, + lastName: req.body.lastName, + email: req.body.email, + phone: req.body.phone, + linkedinUrl: req.body.linkedinUrl, + languages: req.body.languages, + program: req.body.program, + background: req.body.background, + image: req.body.image, + projects: req.body.projects, + cohort: req.body.cohort + }) + .then(()=>{ + res.send('Document updated') + }) + .catch((error)=>{ + console.log(error) + }) +}) + +app.delete('/api/students/:studentId',(req,res)=>{ + Student.findByIdAndDelete(req.params.studentId) + .then(()=>{ + res.send('Document deleted') + }) + .catch((error)=>{ + console.log(error) + }) +}) // START SERVER app.listen(PORT, () => { diff --git a/server/models/Student.model.js b/server/models/Student.model.js index 5bd68e0c..4b841563 100644 --- a/server/models/Student.model.js +++ b/server/models/Student.model.js @@ -13,7 +13,10 @@ const studentSchema = new Schema({ background: String, image: String, projects: [String], - cohort: String, // Change this based in the lecture today. + cohort: { + type: mongoose.Schema.Types.ObjectId, + ref: 'Cohort' + } }) const Student = mongoose.model("Student", studentSchema) From 700a4cf75ff15730ba22fde4ca1fc1f4a6de5e4c Mon Sep 17 00:00:00 2001 From: Igor Montovani Date: Wed, 19 Nov 2025 18:15:44 +0100 Subject: [PATCH 6/8] Add all Cohort routes --- server/app.js | 244 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 161 insertions(+), 83 deletions(-) diff --git a/server/app.js b/server/app.js index d7bbe711..9d73a900 100644 --- a/server/app.js +++ b/server/app.js @@ -2,39 +2,38 @@ const express = require("express"); const morgan = require("morgan"); const cookieParser = require("cookie-parser"); const PORT = 5005; -const mongoose = require("mongoose") -const Cohort = require("./models/Cohort.model") -const Student = require("./models/Student.model") +const mongoose = require("mongoose"); +const Cohort = require("./models/Cohort.model"); +const Student = require("./models/Student.model"); // STATIC DATA // Devs Team - Import the provided files with JSON data of students and cohorts here: // ... -// data import / assignment -const cors = require("cors") +// data import / assignment +const cors = require("cors"); mongoose -.connect("mongodb://127.0.0.1:27017/cohort-tools-api") // 127.0.0.1 same as localhost -.then(() => { - console.log("data base connected") -}) -.catch((error) => { - console.log(error) -}) + .connect("mongodb://127.0.0.1:27017/cohort-tools-api") // 127.0.0.1 same as localhost + .then(() => { + console.log("data base connected"); + }) + .catch((error) => { + console.log(error); + }); // INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express const app = express(); - // MIDDLEWARE // Research Team - Set up CORS middleware here: // ... app.use( cors({ - origin: "http://localhost:5173" + origin: "http://localhost:5173", }) -) +); app.use(express.json()); app.use(morgan("dev")); @@ -42,7 +41,6 @@ app.use(express.static("public")); app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); // Not used in this project. - // ROUTES - https://expressjs.com/en/starter/basic-routing.html // Devs Team - Start working on the routes here: // ... @@ -52,39 +50,120 @@ app.get("/docs", (req, res) => { // data-endopoints -//Cohort Routes +// *** Cohort Routes *** + +// get all cohorts: app.get("/api/cohorts", (req, res) => { - Cohort.find() - .then((cohorts) => { - console.log("Retrieved cohorts: ", cohorts) - res.json(cohorts) + Cohort.find(req.query) + .then((cohorts) => { + console.log("Retrieved cohorts: ", cohorts); + res.json(cohorts); + }) + .catch((error) => { + console.log(error); + res.status(500).json({ error: "failed to retreive cohorts" }); + }); +}); + +// create cohort: + +app.post("/api/cohorts", (req, res) => { + console.log("received request: "); + Cohort.create({ + inProgress: req.body.inProgress, + cohortSlug: req.body.cohortSlug, + cohortName: req.body.cohortName, + program: req.body.program, + campus: req.body.campus, + startDate: req.body.startDate, + endDate: req.body.endDate, + programManager: req.body.programManager, + leadTeacher: req.body.leadTeacher, + totalHours: req.body.totalHours + }) + .then(() => { + res.send("Cohort created sucessfully"); + }) + .catch((error) => { + console.log(error); + }); +}); + +// get spesific cohort by id: + +app.get("/api/cohorts/:cohortId", (req, res) => { + console.log("get request recieved (single cohort)") + + Cohort.find({_id: req.params.cohortId}) + .then((cohort) => { + res.json(cohort) }) .catch((error) => { console.log(error) - res.status(500).json({error: "failed to retreive cohorts"}) }) + }) +// update spesific cohort by id: +app.put("/api/cohorts/:cohortId", (req, res) => { + console.log("update req received") + + Cohort.findByIdAndUpdate(req.params.cohortId, { + inProgress: req.body.inProgress, + cohortSlug: req.body.cohortSlug, + cohortName: req.body.cohortName, + program: req.body.program, + campus: req.body.campus, + startDate: req.body.startDate, + endDate: req.body.endDate, + programManager: req.body.programManager, + leadTeacher: req.body.leadTeacher, + totalHours: req.body.totalHours + }) + .then(() => { + res.send(`cohort with id: ${req.params.cohortId} has updated`) + }) + .catch((error) => { + console.log(error) + }) +}) -// Student Routes +// delete spesific cohort by id: +app.delete("/api/cohorts/:cohortId", (req, res) => { + console.log("delete request received") -app.get("/api/students", (req, res) => { - Student.find(req.query) - .populate('cohort') - .then((students) => { - console.log("Retrieved students: ", students) - res.json(students) + Cohort.findByIdAndDelete(req.params.cohortId) + .then(() => { + res.send(`the cohort (id: ${req.params.cohortId}) has deleted succesfully`) }) .catch((error) => { console.log(error) - res.status(500).json({error: "failed to retreive students"}) }) }) -app.post('/api/students',(req,res)=>{ - console.log('received request: ') +// *** Student Routes *** + +// get student: + +app.get("/api/students", (req, res) => { + Student.find(req.query) + .populate("cohort") + .then((students) => { + console.log("Retrieved students: ", students); + res.json(students); + }) + .catch((error) => { + console.log(error); + res.status(500).json({ error: "failed to retreive students" }); + }); +}); + +// create new student: + +app.post("/api/students", (req, res) => { + console.log("received request: "); Student.create({ firstName: req.body.firstName, lastName: req.body.lastName, @@ -96,43 +175,42 @@ app.post('/api/students',(req,res)=>{ background: req.body.background, image: req.body.image, projects: req.body.projects, - cohort: req.body.cohort - }) - .then(()=>{ - res.send('Student created sucessfully') - }) - .catch((error)=>{ - console.log(error) - }) - -}) + cohort: req.body.cohort, + }) + .then(() => { + res.send("Student created sucessfully"); + }) + .catch((error) => { + console.log(error); + }); +}); -app.get('/api/students/cohort/:cohortId', (req,res)=>{ - console.log(req.params.cohortId) - Student.find({cohort: req.params.cohortId}) - .then((students)=>{ - console.log(students) - res.json(students) - }) - .catch((error)=>{ - console.log(error) - }) -}) +app.get("/api/students/cohort/:cohortId", (req, res) => { + console.log(req.params.cohortId); + Student.find({ cohort: req.params.cohortId }) + .then((students) => { + console.log(students); + res.json(students); + }) + .catch((error) => { + console.log(error); + }); +}); -app.get('/api/students/:studentId',(req,res)=>{ - Student.find({_id: req.params.studentId}) - .populate('cohort') - .then((student)=>{ - res.json(student) - res.send('success') - }) - .catch((error)=>{ - console.log(error) - }) -}) +app.get("/api/students/:studentId", (req, res) => { + Student.find({ _id: req.params.studentId }) + .populate("cohort") + .then((student) => { + res.json(student); + res.send("success"); + }) + .catch((error) => { + console.log(error); + }); +}); -app.put('/api/students/:studentId', (req,res)=>{ - Student.findByIdAndUpdate(req.params.studentId,{ +app.put("/api/students/:studentId", (req, res) => { + Student.findByIdAndUpdate(req.params.studentId, { firstName: req.body.firstName, lastName: req.body.lastName, email: req.body.email, @@ -143,25 +221,25 @@ app.put('/api/students/:studentId', (req,res)=>{ background: req.body.background, image: req.body.image, projects: req.body.projects, - cohort: req.body.cohort - }) - .then(()=>{ - res.send('Document updated') - }) - .catch((error)=>{ - console.log(error) - }) -}) + cohort: req.body.cohort, + }) + .then(() => { + res.send("Document updated"); + }) + .catch((error) => { + console.log(error); + }); +}); -app.delete('/api/students/:studentId',(req,res)=>{ +app.delete("/api/students/:studentId", (req, res) => { Student.findByIdAndDelete(req.params.studentId) - .then(()=>{ - res.send('Document deleted') - }) - .catch((error)=>{ - console.log(error) - }) -}) + .then(() => { + res.send("Document deleted"); + }) + .catch((error) => { + console.log(error); + }); +}); // START SERVER app.listen(PORT, () => { From 2dc620aecf0d44b4966f975fee58301fc6ffc3b2 Mon Sep 17 00:00:00 2001 From: naimyasirozcan Date: Thu, 20 Nov 2025 17:04:30 +0100 Subject: [PATCH 7/8] files and routes created with the team --- server/app.js | 243 +++----------------------------- server/config/index.js | 17 +++ server/db/index.js | 12 ++ server/error-handlers/index.js | 0 server/routes/cohort.routes.js | 96 +++++++++++++ server/routes/index.routes.js | 9 ++ server/routes/student.routes.js | 101 +++++++++++++ 7 files changed, 253 insertions(+), 225 deletions(-) create mode 100644 server/config/index.js create mode 100644 server/db/index.js create mode 100644 server/error-handlers/index.js create mode 100644 server/routes/cohort.routes.js create mode 100644 server/routes/index.routes.js create mode 100644 server/routes/student.routes.js diff --git a/server/app.js b/server/app.js index 9d73a900..375f4eec 100644 --- a/server/app.js +++ b/server/app.js @@ -1,247 +1,40 @@ +try { + process.loadEnvFile() +} catch (error){ + console.log("can't access to .env file") +} + +const mongoose = require("./db") const express = require("express"); -const morgan = require("morgan"); -const cookieParser = require("cookie-parser"); -const PORT = 5005; -const mongoose = require("mongoose"); + const Cohort = require("./models/Cohort.model"); const Student = require("./models/Student.model"); -// STATIC DATA -// Devs Team - Import the provided files with JSON data of students and cohorts here: -// ... - // data import / assignment -const cors = require("cors"); - -mongoose - .connect("mongodb://127.0.0.1:27017/cohort-tools-api") // 127.0.0.1 same as localhost - .then(() => { - console.log("data base connected"); - }) - .catch((error) => { - console.log(error); - }); -// INITIALIZE EXPRESS APP - https://expressjs.com/en/4x/api.html#express const app = express(); -// MIDDLEWARE -// Research Team - Set up CORS middleware here: -// ... +// Midlleware Routes: -app.use( - cors({ - origin: "http://localhost:5173", - }) -); +const config = require("./config") +config(app) -app.use(express.json()); -app.use(morgan("dev")); -app.use(express.static("public")); -app.use(express.urlencoded({ extended: false })); -app.use(cookieParser()); // Not used in this project. +// *** Routes *** -// ROUTES - https://expressjs.com/en/starter/basic-routing.html -// Devs Team - Start working on the routes here: -// ... +// Initialization: app.get("/docs", (req, res) => { res.sendFile(__dirname + "/views/docs.html"); -}); - -// data-endopoints - -// *** Cohort Routes *** - -// get all cohorts: -app.get("/api/cohorts", (req, res) => { - Cohort.find(req.query) - .then((cohorts) => { - console.log("Retrieved cohorts: ", cohorts); - res.json(cohorts); - }) - .catch((error) => { - console.log(error); - res.status(500).json({ error: "failed to retreive cohorts" }); - }); }); -// create cohort: - -app.post("/api/cohorts", (req, res) => { - console.log("received request: "); - Cohort.create({ - inProgress: req.body.inProgress, - cohortSlug: req.body.cohortSlug, - cohortName: req.body.cohortName, - program: req.body.program, - campus: req.body.campus, - startDate: req.body.startDate, - endDate: req.body.endDate, - programManager: req.body.programManager, - leadTeacher: req.body.leadTeacher, - totalHours: req.body.totalHours - }) - .then(() => { - res.send("Cohort created sucessfully"); - }) - .catch((error) => { - console.log(error); - }); -}); - -// get spesific cohort by id: - -app.get("/api/cohorts/:cohortId", (req, res) => { - console.log("get request recieved (single cohort)") - - Cohort.find({_id: req.params.cohortId}) - .then((cohort) => { - res.json(cohort) - }) - .catch((error) => { - console.log(error) - }) - -}) - -// update spesific cohort by id: - -app.put("/api/cohorts/:cohortId", (req, res) => { - console.log("update req received") - - Cohort.findByIdAndUpdate(req.params.cohortId, { - inProgress: req.body.inProgress, - cohortSlug: req.body.cohortSlug, - cohortName: req.body.cohortName, - program: req.body.program, - campus: req.body.campus, - startDate: req.body.startDate, - endDate: req.body.endDate, - programManager: req.body.programManager, - leadTeacher: req.body.leadTeacher, - totalHours: req.body.totalHours - }) - .then(() => { - res.send(`cohort with id: ${req.params.cohortId} has updated`) - }) - .catch((error) => { - console.log(error) - }) -}) - -// delete spesific cohort by id: -app.delete("/api/cohorts/:cohortId", (req, res) => { - console.log("delete request received") +// Routes of Collections: +const indexRouter = require("./routes/index.routes") +app.use("/api", indexRouter) - Cohort.findByIdAndDelete(req.params.cohortId) - .then(() => { - res.send(`the cohort (id: ${req.params.cohortId}) has deleted succesfully`) - }) - .catch((error) => { - console.log(error) - }) -}) -// *** Student Routes *** - -// get student: - -app.get("/api/students", (req, res) => { - Student.find(req.query) - .populate("cohort") - .then((students) => { - console.log("Retrieved students: ", students); - res.json(students); - }) - .catch((error) => { - console.log(error); - res.status(500).json({ error: "failed to retreive students" }); - }); -}); - -// create new student: - -app.post("/api/students", (req, res) => { - console.log("received request: "); - Student.create({ - firstName: req.body.firstName, - lastName: req.body.lastName, - email: req.body.email, - phone: req.body.phone, - linkedinUrl: req.body.linkedinUrl, - languages: req.body.languages, - program: req.body.program, - background: req.body.background, - image: req.body.image, - projects: req.body.projects, - cohort: req.body.cohort, - }) - .then(() => { - res.send("Student created sucessfully"); - }) - .catch((error) => { - console.log(error); - }); -}); - -app.get("/api/students/cohort/:cohortId", (req, res) => { - console.log(req.params.cohortId); - Student.find({ cohort: req.params.cohortId }) - .then((students) => { - console.log(students); - res.json(students); - }) - .catch((error) => { - console.log(error); - }); -}); - -app.get("/api/students/:studentId", (req, res) => { - Student.find({ _id: req.params.studentId }) - .populate("cohort") - .then((student) => { - res.json(student); - res.send("success"); - }) - .catch((error) => { - console.log(error); - }); -}); - -app.put("/api/students/:studentId", (req, res) => { - Student.findByIdAndUpdate(req.params.studentId, { - firstName: req.body.firstName, - lastName: req.body.lastName, - email: req.body.email, - phone: req.body.phone, - linkedinUrl: req.body.linkedinUrl, - languages: req.body.languages, - program: req.body.program, - background: req.body.background, - image: req.body.image, - projects: req.body.projects, - cohort: req.body.cohort, - }) - .then(() => { - res.send("Document updated"); - }) - .catch((error) => { - console.log(error); - }); -}); - -app.delete("/api/students/:studentId", (req, res) => { - Student.findByIdAndDelete(req.params.studentId) - .then(() => { - res.send("Document deleted"); - }) - .catch((error) => { - console.log(error); - }); -}); +// Start Server: +const PORT = process.env.PORT || 5005 -// START SERVER app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); }); diff --git a/server/config/index.js b/server/config/index.js new file mode 100644 index 00000000..9f6d226d --- /dev/null +++ b/server/config/index.js @@ -0,0 +1,17 @@ +const morgan = require("morgan"); +const cookieParser = require("cookie-parser"); +const cors = require("cors"); +const express = require("express"); + +function config (app){ + +app.use(cors({origin: "http://localhost:5173",})); +app.use(express.json()); +app.use(morgan("dev")); +app.use(express.static("public")); +app.use(express.urlencoded({ extended: false })); +app.use(cookieParser()); + +} + +module.exports = config \ No newline at end of file diff --git a/server/db/index.js b/server/db/index.js new file mode 100644 index 00000000..22eef07e --- /dev/null +++ b/server/db/index.js @@ -0,0 +1,12 @@ +const mongoose = require("mongoose") + +mongoose + .connect("mongodb://127.0.0.1:27017/cohort-tools-api") // 127.0.0.1 same as localhost + .then(() => { + console.log("data base connected"); + }) + .catch((error) => { + console.log(error); + }); + + module.exports = mongoose \ No newline at end of file diff --git a/server/error-handlers/index.js b/server/error-handlers/index.js new file mode 100644 index 00000000..e69de29b diff --git a/server/routes/cohort.routes.js b/server/routes/cohort.routes.js new file mode 100644 index 00000000..be379b9c --- /dev/null +++ b/server/routes/cohort.routes.js @@ -0,0 +1,96 @@ +const Cohort = require("../models/Cohort.model") +const express = require("express") +const router = express.Router() + +// Gets all cohorts: + +router.get("/", (req, res) => { + Cohort.find(req.query) + .then((cohorts) => { + console.log("Retrieved cohorts: ", cohorts); + res.json(cohorts); + }) + .catch((error) => { + console.log(error); + res.status(500).json({ error: "failed to retreive cohorts" }); + }); +}); + +// Creates a new cohort: + +router.post("/", (req, res) => { + console.log("received request: "); + Cohort.create({ + inProgress: req.body.inProgress, + cohortSlug: req.body.cohortSlug, + cohortName: req.body.cohortName, + program: req.body.program, + campus: req.body.campus, + startDate: req.body.startDate, + endDate: req.body.endDate, + programManager: req.body.programManager, + leadTeacher: req.body.leadTeacher, + totalHours: req.body.totalHours + }) + .then(() => { + res.send("Cohort created sucessfully"); + }) + .catch((error) => { + console.log(error); + }); +}); + +// Get a spesific cohort by id: + +router.get("/:cohortId", (req, res) => { + console.log("get request recieved (single cohort)") + + Cohort.find({ _id: req.params.cohortId }) + .then((cohort) => { + res.json(cohort) + }) + .catch((error) => { + console.log(error) + }) + +}) + +// update spesific cohort by id: + +router.put("/:cohortId", (req, res) => { + console.log("update req received") + + Cohort.findByIdAndUpdate(req.params.cohortId, { + inProgress: req.body.inProgress, + cohortSlug: req.body.cohortSlug, + cohortName: req.body.cohortName, + program: req.body.program, + campus: req.body.campus, + startDate: req.body.startDate, + endDate: req.body.endDate, + programManager: req.body.programManager, + leadTeacher: req.body.leadTeacher, + totalHours: req.body.totalHours + }) + .then(() => { + res.send(`cohort with id: ${req.params.cohortId} has updated`) + }) + .catch((error) => { + console.log(error) + }) +}) + +// delete spesific cohort by id: +router.delete("/:cohortId", (req, res) => { + console.log("delete request received") + + Cohort.findByIdAndDelete(req.params.cohortId) + .then(() => { + res.send(`the cohort (id: ${req.params.cohortId}) has deleted succesfully`) + }) + .catch((error) => { + console.log(error) + }) +}) + +module.exports = router \ No newline at end of file diff --git a/server/routes/index.routes.js b/server/routes/index.routes.js new file mode 100644 index 00000000..cd62e8c5 --- /dev/null +++ b/server/routes/index.routes.js @@ -0,0 +1,9 @@ +const express = require("express") +const router = express.Router() +const cohortRoutes = require("./cohort.routes") +const studentRoutes = require("./student.routes") + +router.use("/cohorts", cohortRoutes) +router.use("/students", studentRoutes) + +module.exports = router \ No newline at end of file diff --git a/server/routes/student.routes.js b/server/routes/student.routes.js new file mode 100644 index 00000000..632f8ee8 --- /dev/null +++ b/server/routes/student.routes.js @@ -0,0 +1,101 @@ +const Student = require("../models/Student.model") +const express = require("express") +const router = express.Router() + +// get student: + +router.get("/", (req, res) => { + Student.find(req.query) + .populate("cohort") + .then((students) => { + console.log("Retrieved students: ", students); + res.json(students); + }) + .catch((error) => { + console.log(error); + res.status(500).json({ error: "failed to retreive students" }); + }); +}); + +// create new student: + +router.post("/", (req, res) => { + console.log("received request: "); + Student.create({ + firstName: req.body.firstName, + lastName: req.body.lastName, + email: req.body.email, + phone: req.body.phone, + linkedinUrl: req.body.linkedinUrl, + languages: req.body.languages, + program: req.body.program, + background: req.body.background, + image: req.body.image, + projects: req.body.projects, + cohort: req.body.cohort, + }) + .then(() => { + res.send("Student created sucessfully"); + }) + .catch((error) => { + console.log(error); + }); +}); + +router.get("/cohort/:cohortId", (req, res) => { + console.log(req.params.cohortId); + Student.find({ cohort: req.params.cohortId }) + .then((students) => { + console.log(students); + res.json(students); + }) + .catch((error) => { + console.log(error); + }); +}); + +router.get("/:studentId", (req, res) => { + Student.find({ _id: req.params.studentId }) + .populate("cohort") + .then((student) => { + res.json(student); + res.send("success"); + }) + .catch((error) => { + console.log(error); + }); +}); + +router.put("/:studentId", (req, res) => { + Student.findByIdAndUpdate(req.params.studentId, { + firstName: req.body.firstName, + lastName: req.body.lastName, + email: req.body.email, + phone: req.body.phone, + linkedinUrl: req.body.linkedinUrl, + languages: req.body.languages, + program: req.body.program, + background: req.body.background, + image: req.body.image, + projects: req.body.projects, + cohort: req.body.cohort, + }) + .then(() => { + res.send("Document updated"); + }) + .catch((error) => { + console.log(error); + }); +}); + +router.delete("/:studentId", (req, res) => { + Student.findByIdAndDelete(req.params.studentId) + .then(() => { + res.send("Document deleted"); + }) + .catch((error) => { + console.log(error); + }); +}); + +module.exports = router \ No newline at end of file From a77785924dfd777f9135aac111da1446e4d1fe0d Mon Sep 17 00:00:00 2001 From: naimyasirozcan Date: Thu, 20 Nov 2025 17:48:14 +0100 Subject: [PATCH 8/8] error handling fixed, done for today --- server/app.js | 14 ++++----- server/error-handlers/index.js | 12 ++++++++ server/routes/cohort.routes.js | 12 ++++++++ server/routes/student.routes.js | 54 ++++++++++++++++----------------- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/server/app.js b/server/app.js index 375f4eec..01a6b30a 100644 --- a/server/app.js +++ b/server/app.js @@ -1,6 +1,6 @@ try { process.loadEnvFile() -} catch (error){ +} catch (error) { console.log("can't access to .env file") } @@ -10,27 +10,25 @@ const express = require("express"); const Cohort = require("./models/Cohort.model"); const Student = require("./models/Student.model"); -// data import / assignment - +// Creation of the Server: const app = express(); // Midlleware Routes: - const config = require("./config") config(app) -// *** Routes *** - // Initialization: app.get("/docs", (req, res) => { res.sendFile(__dirname + "/views/docs.html"); - }); // Routes of Collections: -const indexRouter = require("./routes/index.routes") +const indexRouter = require("./routes/index.routes") app.use("/api", indexRouter) +// Error Handlers: +const errorHandlers = require("./error-handlers") +errorHandlers(app) // Start Server: const PORT = process.env.PORT || 5005 diff --git a/server/error-handlers/index.js b/server/error-handlers/index.js index e69de29b..62604bf9 100644 --- a/server/error-handlers/index.js +++ b/server/error-handlers/index.js @@ -0,0 +1,12 @@ +function errorHandlers(app){ + + app.use((req, res) => { + res.status(404).json({message: "Could't find anything, maybe you've lost." }) + }) + + app.use((error, req, res, next) => { + res.status(500).json({errorMessage: "Something went wrong."}) + }) +} + +module.exports = errorHandlers \ No newline at end of file diff --git a/server/routes/cohort.routes.js b/server/routes/cohort.routes.js index be379b9c..a757dce0 100644 --- a/server/routes/cohort.routes.js +++ b/server/routes/cohort.routes.js @@ -55,6 +55,18 @@ router.get("/:cohortId", (req, res) => { }) +router.get("/cohort/:cohortId", (req, res) => { + console.log(req.params.cohortId); + Student.find({ cohort: req.params.cohortId }) + .then((students) => { + console.log(students); + res.json(students); + }) + .catch((error) => { + console.log(error); + }); +}); + // update spesific cohort by id: router.put("/:cohortId", (req, res) => { diff --git a/server/routes/student.routes.js b/server/routes/student.routes.js index 632f8ee8..c8ea9df2 100644 --- a/server/routes/student.routes.js +++ b/server/routes/student.routes.js @@ -3,7 +3,6 @@ const express = require("express") const router = express.Router() // get student: - router.get("/", (req, res) => { Student.find(req.query) .populate("cohort") @@ -13,12 +12,12 @@ router.get("/", (req, res) => { }) .catch((error) => { console.log(error); - res.status(500).json({ error: "failed to retreive students" }); + console.log("XXXXXXXXXX ERROR TRIGGERED XXXXXXXXXXXXX"); + // res.status(500).json({ error: "failed to retreive students" }); }); }); // create new student: - router.post("/", (req, res) => { console.log("received request: "); Student.create({ @@ -42,19 +41,13 @@ router.post("/", (req, res) => { }); }); -router.get("/cohort/:cohortId", (req, res) => { - console.log(req.params.cohortId); - Student.find({ cohort: req.params.cohortId }) - .then((students) => { - console.log(students); - res.json(students); - }) - .catch((error) => { - console.log(error); - }); -}); - router.get("/:studentId", (req, res) => { + + if(req.params.studentId.length !== 24){ + res.status(400).json({errorMessage: "Not a valid ID."}) + return + } + Student.find({ _id: req.params.studentId }) .populate("cohort") .then((student) => { @@ -80,22 +73,29 @@ router.put("/:studentId", (req, res) => { projects: req.body.projects, cohort: req.body.cohort, }) - .then(() => { - res.send("Document updated"); - }) - .catch((error) => { - console.log(error); - }); + .then(() => { + res.send("Document updated"); + }) + .catch((error) => { + console.log(error); + }); }); + router.delete("/:studentId", (req, res) => { + + if(req.params.studentId.length !== 24){ + res.status(400).json({errorMessage: "Not a valid ID."}) + return + } + Student.findByIdAndDelete(req.params.studentId) - .then(() => { - res.send("Document deleted"); - }) - .catch((error) => { - console.log(error); - }); + .then(() => { + res.send("Document deleted"); + }) + .catch((error) => { + console.log(error); + }); }); module.exports = router \ No newline at end of file