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
12 changes: 12 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true,
},
extends: ['standard', 'prettier'],
parserOptions: {
ecmaVersion: 12,
},
rules: {},
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
.env
.idea
.vscode
34 changes: 18 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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 { router as contactsRouter } from "./routes/api/contactsRouter.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 })
})
app.use((err, _req, res, _next) => {
const { status = 500, message = "Server error" } = err;
res.status(status).json({ message });
});

module.exports = app
// module.exports = app;
export { app };
15 changes: 15 additions & 0 deletions helpers/HttpError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const messages = {
400: "Bad request",
401: "Unauthorized",
403: "Forbidden",
404: "Not found",
409: "Conflict",
};

const httpError = (status, message = messages[status]) => {
const error = new Error(message);
error.status = status;
return error;
};

export { httpError };
Binary file added images/DELETE.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/GET_all.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/GET_id.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Post.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Put.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 58 additions & 13 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
// const fs = require('fs/promises')
import fs from "fs/promises";
import path from "path";
import { nanoid } from "nanoid";

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

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

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

const addContact = async (body) => {}
const removeContact = async (contactId) => {
const contacts = await listContacts();
const index = contacts.findIndex((item) => item.id === contactId);

const updateContact = async (contactId, body) => {}
if (index === -1) {
return null;
}

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
const deletedContact = contacts.splice(index, 1);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return deletedContact;
};

const addContact = async ({ name, email, phone }) => {
const contacts = await listContacts();
const newContact = {
id: nanoid(),
name,
email,
phone,
};
const allContacts = [...contacts, newContact];
await fs.writeFile(contactsPath, JSON.stringify(allContacts, null, 2));
return newContact;
};

const updateContact = async (id, { name, email, phone }) => {
const contacts = await listContacts();
const index = contacts.findIndex((item) => item.id === id);

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

contacts[index] = {
id,
name,
email,
phone,
};

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

// prettier-ignore
export { listContacts, getContactById, removeContact, addContact, updateContact };
28 changes: 17 additions & 11 deletions models/contacts.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,68 @@
[
{
"id": "1",
"id": "AeHIrLTr6JkxGE6SN-0Rw",
"name": "Allen Raymond",
"email": "nulla.ante@vestibul.co.uk",
"phone": "(992) 914-3792"
},
{
"id": "2",
"id": "qdggE76Jtbfd9eWJHrssH",
"name": "Chaim Lewis",
"email": "dui.in@egetlacus.ca",
"phone": "(294) 840-6685"
},
{
"id": "3",
"id": "drsAJ4SHPYqZeG-83QTVW",
"name": "Kennedy Lane",
"email": "mattis.Cras@nonenimMauris.net",
"phone": "(542) 451-7038"
},
{
"id": "4",
"id": "vza2RIzNGIwutCVCs4mCL",
"name": "Wylie Pope",
"email": "est@utquamvel.net",
"phone": "(692) 802-2949"
},
{
"id": "5",
"id": "05olLMgyVQdWRwgKfg5J6",
"name": "Cyrus Jackson",
"email": "nibh@semsempererat.com",
"phone": "(501) 472-5218"
},
{
"id": "6",
"id": "1DEXoP8AuCGYc1YgoQ6hw",
"name": "Abbot Franks",
"email": "scelerisque@magnis.org",
"phone": "(186) 568-3720"
},
{
"id": "7",
"id": "Z5sbDlS7pCzNsnAHLtDJd",
"name": "Reuben Henry",
"email": "pharetra.ut@dictum.co.uk",
"phone": "(715) 598-5792"
},
{
"id": "8",
"id": "C9sjBfCo4UJCWjzBnOtxl",
"name": "Simon Morton",
"email": "dui.Fusce.diam@Donec.com",
"phone": "(233) 738-2360"
},
{
"id": "9",
"id": "e6ywwRe4jcqxXfCZOj_1e",
"name": "Thomas Lucas",
"email": "nec@Nulla.com",
"phone": "(704) 398-7993"
},
{
"id": "10",
"id": "rsKkOQUi80UsgVPCcLZZW",
"name": "Alec Howard",
"email": "Donec.elementum@scelerisquescelerisquedui.net",
"phone": "(748) 206-2688"
},
{
"id": "9t_um3FNF8_91GjzK8OJi",
"name": "Reuben Henry",
"email": "pharetra.ut@dictum.co.uk",
"phone": "(715) 598-5792"
}
]
]
Loading