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
33 changes: 17 additions & 16 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')
import express from "express";
import logger from "morgan";
import cors from "cors";
import contactsRouter from "./routes/api/contacts.js";
import connectToDb from "./utils/connectToDb.js";
const app = express();

const contactsRouter = require('./routes/api/contacts')
const formatsLogger = app.get("env") === "development" ? "dev" : "short";

const app = express()
connectToDb();

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
export default app;
66 changes: 66 additions & 0 deletions controller/contactsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// import (promises as fs) from fs;
// eslint-disable-next-line
import { v4 as uuidv4 } from "uuid";
import Contact from "../models/contacts.js";

const ContactsController = {
listContacts,
getContactsById,
addContact,
updateStatusContact,
updateContact,
deleteContact,
};

async function listContacts() {
console.log("--- List Contacts --- ");
try {
return Contact.find();
} catch (error) {
console.error(error);
}
}

async function getContactsById(id) {
console.log(`--- List Contacts by id #{id} --- `);
try {
return Contact.findById(id);
} catch (error) {
console.error(error);
}
}

async function addContact(contact) {
return Contact.create(contact);
}



async function deleteContact(contactId) {
return Contact.findByIdAndDelete(contactId);
}

async function updateContact(contactId, updatedData) {
try {
// Găsește contactul după ID și actualizează complet
return await Contact.findByIdAndUpdate(contactId, updatedData, { new: true, runValidators: true });
} catch (error) {
console.error("Error updating contact:", error);
return null;
}
}


async function updateStatusContact(contactId, updateData) {
try {
// Găsește contactul după ID și actualizează câmpul `favorite`
return await Contact.findByIdAndUpdate(contactId, updateData, { new: true });
} catch (error) {
console.error("Error updating contact status:", error);
return null;
}
}



export default ContactsController;
41 changes: 24 additions & 17 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
// const fs = require('fs/promises')
import mongoose from "mongoose";
const { Schema, model } = mongoose;

const listContacts = async () => {}
const schema = new Schema(
{
name: {
type: String,
required: [true, 'Set name for contact'],
},
email: {
type: String,
},
phone: {
type: String,
},
favorite: {
type: Boolean,
default: false,
},
},

{ collection: "contacts" }
);
const Contact = model("Contact", schema);

const getContactById = async (contactId) => {}

const removeContact = async (contactId) => {}

const addContact = async (body) => {}

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

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
export default Contact;
40 changes: 20 additions & 20 deletions models/contacts.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
[
{
"id": "AeHIrLTr6JkxGE6SN-0Rw",
"name": "Allen Raymond",
"email": "[email protected]",
"phone": "(992) 914-3792"
"phone": "(992) 914-3792",
"favorite": false
},
{
"id": "qdggE76Jtbfd9eWJHrssH",
"name": "Chaim Lewis",
"email": "[email protected]",
"phone": "(294) 840-6685"
"phone": "(294) 840-6685",
"favorite": true
},
{
"id": "drsAJ4SHPYqZeG-83QTVW",
"name": "Kennedy Lane",
"email": "[email protected]",
"phone": "(542) 451-7038"
"phone": "(542) 451-7038",
"favorite": false
},
{
"id": "vza2RIzNGIwutCVCs4mCL",
"name": "Wylie Pope",
"email": "[email protected]",
"phone": "(692) 802-2949"
"phone": "(692) 802-2949",
"favorite": true
},
{
"id": "05olLMgyVQdWRwgKfg5J6",
"name": "Cyrus Jackson",
"email": "[email protected]",
"phone": "(501) 472-5218"
"phone": "(501) 472-5218",
"favorite": true
},
{
"id": "1DEXoP8AuCGYc1YgoQ6hw",
"name": "Abbot Franks",
"email": "[email protected]",
"phone": "(186) 568-3720"
"phone": "(186) 568-3720",
"favorite": true
},
{
"id": "Z5sbDlS7pCzNsnAHLtDJd",
"name": "Reuben Henry",
"email": "[email protected]",
"phone": "(715) 598-5792"
"phone": "(715) 598-5792",
"favorite": true
},
{
"id": "C9sjBfCo4UJCWjzBnOtxl",
"name": "Simon Morton",
"email": "[email protected]",
"phone": "(233) 738-2360"
"phone": "(233) 738-2360",
"favorite": true
},
{
"id": "e6ywwRe4jcqxXfCZOj_1e",
"name": "Thomas Lucas",
"email": "[email protected]",
"phone": "(704) 398-7993"
"phone": "(704) 398-7993",
"favorite": false
},
{
"id": "rsKkOQUi80UsgVPCcLZZW",
"name": "Alec Howard",
"email": "[email protected]",
"phone": "(748) 206-2688"
"phone": "(748) 206-2688",
"favorite": true
}
]
Loading