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
45 changes: 29 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
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 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 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.get("/", (req, res) => {
res.send("API is up and running!");
});

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 })
})

module.exports = app
console.error(err.stack);
res.status(err.status || 500).json({
message: err.message || "Server error",
stack: process.env.NODE_ENV === "development" ? err.stack : undefined,
});
});

const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});

module.exports = app;
48 changes: 41 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
// const fs = require('fs/promises')
const fs = require("fs/promises");
const path = require("path");
const { v4: uuidv4 } = require("uuid");

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

const getContactById = async (contactId) => {}
const listContacts = async () => {
const data = await fs.readFile(contactsPath, "utf8");
return JSON.parse(data);
};

const removeContact = async (contactId) => {}
const getContactById = async (contactId) => {
const contacts = await listContacts();
return contacts.find((contact) => contact.id === contactId) || null;
};

const addContact = async (body) => {}
const removeContact = async (contactId) => {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) {
return null;
}
const [removedContact] = contacts.splice(index, 1);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return removedContact;
};

const updateContact = async (contactId, body) => {}
const addContact = async ({ name, email, phone }) => {
const contacts = await listContacts();
const newContact = { id: uuidv4(), name, email, phone };
contacts.push(newContact);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return newContact;
};

const updateContact = async (contactId, body) => {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) {
return null;
}
contacts[index] = { ...contacts[index], ...body };
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return contacts[index];
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
};
118 changes: 115 additions & 3 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "template",
"name": "silvia",
"version": "0.0.0",
"private": true,
"scripts": {
Expand All @@ -12,7 +12,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",
"uuid": "^11.0.3"
},
"devDependencies": {
"eslint": "7.19.0",
Expand Down
Loading