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 express #54

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
File renamed without changes.
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.jpg
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.jpg
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.jpg
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.jpg
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.jpg
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 };
136 changes: 124 additions & 12 deletions package-lock.json

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

Loading