From 28d537c9ef567f150e6a8172b60e7f1de237ed3d Mon Sep 17 00:00:00 2001 From: stefania m Date: Sat, 26 Oct 2024 16:20:55 +0300 Subject: [PATCH 1/5] update package json --- models/contacts.js | 3 +++ package.json | 9 ++++++--- server.js | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/models/contacts.js b/models/contacts.js index 409d11c7c09..3e286a465c6 100644 --- a/models/contacts.js +++ b/models/contacts.js @@ -17,3 +17,6 @@ module.exports = { addContact, updateContact, } + + + diff --git a/package.json b/package.json index 5045e827160..09169778cd5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { - "name": "template", + "name": "nodejs-homework-rest-api", "version": "0.0.0", "private": true, + "type": "module", "scripts": { "start": "cross-env NODE_ENV=production node ./server.js", "start:dev": "cross-env NODE_ENV=development nodemon ./server.js", @@ -12,7 +13,9 @@ "cors": "2.8.5", "cross-env": "7.0.3", "express": "4.17.1", - "morgan": "1.10.0" + "joi": "^17.13.3", + "morgan": "1.10.0", + "nanoid": "^5.0.7" }, "devDependencies": { "eslint": "7.19.0", @@ -23,4 +26,4 @@ "eslint-plugin-promise": "5.2.0", "nodemon": "2.0.15" } -} +} \ No newline at end of file diff --git a/server.js b/server.js index fc4e4c6bb3a..86caabc6719 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,5 @@ -const app = require("./app"); +import app from "./app.js"; app.listen(3000, () => { console.log("Server is running. Use our API on port: 3000"); -}); +}); \ No newline at end of file From f56423fd4b6c825780c55152cba080dabe4ed54f Mon Sep 17 00:00:00 2001 From: stefania m Date: Sat, 26 Oct 2024 17:05:20 +0300 Subject: [PATCH 2/5] up2 --- package-lock.json | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index e6d047044e5..3b16307f2f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,19 @@ { - "name": "template", + "name": "nodejs-homework-rest-api", "version": "0.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "template", + "name": "nodejs-homework-rest-api", "version": "0.0.0", "dependencies": { "cors": "2.8.5", "cross-env": "7.0.3", "express": "4.17.1", - "morgan": "1.10.0" + "joi": "^17.13.3", + "morgan": "1.10.0", + "nanoid": "^5.0.7" }, "devDependencies": { "eslint": "7.19.0", From 6c6cab4225f100a2cb9ff9853e44d079566e64b9 Mon Sep 17 00:00:00 2001 From: stefania m Date: Sat, 26 Oct 2024 17:05:52 +0300 Subject: [PATCH 3/5] appjs --- app.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 40fd9bc167f..539a2b773e1 100644 --- a/app.js +++ b/app.js @@ -1,25 +1,25 @@ -const express = require('express') -const logger = require('morgan') -const cors = require('cors') +import express from "express"; +import logger from "morgan"; +import cors from "cors"; -const contactsRouter = require('./routes/api/contacts') +import contactsRouter from "./routes/api/contacts.js"; -const app = express() +const app = express(); -const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short' +const formatsLogger = app.get("env") === "development" ? "dev" : "short"; -app.use(logger(formatsLogger)) -app.use(cors()) -app.use(express.json()) +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 }) -}) + res.status(500).json({ message: err.message }); +}); -module.exports = app +export default app; \ No newline at end of file From 00f4c2235a95270666e1665c3df6d0c7cae9c2db Mon Sep 17 00:00:00 2001 From: stefania m Date: Sat, 26 Oct 2024 17:46:37 +0300 Subject: [PATCH 4/5] Update package-lock.json --- package-lock.json | 115 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/package-lock.json b/package-lock.json index 3b16307f2f6..853b829114d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -143,6 +143,42 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/@hapi/hoek": { + "version": "9.3.0", + "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz", + "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==", + "license": "BSD-3-Clause" + }, + "node_modules/@hapi/topo": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz", + "integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==", + "license": "BSD-3-Clause", + "dependencies": { + "@hapi/hoek": "^9.0.0" + } + }, + "node_modules/@sideway/address": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.5.tgz", + "integrity": "sha512-IqO/DUQHUkPeixNQ8n0JA6102hT9CmaljNTPmQ1u8MEhBo/R4Q8eKLN/vGZxuebwOroDB4cbpjheD4+/sKFK4Q==", + "license": "BSD-3-Clause", + "dependencies": { + "@hapi/hoek": "^9.0.0" + } + }, + "node_modules/@sideway/formula": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.1.tgz", + "integrity": "sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg==", + "license": "BSD-3-Clause" + }, + "node_modules/@sideway/pinpoint": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz", + "integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==", + "license": "BSD-3-Clause" + }, "node_modules/@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -2168,6 +2204,19 @@ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" }, + "node_modules/joi": { + "version": "17.13.3", + "resolved": "https://registry.npmjs.org/joi/-/joi-17.13.3.tgz", + "integrity": "sha512-otDA4ldcIx+ZXsKHWmp0YizCweVRZG96J10b0FevjfuncLO1oX59THoAmHkNubYJ+9gWsYsp5k8v4ib6oDv1fA==", + "license": "BSD-3-Clause", + "dependencies": { + "@hapi/hoek": "^9.3.0", + "@hapi/topo": "^5.1.0", + "@sideway/address": "^4.1.5", + "@sideway/formula": "^3.0.1", + "@sideway/pinpoint": "^2.0.0" + } + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -2441,6 +2490,24 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "node_modules/nanoid": { + "version": "5.0.7", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.0.7.tgz", + "integrity": "sha512-oLxFY2gd2IqnjcYyOXD8XGCftpGtZP2AbHbOkthDkvRywH5ayNtPVy9YlOPcHckXzbLTCHpkb7FB+yuxKV13pQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.js" + }, + "engines": { + "node": "^18 || >=20" + } + }, "node_modules/natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -3759,6 +3826,37 @@ "strip-json-comments": "^3.1.1" } }, + "@hapi/hoek": { + "version": "9.3.0", + "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz", + "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==" + }, + "@hapi/topo": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz", + "integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==", + "requires": { + "@hapi/hoek": "^9.0.0" + } + }, + "@sideway/address": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.5.tgz", + "integrity": "sha512-IqO/DUQHUkPeixNQ8n0JA6102hT9CmaljNTPmQ1u8MEhBo/R4Q8eKLN/vGZxuebwOroDB4cbpjheD4+/sKFK4Q==", + "requires": { + "@hapi/hoek": "^9.0.0" + } + }, + "@sideway/formula": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.1.tgz", + "integrity": "sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg==" + }, + "@sideway/pinpoint": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz", + "integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==" + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -5271,6 +5369,18 @@ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" }, + "joi": { + "version": "17.13.3", + "resolved": "https://registry.npmjs.org/joi/-/joi-17.13.3.tgz", + "integrity": "sha512-otDA4ldcIx+ZXsKHWmp0YizCweVRZG96J10b0FevjfuncLO1oX59THoAmHkNubYJ+9gWsYsp5k8v4ib6oDv1fA==", + "requires": { + "@hapi/hoek": "^9.3.0", + "@hapi/topo": "^5.1.0", + "@sideway/address": "^4.1.5", + "@sideway/formula": "^3.0.1", + "@sideway/pinpoint": "^2.0.0" + } + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -5488,6 +5598,11 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "nanoid": { + "version": "5.0.7", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.0.7.tgz", + "integrity": "sha512-oLxFY2gd2IqnjcYyOXD8XGCftpGtZP2AbHbOkthDkvRywH5ayNtPVy9YlOPcHckXzbLTCHpkb7FB+yuxKV13pQ==" + }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", From 8b0b6b4c2467c4df99dc36b1dd3334fb177b5d3a Mon Sep 17 00:00:00 2001 From: stefania m Date: Sun, 27 Oct 2024 17:38:54 +0200 Subject: [PATCH 5/5] done --- models/contacts.js | 97 ++++++++++++++++++++++++++++++++------- models/contacts.json | 44 ++++++++++++++---- package-lock.json | 34 +++++++++++++- package.json | 13 ++++-- routes/api/contacts.js | 101 +++++++++++++++++++++++++++++++++-------- 5 files changed, 241 insertions(+), 48 deletions(-) diff --git a/models/contacts.js b/models/contacts.js index 3e286a465c6..3981d86da57 100644 --- a/models/contacts.js +++ b/models/contacts.js @@ -1,22 +1,87 @@ -// const fs = require('fs/promises') +import { error } from "console"; +import e from "express"; +import fs from "fs/promises"; +import { nanoid } from "nanoid"; -const listContacts = async () => {} +const contactsPath = "./models/contacts.json"; -const getContactById = async (contactId) => {} +export const listContacts = async () => { + try { + const data = await fs.readFile(contactsPath, "utf8"); + return JSON.parse(data); + } catch (error) { + console.log(error.message); + throw error; + } +}; -const removeContact = async (contactId) => {} - -const addContact = async (body) => {} - -const updateContact = async (contactId, body) => {} - -module.exports = { - listContacts, - getContactById, - removeContact, - addContact, - updateContact, -} +export const getContactById = async (contactId) => { + try { + const data = await fs.readFile(contactsPath, "utf8"); + const contacts = JSON.parse(data); + const contact = contacts.find((contact) => contact.id === contactId); + return contact; + } catch (error) { + console.log(error.message); + throw error; + } +}; +export const addContact = async (body) => { + try { + const data = await fs.readFile(contactsPath, "utf8"); + const contacts = JSON.parse(data); + const newContact = { + id: nanoid(), + ...body, + }; + contacts.push(newContact); + await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2)); + return newContact; + } catch (error) { + console.log(error.message); + throw error; + } +}; +export const removeContact = async (contactId) => { + try { + const data = await fs.readFile(contactsPath, "utf8"); + const contacts = JSON.parse(data); + const deleteContact = contacts.find((contact) => contact.id === contactId); + if (!deleteContact) { + return null; + } + const newContacts = contacts.filter((contact) => contact.id !== contactId); + await fs.writeFile(contactsPath, JSON.stringify(newContacts, null, 2)); + return deleteContact; + } catch (error) { + console.log(error.message); + throw error; + } +}; +export const updateContact = async (contactId, body) => { + try { + const data = await fs.readFile(contactsPath, "utf8"); + const contacts = JSON.parse(data); + const contactIndex = contacts.findIndex( + (contact) => contact.id === contactId + ); + if (contactIndex === -1) { + throw new Error("Contact not found"); + } + const updatedContact = { + id: contactId, + name: body.name || contacts[contactIndex].name, + email: body.email || contacts[contactIndex].email, + phone: body.phone || contacts[contactIndex].phone, + }; + contacts[contactIndex] = updatedContact; + await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2)); + return updatedContact; + } catch (error) { + console.log(error.message); + throw error; + } +}; \ No newline at end of file diff --git a/models/contacts.json b/models/contacts.json index a21679132de..f1c9b7dcd58 100644 --- a/models/contacts.json +++ b/models/contacts.json @@ -1,15 +1,9 @@ [ { "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" + "name": "Hey 9", + "email": "hello.hellohey@example.com", + "phone": "199999" }, { "id": "drsAJ4SHPYqZeG-83QTVW", @@ -58,5 +52,35 @@ "name": "Alec Howard", "email": "Donec.elementum@scelerisquescelerisquedui.net", "phone": "(748) 206-2688" + }, + { + "id": "7fSsSg5NCnRiU36bQX9ae", + "name": "Hello Hey", + "email": "hello.hey@example.com", + "phone": "123456789" + }, + { + "id": "_62WqjtfodQWWMBlPbMIX", + "name": "Hello Heyy", + "email": "hello.heyy@example.com", + "phone": "1234567899" + }, + { + "id": "iAWcrrIC54CVbg6-zrC1K", + "name": "Hello Heyyy", + "email": "hello.heyyy@example.com", + "phone": "12345678999" + }, + { + "id": "HywEcC6l7vyZBkaP8giqC", + "name": "Hello Heyyyy", + "email": "hello.heyyyy@example.com", + "phone": "123456789999" + }, + { + "id": "oksI0WBi3GazkaHifcLHW", + "name": "Hey 999999", + "email": "hello.hellohey@example.com", + "phone": "1234599" } -] +] \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 853b829114d..eee9a646ea5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,10 +7,13 @@ "": { "name": "nodejs-homework-rest-api", "version": "0.0.0", + "license": "ISC", "dependencies": { + "console": "^0.7.2", "cors": "2.8.5", "cross-env": "7.0.3", - "express": "4.17.1", + "dotenv": "^16.4.5", + "express": "^4.17.1", "joi": "^17.13.3", "morgan": "1.10.0", "nanoid": "^5.0.7" @@ -679,6 +682,12 @@ "node": ">=8" } }, + "node_modules/console": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/console/-/console-0.7.2.tgz", + "integrity": "sha512-+JSDwGunA4MTEgAV/4VBKwUHonP8CzJ/6GIuwPi6acKFqFfHUdSGCm89ZxZ5FfGWdZfkdgAroy5bJ5FSeN/t4g==", + "license": "MIT" + }, "node_modules/content-disposition": { "version": "0.5.3", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", @@ -861,6 +870,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", @@ -1392,6 +1413,7 @@ "version": "4.17.1", "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz", "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==", + "license": "MIT", "dependencies": { "accepts": "~1.3.7", "array-flatten": "1.1.1", @@ -4238,6 +4260,11 @@ "xdg-basedir": "^4.0.0" } }, + "console": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/console/-/console-0.7.2.tgz", + "integrity": "sha512-+JSDwGunA4MTEgAV/4VBKwUHonP8CzJ/6GIuwPi6acKFqFfHUdSGCm89ZxZ5FfGWdZfkdgAroy5bJ5FSeN/t4g==" + }, "content-disposition": { "version": "0.5.3", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", @@ -4367,6 +4394,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 09169778cd5..f7f2081aa27 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,11 @@ "lint:fix": "eslint --fix **/*.js" }, "dependencies": { + "console": "^0.7.2", "cors": "2.8.5", "cross-env": "7.0.3", - "express": "4.17.1", + "dotenv": "^16.4.5", + "express": "^4.17.1", "joi": "^17.13.3", "morgan": "1.10.0", "nanoid": "^5.0.7" @@ -25,5 +27,10 @@ "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "5.2.0", "nodemon": "2.0.15" - } -} \ No newline at end of file + }, + "description": "Виконайте форк цього репозиторію для виконання домашніх завдань (2-6)\r Форк створить репозиторій на вашому http://github.com", + "main": ".eslintrc.js", + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/routes/api/contacts.js b/routes/api/contacts.js index a60ebd69231..69eab79befc 100644 --- a/routes/api/contacts.js +++ b/routes/api/contacts.js @@ -1,25 +1,90 @@ -const express = require('express') +import express from "express"; +import Joi from "joi"; -const router = express.Router() +import { + listContacts, + getContactById, + addContact, + removeContact, + updateContact, +} from "../../models/contacts.js"; -router.get('/', async (req, res, next) => { - res.json({ message: 'template message' }) -}) +const contactSchema = Joi.object({ + name: Joi.string().required(), + email: Joi.string().email().required(), + phone: Joi.string() + .pattern(/^[0-9]+$/) + .required(), +}); -router.get('/:contactId', async (req, res, next) => { - res.json({ message: 'template message' }) -}) +const router = express.Router(); -router.post('/', async (req, res, next) => { - res.json({ message: 'template message' }) -}) +router.get("/", async (req, res, next) => { + try { + const contacts = await listContacts(); + res.status(200).json(contacts); + } catch (error) { + next(error); + } +}); -router.delete('/:contactId', async (req, res, next) => { - res.json({ message: 'template message' }) -}) +router.get("/:contactId", async (req, res, next) => { + try { + const { contactId } = req.params; + const contact = await getContactById(contactId); + if (contact) { + res.status(200).json(contact); + } else { + res.status(404).json({ message: "Not found" }); + } + } catch (error) { + next(error); + } +}); -router.put('/:contactId', async (req, res, next) => { - res.json({ message: 'template message' }) -}) +router.post("/", async (req, res, next) => { + try { + const { error } = contactSchema.validate(req.body); + if (error) { + return res.status(400).json({ message: error.details[0].message }); + } + const contact = await addContact(req.body); + res.status(201).json(contact); + } catch (error) { + next(error); + } +}); -module.exports = router +router.delete("/:contactId", async (req, res, next) => { + try { + const { contactId } = req.params; + const contact = await removeContact(contactId); + if (contact) { + res.status(200).json({ message: "Contact Deleted" }); + } else { + res.status(404).json({ message: "Not found" }); + } + } catch (error) { + next(error); + } +}); + +router.put("/:contactId", async (req, res, next) => { + try { + const { error } = contactSchema.validate(req.body); + if (error) { + return res.status(400).json({ message: error.details[0].message }); + } + const { contactId } = req.params; + const updatedContact = await updateContact(contactId, req.body); + if (updatedContact) { + res.status(200).json(updatedContact); + } else { + res.status(404).json({ message: "Not found" }); + } + } catch (error) { + next(error); + } +}); + +export default router; \ No newline at end of file