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
31 changes: 16 additions & 15 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')
const contactsRouter = require("./routes/api/contactsRouter");

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 })
})
const { status = 500, message = "server error" } = err;
res.status(status).json({ message });
});

module.exports = app
module.exports = app;
15 changes: 15 additions & 0 deletions controllers/contacts/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const contactOperations = require("../../models/contacts");

const add = async (req, res) => {
const { body } = req;
const addedContact = await contactOperations.addContact(body);
res.status(201).json({
status: "success",
code: 201,
data: {
result: addedContact,
},
});
};

module.exports = add;
14 changes: 14 additions & 0 deletions controllers/contacts/getAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const contactOperations = require("../../models/contacts");

const getAll = async (req, res) => {
const allContact = await contactOperations.listContacts();
res.json({
status: "success",
code: 200,
data: {
result: allContact,
},
});
};

module.exports = getAll;
20 changes: 20 additions & 0 deletions controllers/contacts/getById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const contactOperations = require("../../models/contacts");

const getById = async (req, res) => {
const { contactId } = req.params;
const contact = await contactOperations.getContactById(contactId);
if (!contact) {
const error = new Error(`contact whith id = ${contactId} not found`);
error.status = 404;
throw error;
}
res.json({
status: "success",
code: 200,
data: {
result: contact,
},
});
};

module.exports = getById;
13 changes: 13 additions & 0 deletions controllers/contacts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const getAll = require("./getAll");
const getById = require("./getById");
const add = require("./add");
const remove = require("./remove");
const update = require("./update");

module.exports = {
getAll,
getById,
add,
remove,
update,
};
20 changes: 20 additions & 0 deletions controllers/contacts/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const contactOperations = require("../../models/contacts");

const remove = async (req, res) => {
const { contactId } = req.params;
const contactToDelete = await contactOperations.removeContact(contactId);
if (!contactToDelete) {
const error = new Error(`contact whith id = ${contactId} not found`);
error.status = 404;
throw error;
}
res.json({
status: "success",
code: 200,
data: {
result: contactToDelete,
},
});
};

module.exports = remove;
24 changes: 24 additions & 0 deletions controllers/contacts/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const contactOperations = require("../../models/contacts");

const update = async (req, res) => {
const { body } = req;
const { contactId } = req.params;
const contactToUpdate = await contactOperations.updateContact(
contactId,
body
);
if (!contactToUpdate) {
const error = new Error(`contact whith id = ${contactId} not found`);
error.status = 404;
throw error;
}
res.json({
status: "success",
code: 200,
data: {
result: contactToUpdate,
},
});
};

module.exports = update;
5 changes: 5 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const contactsControllers = require("./contacts");

module.exports = {
contactsControllers,
};
10 changes: 10 additions & 0 deletions middlewars/cntrlWrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const cntrlWrap = (cntrl) => {
return async (req, res, next) => {
try {
await cntrl(req, res, next);
} catch (error) {
next(error);
}
};
};
module.exports = cntrlWrap;
7 changes: 7 additions & 0 deletions middlewars/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const validation = require("./validation");
const cntrlWrap = require("./cntrlWrap");

module.exports = {
validation,
cntrlWrap,
};
12 changes: 12 additions & 0 deletions middlewars/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const validation = (schema) => {
return (req, res, next) => {
const { body } = req;
const { error } = schema.validate(body);
if (error) {
error.status = 400;
next(error);
}
next();
};
};
module.exports = validation;
53 changes: 46 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
// const fs = require('fs/promises')
const fs = require("fs/promises");
const path = require("path");
const { v4 } = 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);
const contacts = await JSON.parse(data);
return contacts;
};

const removeContact = async (contactId) => {}
const getContactById = async (contactId) => {
const data = await listContacts();
const contact = data.find((item) => item.id === contactId);
if (!contact) {
return null;
}
return contact;
};

const addContact = async (body) => {}
const removeContact = async (contactId) => {
const data = await listContacts();
const idx = data.findIndex((item) => item.id === contactId);
if (idx === -1) {
return null;
}
const updatedContacts = data.filter((_, index) => index !== idx);
await fs.writeFile(contactsPath, JSON.stringify(updatedContacts));
return data[idx];
};

const updateContact = async (contactId, body) => {}
const addContact = async (body) => {
const result = await listContacts();
const addedContact = { ...body, id: v4() };
result.push(addedContact);
await fs.writeFile(contactsPath, JSON.stringify(result));
return addedContact;
};

const updateContact = async (contactId, body) => {
const result = await listContacts();
const idx = result.findIndex((item) => item.id === contactId);
if (idx === -1) {
return null;
}
result[idx] = { ...body, id: contactId };
await fs.writeFile(contactsPath, JSON.stringify(result));
return result[idx];
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
};
8 changes: 4 additions & 4 deletions models/contacts.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{
"id": "1",
"name": "Allen Raymond",
"email": "nulla.ante@vestibul.co.uk",
"phone": "(992) 914-3792"
"name": "Allen Limo",
"email": "Allen.ante@vestibul.co.uk",
"phone": "(992) 914-3792",
"id": "1"
},
{
"id": "2",
Expand Down
Loading