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 = {
extends: "eslint:recommended",
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
env: {
browser: true,
node: true,
},
};

12 changes: 0 additions & 12 deletions .eslintrc.js

This file was deleted.

40 changes: 24 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
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 contactsRouter from './routes/api/contacts.js';

const app = express()
const app = express();

const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'
// Setăm formatul pentru logger în funcție de mediul de rulare
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';

app.use(logger(formatsLogger))
app.use(cors())
app.use(express.json())
// Middleware
app.use(logger(formatsLogger));
app.use(cors());
app.use(express.json());

app.use('/api/contacts', contactsRouter)
// Rutele pentru contactele API
app.use('/api/contacts', contactsRouter);

// Middleware pentru rutele nevalide
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 })
})
// Middleware pentru gestionarea erorilor
app.use((err, req, res) => {
const status = err.status || 500;
const message = status === 500 ? 'Internal Server Error' : err.message;
res.status(status).json({ message });
});

export default app;

module.exports = app
63 changes: 51 additions & 12 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
// const fs = require('fs/promises')
import fs from "fs/promises";
import path from "path";

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

const getContactById = async (contactId) => {}

const removeContact = async (contactId) => {}

const addContact = async (body) => {}

const updateContact = async (contactId, body) => {}

module.exports = {
const ContactsService = {
listContacts,
getContactById,
removeContact,
addContact,
removeContact,
updateContact,
};

async function listContacts() {
const data = await fs.readFile(contactsPath, "utf-8");
return JSON.parse(data);
}

async function getContactById(id) {
const contacts = await listContacts();
return contacts.find((contact) => contact.id === id);
}

async function addContact(newContact) {
const contacts = await listContacts();
const contact = { id: Date.now().toString(), ...newContact };
contacts.push(contact);
await saveContactsToFile(contacts);
return contact;
}

async function removeContact(id) {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === id);
if (index === -1) return null;
const [deletedContact] = contacts.splice(index, 1);
await saveContactsToFile(contacts);
return deletedContact;
}

async function updateContact(id, updatedData) {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === id);
if (index === -1) return null;
contacts[index] = { ...contacts[index], ...updatedData };
await saveContactsToFile(contacts);
return contacts[index];
}

async function saveContactsToFile(contacts) {
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2), "utf-8");
}

export default ContactsService;




14 changes: 10 additions & 4 deletions models/contacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@
},
{
"id": "rsKkOQUi80UsgVPCcLZZW",
"name": "Alec Howard",
"email": "[email protected]",
"phone": "(748) 206-2688"
"name": "Updated Name",
"email": "[email protected]",
"phone": "1234567890"
},
{
"id": "1732362678654",
"name": "Updated Name",
"email": "[email protected]",
"phone": "1234567890"
}
]
]
93 changes: 93 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
"cors": "2.8.5",
"cross-env": "7.0.3",
"express": "4.17.1",
"joi": "^17.13.3",
"morgan": "1.10.0"
},
"type": "module",
"devDependencies": {
"eslint": "7.19.0",
"eslint-config-prettier": "^8.3.0",
Expand Down
Loading