Skip to content
Merged
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
35 changes: 18 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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'); // Import the contacts route

const contactsRouter = require('./routes/api/contacts')
const app = express();

const app = express()
// Middleware setup
app.use(logger('dev')); // Logging
app.use(cors()); // Allow CORS
app.use(express.json()); // Middleware to parse JSON requests

const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'

app.use(logger(formatsLogger))
app.use(cors())
app.use(express.json())

app.use('/api/contacts', contactsRouter)
// Use the contacts router
app.use('/api/contacts', contactsRouter);

// 404 handler for unknown routes
app.use((req, res) => {
res.status(404).json({ message: 'Not found' })
})
res.status(404).json({ message: 'Not found' });
});

// Global error handler
app.use((err, req, res, next) => {
res.status(500).json({ message: err.message })
})
res.status(500).json({ message: err.message });
});

module.exports = app
module.exports = app;
50 changes: 37 additions & 13 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
// 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, '../data/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 getById = async (id) => {
const contacts = await listContacts();
return contacts.find((contact) => contact.id === id) || null;
};

const addContact = async (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 removeContact = async (id) => {
const contacts = await listContacts();
const filteredContacts = contacts.filter((contact) => contact.id !== id);
if (contacts.length === filteredContacts.length) return false;
await fs.writeFile(contactsPath, JSON.stringify(filteredContacts, null, 2));
return true;
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
const updateContact = async (id, updatedFields) => {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === id);
if (index === -1) return null;

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

module.exports = { listContacts, getById, addContact, removeContact, updateContact };
Loading