Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -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 { router as contactsRouter } from "./routes/api/contactsRouter.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('/api/contacts', contactsRouter)
app.use(logger(formatsLogger));
app.use(cors());
app.use(express.json());
// http://localhost:3000/api/contacts
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 { app };
15 changes: 15 additions & 0 deletions helpers/httpError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const messages = {
400: "Bad request",
401: "Unauthorized",
403: "Forbidden",
404: "Not found",
409: "Conflict",
};

const httpError = (status, message = messages[status]) => {
const error = new Error(message);
error.status = status;
return error;
};

export { httpError };
Binary file added images/Delete_contact.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Get_allContacts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Get_by_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Post_contact (2).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Post_contact.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Postman.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Put_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 73 additions & 13 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,79 @@
// const fs = require('fs/promises')
import fs from "fs/promises";
// const fs from "fs/promises";
import path from "path";
import { nanoid } from "nanoid";

const listContacts = async () => {}
const contactsPath = path.join("models", "contacts.json");

const getContactById = async (contactId) => {}
const listContacts = async () => {
try {
const contacts = await fs.readFile(contactsPath);
return JSON.parse(contacts);
} catch (error) {
console.error("Error reading contacts:", error.message);
}
};

const removeContact = async (contactId) => {}
const getContactById = async (contactId) => {
try {
const contacts = await listContacts();
const result = contacts.find((contact) => contact.id === contactId);
return result || null;
} catch (error) {
console.error("Error reading contacts by id:", error.message);
}
};

const addContact = async (body) => {}
const removeContact = async (contactId) => {
try {
const contacts = await listContacts();
const index = contacts.findIndex((item) => item.id === contactId);
if (index === -1) {
return null;
}
const deletedContact = contacts.splice(index, 1);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return deletedContact;
} catch (error) {
console.error("Error removing contact:", error.message);
}
};

const updateContact = async (contactId, body) => {}
const addContact = async ({ name, email, phone }) => {
try {
const contacts = await listContacts();
const newContact = {
id: nanoid(),
name,
email,
phone,
};
const allContacts = [...contacts, newContact];
await fs.writeFile(contactsPath, JSON.stringify(allContacts, null, 2));
return newContact;
} catch (error) {
console.error("Error adding new contact:", error.message);
}
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
const updateContact = async (id, { name, email, phone }) => {
const contacts = await listContacts();
const index = contacts.findIndex((item) => item.id === id);

if (index === -1) {
return null;
}

contacts[index] = {
id,
name,
email,
phone,
};

await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return contacts[index];
};

// prettier-ignore
export { listContacts, getContactById, removeContact, addContact, updateContact };
20 changes: 13 additions & 7 deletions models/contacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@
"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",
Expand All @@ -58,5 +52,17 @@
"name": "Alec Howard",
"email": "Donec.elementum@scelerisquescelerisquedui.net",
"phone": "(748) 206-2688"
},
{
"id": "FolUehVRM8pfWysPJ6Ksx",
"name": "Manuel Saavedra Jr",
"email": "newojunior@gmail.com",
"phone": "(649) 5555"
},
{
"id": "5xTD4iIVlbRp6bzQHncm8",
"name": "Simon Morton",
"email": "dui.Fusce.diam@Donec.com",
"phone": "(233) 738-2360"
}
]
]
6 changes: 6 additions & 0 deletions models/mockData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const mockData = [
{ id: 1, name: "Owen Saavedra", email: "newojunior@gmail.com" },
{ id: 2, name: "Newo Ardevaz", email: "manuelcsaavedrajr@gmail.com" },
{ id: 3, name: "Melanie Saavedra", email: "malaniesaavedra@gmail.com" },
{ id: 4, name: "Melanie Cetron", email: "malaniecetron@gmail.com" },
];
112 changes: 111 additions & 1 deletion package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "template",
"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",
Expand All @@ -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",
Expand Down
25 changes: 0 additions & 25 deletions routes/api/contacts.js

This file was deleted.

Loading