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
30 changes: 15 additions & 15 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 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;
94 changes: 81 additions & 13 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +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) => {}
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;
}
};

const addContact = async (body) => {}
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;
}
};

const updateContact = async (contactId, body) => {}
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;
}
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
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;
}
};
44 changes: 34 additions & 10 deletions models/contacts.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
]
]
Loading