Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hw02 #5494

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

hw02 #5494

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')
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.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
module.exports = app;
67 changes: 60 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
// const fs = require('fs/promises')
const { read } = require("fs");
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 readContactsFile = async () => {
try {
const data = await fs.readFile(contactsPath, "utf-8");
return JSON.parse(data);
} catch (error) {
console.error("Error reading contacts file:", error.message);
return [];
}
};

const removeContact = async (contactId) => {}
const writeContactsFile = async (contacts) => {
try {
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
} catch (error) {
console.error("Error writing contacts file:", error.message);
}
};

const addContact = async (body) => {}
const listContacts = async () => {
return await readContactsFile();
};

const updateContact = async (contactId, body) => {}
const getContactById = async (contactId) => {
const contacts = await readContactsFile();
return contacts.find((contact) => contact.id === contactId) || null;
};

const removeContact = async (contactId) => {
const contacts = await readContactsFile();
const index = contacts.findIndex((contact) => contact.id === contactId);

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

const [removedContact] = contacts.splice(index, 1);
await writeContactsFile(contacts);
return removedContact;
};

const addContact = async (body) => {
const contacts = await readContactsFile();
const newContact = { id: uuidv4(), ...body };

contacts.push(newContact);
await writeContactsFile(contacts);
return newContact;
};

const updateContact = async (contactId, body) => {
const contacts = await readContactsFile();
const index = contacts.findIndex((contact) => contact.id === contactId);

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

contacts[index] = { ...contacts[index], ...body };
await writeContactsFile(contacts);
return contacts[index];
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
};
14 changes: 10 additions & 4 deletions models/contacts.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{
"id": "AeHIrLTr6JkxGE6SN-0Rw",
"name": "Allen Raymond",
"email": "[email protected]",
"phone": "(992) 914-3792"
"name": "Sebastian",
"email": "[email protected]",
"phone": "123456789"
},
{
"id": "qdggE76Jtbfd9eWJHrssH",
Expand Down Expand Up @@ -58,5 +58,11 @@
"name": "Alec Howard",
"email": "[email protected]",
"phone": "(748) 206-2688"
},
{
"id": "c967f137-6519-4c36-a0a2-1110e98fce27",
"name": "Aaaaa Test",
"email": "[email protected]",
"phone": "11111111"
}
]
]
114 changes: 113 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.2"
},
"devDependencies": {
"eslint": "7.19.0",
Expand Down
Loading