From 91328435e54c373fc2068a6ff325f94b8dfe9f01 Mon Sep 17 00:00:00 2001 From: miker2004 <157730940+miker2004@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:23:40 +0100 Subject: [PATCH 1/6] Delete readme.ro.md --- readme.ro.md | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 readme.ro.md diff --git a/readme.ro.md b/readme.ro.md deleted file mode 100644 index 3750be63fc9..00000000000 --- a/readme.ro.md +++ /dev/null @@ -1,29 +0,0 @@ -## GoIT Node.js Course Template Homework - -Realizează un fork al acestui repozitoriu pentru a îndeplini temele de acasă (2-6). Fork-ul va crea un repozitoriu pe contul tău de pe http://github.com - -Adaugă mentorul la colaborare. - -Pentru fiecare temă, creează un branch separat. - -- hw02 -- hw03 -- hw04 -- hw05 -- hw06 - -Fiecare branch nou pentru fiecare temă trebuie să fie derivat din branch-ul principal (master). - -După ce ai terminat lucrul la tema de acasă în branch-ul tău, trebuie să creezi un pull request (PR). Apoi, adaugă mentorul pentru revizuirea codului. Abia după ce mentorul aprobă PR-ul, poți face "merge" a branch-ului cu tema de acasă în branch-ul master. - -Citește cu atenție comentariile mentorului. Corectează observațiile și fă un "commit" în branch-ul cu tema de acasă. Modificările se vor reflecta automat în PR după ce trimiți "commit"-ul cu corecțiile pe GitHub. După corectare, adaugă din nou mentorul pentru revizuirea codului. - -- La predarea temei de acasă, este furnizat un link către PR. -- Codul JavaScript este curat și ușor de înțeles, iar pentru formatare se folosește Prettier. - -### Comenzi: - -- `npm start` — pornește serverul în modul production. -- `npm run start:dev` — pornește serverul în modul dezvoltare (development). -- `npm run lint` — rulează verificarea codului cu ESLint, este necesar să se ruleze înaintea fiecărui PR și să se corecteze toate erorile linterului. -- `npm lint:fix` — aceeași verificare a linterului, dar cu corecții automate pentru erorile simple. From 17bcca9f0e0f21b324f00afb5c82499905e515db Mon Sep 17 00:00:00 2001 From: miker2004 <157730940+miker2004@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:24:11 +0100 Subject: [PATCH 2/6] Delete readme.es.md --- readme.es.md | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 readme.es.md diff --git a/readme.es.md b/readme.es.md deleted file mode 100644 index 25cf77f2ef2..00000000000 --- a/readme.es.md +++ /dev/null @@ -1,27 +0,0 @@ -## GoIT Node.js Course Template Homework - -Realice un fork de este repositorio para realizar las tareas (2-6).El fork creará un repositorio en su - -[GitHub: Let’s build from here](https://github.com/) - -Agregue al mentor como colaborador.Para cada tarea, cree su propia rama. - -- hw02 -- hw03 -- hw04 -- hw05 -- hw06 - -Cada nueva rama para la tarea debe crearse desde la rama master. -Después de completar una tarea en su rama, debe hacer una solicitud de extracción (PR). Luego, agregue al mentor para la revisión del código. Solo después de que el mentor apruebe el PR, puede fusionar la rama de la tarea en la rama principal. - -Lea atentamente los comentarios del mentor. Corrija los comentarios y haga un commit en la rama de la tarea. Los cambios se reflejarán automáticamente en el PR después de que envíe el commit con las correcciones a GitHub. Luego, agregue nuevamente al mentor para la revisión del código.- Al entregar una tarea, incluya un enlace al PR. - -- El código JavaScript debe estar limpio y claro, y se debe usar Prettier para el formato. - -### Comandos: - -- `npm start` — Inicia el servidor en modo de producción. -- `npm run start:dev` — Inicia el servidor en modo de desarrollo. -- `npm run lint` — Ejecuta la comprobación del código con eslint. Debe ejecutarse antes de cada PR y corregir todos los errores del linter. -- `npm lint:fix` — Lo mismo que la comprobación del linter, pero con correcciones automáticas de errores simples. \ No newline at end of file From 2002fe0597ba1bdd57851e6dd756516fcde87256 Mon Sep 17 00:00:00 2001 From: miker2004 <157730940+miker2004@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:24:26 +0100 Subject: [PATCH 3/6] Delete readme.md --- readme.md | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 readme.md diff --git a/readme.md b/readme.md deleted file mode 100644 index 4ecbbd24691..00000000000 --- a/readme.md +++ /dev/null @@ -1,31 +0,0 @@ -## GoIT Node.js Course Template Homework - -Виконайте форк цього репозиторію для виконання домашніх завдань (2-6) -Форк створить репозиторій на вашому http://github.com - -Додайте ментора до колаборації - -Для кожної домашньої роботи створюйте свою гілку. - -- hw02 -- hw03 -- hw04 -- hw05 -- hw06 - -Кожна нова гілка для др повинна робитися з master - -Після того, як ви закінчили виконувати домашнє завдання у своїй гілці, необхідно зробити пулл-реквест (PR). Потім додати ментора для рев'ю коду. Тільки після того, як ментор заапрувить PR, ви можете виконати мердж гілки з домашнім завданням у майстер. - -Уважно читайте коментарі ментора. Виправте зауваження та зробіть коміт у гілці з домашнім завданням. Зміни підтягнуться у PR автоматично після того, як ви відправите коміт з виправленнями на github -Після виправлення знову додайте ментора на рев'ю коду. - -- При здачі домашньої роботи є посилання на PR -- JS-код чистий та зрозумілий, для форматування використовується Prettier - -### Команди: - -- `npm start` — старт сервера в режимі production -- `npm run start:dev` — старт сервера в режимі розробки (development) -- `npm run lint` — запустити виконання перевірки коду з eslint, необхідно виконувати перед кожним PR та виправляти всі помилки лінтера -- `npm lint:fix` — та ж перевірка лінтера, але з автоматичними виправленнями простих помилок From 901e6292363615203e99c094648894337e0288ee Mon Sep 17 00:00:00 2001 From: miker2004 <157730940+miker2004@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:25:18 +0100 Subject: [PATCH 4/6] Rename readme.pl.md to readme.md --- readme.pl.md => readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename readme.pl.md => readme.md (97%) diff --git a/readme.pl.md b/readme.md similarity index 97% rename from readme.pl.md rename to readme.md index 817da777968..5b8c6a2ea68 100644 --- a/readme.pl.md +++ b/readme.md @@ -26,4 +26,4 @@ Uważnie czytaj komentarze mentora. Popraw uwagi i zrób commit na gałęzi z za - `npm start` — uruchamia serwer w trybie produkcyjnym - `npm run start:dev` — uruchamia serwer w trybie deweloperskim (development) - `npm run lint` — uruchamia sprawdzanie kodu z ESLint, należy wykonać przed każdym PR i poprawić wszystkie błędy lintera -- `npm lint:fix` — to samo co powyższe, ale również automatycznie poprawia proste błędy. \ No newline at end of file +- `npm lint:fix` — to samo co powyższe, ale również automatycznie poprawia proste błędy. From ba0efff08a41ce56d508047e7858fdc452472103 Mon Sep 17 00:00:00 2001 From: miker2004 <157730940+miker2004@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:33:20 +0100 Subject: [PATCH 5/6] 1.0001 MongoDB, Moongose --- app.js | 44 ++++-- models/contacts.js | 35 ++-- models/contacts.json | 62 -------- package-lock.json | 351 +++++++++++++++++++++++++++++++++++++++-- package.json | 2 + routes/api/contacts.js | 99 ++++++++++-- server.js | 10 +- 7 files changed, 478 insertions(+), 125 deletions(-) delete mode 100644 models/contacts.json diff --git a/app.js b/app.js index 40fd9bc167f..1c5173c1bf0 100644 --- a/app.js +++ b/app.js @@ -1,25 +1,39 @@ -const express = require('express') -const logger = require('morgan') -const cors = require('cors') +const express = require('express'); +const logger = require('morgan'); +const cors = require('cors'); +const mongoose = require('mongoose'); -const contactsRouter = require('./routes/api/contacts') +const contactsRouter = require('./routes/api/contacts'); -const app = express() +const app = express(); -const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short' +const uri = 'mongodb+srv://dzanuszgames:il9xxngkjkAEgep0@personalfinancetracker.drflz.mongodb.net/'; -app.use(logger(formatsLogger)) -app.use(cors()) -app.use(express.json()) +mongoose.connect(uri, { + useNewUrlParser: true, + useUnifiedTopology: true, +}) + .then(() => console.log('Database connection successful')) + .catch(err => { + console.error('Database connection error:', err.message); + process.exit(1); + }); + +const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'; + +app.use(logger(formatsLogger)); +app.use(cors()); +app.use(express.json()); -app.use('/api/contacts', contactsRouter) +app.use('/api/contacts', contactsRouter); app.use((req, res) => { - res.status(404).json({ message: 'Not found' }) -}) + res.status(404).json({ message: 'Not found' }); +}); app.use((err, req, res, next) => { - res.status(500).json({ message: err.message }) -}) + console.error(err.stack); + res.status(500).json({ message: 'Internal Server Error' }); +}); -module.exports = app +module.exports = app; diff --git a/models/contacts.js b/models/contacts.js index 409d11c7c09..9de047db4e9 100644 --- a/models/contacts.js +++ b/models/contacts.js @@ -1,19 +1,22 @@ -// const fs = require('fs/promises') +const mongoose = require('mongoose'); -const listContacts = async () => {} +const contactSchema = new mongoose.Schema({ + name: { + type: String, + required: [true, 'Set name for contact'], + }, + email: { + type: String, + }, + phone: { + type: String, + }, + favorite: { + type: Boolean, + default: false, + }, +}); -const getContactById = async (contactId) => {} +const Contact = mongoose.model('Contact', contactSchema); -const removeContact = async (contactId) => {} - -const addContact = async (body) => {} - -const updateContact = async (contactId, body) => {} - -module.exports = { - listContacts, - getContactById, - removeContact, - addContact, - updateContact, -} +module.exports = Contact; diff --git a/models/contacts.json b/models/contacts.json deleted file mode 100644 index a21679132de..00000000000 --- a/models/contacts.json +++ /dev/null @@ -1,62 +0,0 @@ -[ - { - "id": "AeHIrLTr6JkxGE6SN-0Rw", - "name": "Allen Raymond", - "email": "nulla.ante@vestibul.co.uk", - "phone": "(992) 914-3792" - }, - { - "id": "qdggE76Jtbfd9eWJHrssH", - "name": "Chaim Lewis", - "email": "dui.in@egetlacus.ca", - "phone": "(294) 840-6685" - }, - { - "id": "drsAJ4SHPYqZeG-83QTVW", - "name": "Kennedy Lane", - "email": "mattis.Cras@nonenimMauris.net", - "phone": "(542) 451-7038" - }, - { - "id": "vza2RIzNGIwutCVCs4mCL", - "name": "Wylie Pope", - "email": "est@utquamvel.net", - "phone": "(692) 802-2949" - }, - { - "id": "05olLMgyVQdWRwgKfg5J6", - "name": "Cyrus Jackson", - "email": "nibh@semsempererat.com", - "phone": "(501) 472-5218" - }, - { - "id": "1DEXoP8AuCGYc1YgoQ6hw", - "name": "Abbot Franks", - "email": "scelerisque@magnis.org", - "phone": "(186) 568-3720" - }, - { - "id": "Z5sbDlS7pCzNsnAHLtDJd", - "name": "Reuben Henry", - "email": "pharetra.ut@dictum.co.uk", - "phone": "(715) 598-5792" - }, - { - "id": "C9sjBfCo4UJCWjzBnOtxl", - "name": "Simon Morton", - "email": "dui.Fusce.diam@Donec.com", - "phone": "(233) 738-2360" - }, - { - "id": "e6ywwRe4jcqxXfCZOj_1e", - "name": "Thomas Lucas", - "email": "nec@Nulla.com", - "phone": "(704) 398-7993" - }, - { - "id": "rsKkOQUi80UsgVPCcLZZW", - "name": "Alec Howard", - "email": "Donec.elementum@scelerisquescelerisquedui.net", - "phone": "(748) 206-2688" - } -] diff --git a/package-lock.json b/package-lock.json index e6d047044e5..57ff9146bd6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,8 @@ "cors": "2.8.5", "cross-env": "7.0.3", "express": "4.17.1", + "mongodb": "^6.10.0", + "mongoose": "^8.8.1", "morgan": "1.10.0" }, "devDependencies": { @@ -141,6 +143,15 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/@mongodb-js/saslprep": { + "version": "1.1.9", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz", + "integrity": "sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw==", + "license": "MIT", + "dependencies": { + "sparse-bitfield": "^3.0.3" + } + }, "node_modules/@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -168,6 +179,21 @@ "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=", "dev": true }, + "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/abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -452,6 +478,15 @@ "node": ">=8" } }, + "node_modules/bson": { + "version": "6.9.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.9.0.tgz", + "integrity": "sha512-X9hJeyeM0//Fus+0pc5dSUMhhrrmWwQUtdavaQeF3Ta6m69matZkGWV/MrBcnwUeLC8W9kwwc2hfkZgUuCX3Ig==", + "license": "Apache-2.0", + "engines": { + "node": ">=16.20.1" + } + }, "node_modules/bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -728,7 +763,6 @@ "version": "4.3.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -2215,6 +2249,15 @@ "json5": "lib/cli.js" } }, + "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/keyv": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz", @@ -2327,6 +2370,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", @@ -2397,6 +2446,90 @@ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "dev": true }, + "node_modules/mongodb": { + "version": "6.10.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.10.0.tgz", + "integrity": "sha512-gP9vduuYWb9ZkDM546M+MP2qKVk5ZG2wPF63OvSRuUbqCR+11ZCAE1mOfllhlAG0wcoJY5yDL/rV3OmYEwXIzg==", + "license": "Apache-2.0", + "dependencies": { + "@mongodb-js/saslprep": "^1.1.5", + "bson": "^6.7.0", + "mongodb-connection-string-url": "^3.0.0" + }, + "engines": { + "node": ">=16.20.1" + }, + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.2.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.1", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.1.tgz", + "integrity": "sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==", + "license": "Apache-2.0", + "dependencies": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^13.0.0" + } + }, + "node_modules/mongoose": { + "version": "8.8.1", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.1.tgz", + "integrity": "sha512-l7DgeY1szT98+EKU8GYnga5WnyatAu+kOQ2VlVX1Mxif6A0Umt0YkSiksCiyGxzx8SPhGe9a53ND1GD4yVDrPA==", + "license": "MIT", + "dependencies": { + "bson": "^6.7.0", + "kareem": "2.6.3", + "mongodb": "~6.10.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", @@ -2433,11 +2566,31 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "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/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/natural-compare": { "version": "1.4.0", @@ -2863,10 +3016,10 @@ } }, "node_modules/punycode": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", - "dev": true, + "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" } @@ -3185,6 +3338,12 @@ "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/signal-exit": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.6.tgz", @@ -3208,6 +3367,15 @@ "url": "https://github.com/chalk/slice-ansi?sponsor=1" } }, + "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/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -3392,6 +3560,18 @@ "nodetouch": "bin/nodetouch.js" } }, + "node_modules/tr46": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", + "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "license": "MIT", + "dependencies": { + "punycode": "^2.3.0" + }, + "engines": { + "node": ">=14" + } + }, "node_modules/tsconfig-paths": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.12.0.tgz", @@ -3558,6 +3738,28 @@ "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": "13.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==", + "license": "MIT", + "dependencies": { + "tr46": "^4.1.1", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=16" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -3757,6 +3959,14 @@ "strip-json-comments": "^3.1.1" } }, + "@mongodb-js/saslprep": { + "version": "1.1.9", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz", + "integrity": "sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw==", + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -3778,6 +3988,19 @@ "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=", "dev": true }, + "@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==" + }, + "@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==", + "requires": { + "@types/webidl-conversions": "*" + } + }, "abbrev": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", @@ -3997,6 +4220,11 @@ "fill-range": "^7.0.1" } }, + "bson": { + "version": "6.9.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.9.0.tgz", + "integrity": "sha512-X9hJeyeM0//Fus+0pc5dSUMhhrrmWwQUtdavaQeF3Ta6m69matZkGWV/MrBcnwUeLC8W9kwwc2hfkZgUuCX3Ig==" + }, "bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -4198,7 +4426,6 @@ "version": "4.3.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", "integrity": "sha512-/zxw5+vh1Tfv+4Qn7a5nsbcJKPaSvCDhojn6FEl9vupwK2VCSDtEiEtqr8DFtzYFOdz63LBkxec7DYuc2jon6Q==", - "dev": true, "requires": { "ms": "2.1.2" } @@ -5312,6 +5539,11 @@ "minimist": "^1.2.0" } }, + "kareem": { + "version": "2.6.3", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.6.3.tgz", + "integrity": "sha512-C3iHfuGUXK2u8/ipq9LfjFfXFxAZMQJJq7vLS45r3D9Y2xQ/m4S8zaR4zMLFWh9AsNPXmcFfUDhTEO8UIC/V6Q==" + }, "keyv": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/keyv/-/keyv-3.1.0.tgz", @@ -5399,6 +5631,11 @@ "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, + "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==" + }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -5448,6 +5685,46 @@ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", "dev": true }, + "mongodb": { + "version": "6.10.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.10.0.tgz", + "integrity": "sha512-gP9vduuYWb9ZkDM546M+MP2qKVk5ZG2wPF63OvSRuUbqCR+11ZCAE1mOfllhlAG0wcoJY5yDL/rV3OmYEwXIzg==", + "requires": { + "@mongodb-js/saslprep": "^1.1.5", + "bson": "^6.7.0", + "mongodb-connection-string-url": "^3.0.0" + } + }, + "mongodb-connection-string-url": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.1.tgz", + "integrity": "sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==", + "requires": { + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^13.0.0" + } + }, + "mongoose": { + "version": "8.8.1", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.1.tgz", + "integrity": "sha512-l7DgeY1szT98+EKU8GYnga5WnyatAu+kOQ2VlVX1Mxif6A0Umt0YkSiksCiyGxzx8SPhGe9a53ND1GD4yVDrPA==", + "requires": { + "bson": "^6.7.0", + "kareem": "2.6.3", + "mongodb": "~6.10.0", + "mpath": "0.9.0", + "mquery": "5.0.0", + "ms": "2.1.3", + "sift": "17.1.3" + }, + "dependencies": { + "ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + } + } + }, "morgan": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz", @@ -5480,11 +5757,23 @@ } } }, + "mpath": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.9.0.tgz", + "integrity": "sha512-ikJRQTk8hw5DEoFVxHG1Gn9T/xcjtdnOKIU1JTmGjZZlg9LST2mBLmcX3/ICIbgJydT2GOc15RnNy5mHmzfSew==" + }, + "mquery": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-5.0.0.tgz", + "integrity": "sha512-iQMncpmEK8R8ncT8HJGsGc9Dsp8xcgYMVSbs5jgnm1lFHTZqMJTUWTDx1LBO8+mK3tPNZWFLBghQEIOULSTHZg==", + "requires": { + "debug": "4.x" + } + }, "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "natural-compare": { "version": "1.4.0", @@ -5795,10 +6084,9 @@ } }, "punycode": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", - "dev": true + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==" }, "pupa": { "version": "2.1.1", @@ -6047,6 +6335,11 @@ "object-inspect": "^1.9.0" } }, + "sift": { + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/sift/-/sift-17.1.3.tgz", + "integrity": "sha512-Rtlj66/b0ICeFzYTuNvX/EF1igRbbnGSvEyT79McoZa/DeGhMyC5pWKOEsZKnpkqtSeovd5FL/bjHWC3CIIvCQ==" + }, "signal-exit": { "version": "3.0.6", "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.6.tgz", @@ -6064,6 +6357,14 @@ "is-fullwidth-code-point": "^3.0.0" } }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", + "requires": { + "memory-pager": "^1.0.2" + } + }, "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -6204,6 +6505,14 @@ "nopt": "~1.0.10" } }, + "tr46": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", + "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "requires": { + "punycode": "^2.3.0" + } + }, "tsconfig-paths": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.12.0.tgz", @@ -6337,6 +6646,20 @@ "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" }, + "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==" + }, + "whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==", + "requires": { + "tr46": "^4.1.1", + "webidl-conversions": "^7.0.0" + } + }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 5045e827160..864b3b973e5 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "cors": "2.8.5", "cross-env": "7.0.3", "express": "4.17.1", + "mongodb": "^6.10.0", + "mongoose": "^8.8.1", "morgan": "1.10.0" }, "devDependencies": { diff --git a/routes/api/contacts.js b/routes/api/contacts.js index a60ebd69231..fc37ea58c53 100644 --- a/routes/api/contacts.js +++ b/routes/api/contacts.js @@ -1,25 +1,96 @@ -const express = require('express') - -const router = express.Router() +const express = require('express'); +const router = express.Router(); +const Contact = require('../../models/contacts'); router.get('/', async (req, res, next) => { - res.json({ message: 'template message' }) -}) + try { + const contacts = await Contact.find(); + res.json(contacts); + } catch (error) { + next(error); + } +}); router.get('/:contactId', async (req, res, next) => { - res.json({ message: 'template message' }) -}) + try { + const { contactId } = req.params; + const contact = await Contact.findById(contactId); + + if (!contact) { + return res.status(404).json({ message: 'Contact not found' }); + } + + res.json(contact); + } catch (error) { + next(error); + } +}); router.post('/', async (req, res, next) => { - res.json({ message: 'template message' }) -}) + try { + const newContact = await Contact.create(req.body); + res.status(201).json(newContact); + } catch (error) { + next(error); + } +}); router.delete('/:contactId', async (req, res, next) => { - res.json({ message: 'template message' }) -}) + try { + const { contactId } = req.params; + const deletedContact = await Contact.findByIdAndDelete(contactId); + + if (!deletedContact) { + return res.status(404).json({ message: 'Contact not found' }); + } + + res.status(204).send(); + } catch (error) { + next(error); + } +}); router.put('/:contactId', async (req, res, next) => { - res.json({ message: 'template message' }) -}) + try { + const { contactId } = req.params; + const updatedContact = await Contact.findByIdAndUpdate(contactId, req.body, { + new: true, + runValidators: true, + }); + + if (!updatedContact) { + return res.status(404).json({ message: 'Contact not found' }); + } + + res.json(updatedContact); + } catch (error) { + next(error); + } +}); + +router.patch('/:contactId/favorite', async (req, res, next) => { + try { + const { contactId } = req.params; + const { favorite } = req.body; + + if (favorite === undefined) { + return res.status(400).json({ message: 'missing field favorite' }); + } + + const updatedContact = await Contact.findByIdAndUpdate( + contactId, + { favorite }, + { new: true } + ); + + if (!updatedContact) { + return res.status(404).json({ message: 'Not found' }); + } + + res.status(200).json(updatedContact); + } catch (error) { + next(error); + } +}); -module.exports = router +module.exports = router; diff --git a/server.js b/server.js index db330824656..859f6832e52 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,7 @@ -const app = require('./app') +const app = require('./app'); -app.listen(3000, () => { - console.log("Server running. Use our API on port: 3000") -}) +const PORT = process.env.PORT || 3000; + +app.listen(PORT, () => { + console.log(`Server running on port ${PORT}`); +}); \ No newline at end of file From 4e8b64da562a09c5643b3a3438123405915a42f7 Mon Sep 17 00:00:00 2001 From: miker2004 <157730940+miker2004@users.noreply.github.com> Date: Sat, 16 Nov 2024 15:26:34 +0100 Subject: [PATCH 6/6] env to password --- app.js | 10 ++++------ package-lock.json | 18 ++++++++++++++++++ package.json | 1 + 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 1c5173c1bf0..2c18c9a273a 100644 --- a/app.js +++ b/app.js @@ -2,23 +2,21 @@ const express = require('express'); const logger = require('morgan'); const cors = require('cors'); const mongoose = require('mongoose'); +require('dotenv').config(); const contactsRouter = require('./routes/api/contacts'); const app = express(); -const uri = 'mongodb+srv://dzanuszgames:il9xxngkjkAEgep0@personalfinancetracker.drflz.mongodb.net/'; +const uri = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER}/${process.env.MONGO_DB}?retryWrites=true&w=majority`; -mongoose.connect(uri, { - useNewUrlParser: true, - useUnifiedTopology: true, -}) +mongoose.connect(uri) .then(() => console.log('Database connection successful')) .catch(err => { console.error('Database connection error:', err.message); process.exit(1); }); - + const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'; app.use(logger(formatsLogger)); diff --git a/package-lock.json b/package-lock.json index 57ff9146bd6..f444512775e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "cors": "2.8.5", "cross-env": "7.0.3", + "dotenv": "^16.4.5", "express": "4.17.1", "mongodb": "^6.10.0", "mongoose": "^8.8.1", @@ -857,6 +858,18 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", @@ -4494,6 +4507,11 @@ "is-obj": "^2.0.0" } }, + "dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==" + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", diff --git a/package.json b/package.json index 864b3b973e5..c77cf6ccd23 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "cors": "2.8.5", "cross-env": "7.0.3", + "dotenv": "^16.4.5", "express": "4.17.1", "mongodb": "^6.10.0", "mongoose": "^8.8.1",