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
39 changes: 24 additions & 15 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
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 mongoose = require("mongoose");

const contactsRouter = require('./routes/api/contacts')
const contactsRouter = require("./routes/api/contacts");

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

const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'
const mongoURI =
"mongodb+srv://sstobiecki93:[email protected]/db-contacts";

app.use(logger(formatsLogger))
app.use(cors())
app.use(express.json())
mongoose
.connect(mongoURI)
.then(() => console.log("MongoDB connected successfully"))
.catch((err) => console.error("MongoDB connection error:", err));

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

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

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
module.exports = app;
17 changes: 17 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require("mongoose");

const connectDB = async () => {
try {
const uri = "mongodb://localhost:27017/contactsDB";
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Database connection successful");
} catch (error) {
console.error("Database connection error:", error.message);
process.exit(1);
}
};

module.exports = connectDB;
91 changes: 84 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,96 @@
// const fs = require('fs/promises')
const { Schema, model } = require("mongoose");

const listContacts = async () => {}
const contactSchema = new Schema(
{
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
phone: {
type: String,
required: true,
},
favorite: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);

const getContactById = async (contactId) => {}
const Contact = model("Contact", contactSchema);

const removeContact = async (contactId) => {}
const listContacts = async () => {
try {
return await Contact.find();
} catch (error) {
console.error("Error listing contacts:", error.message);
return [];
}
};

const addContact = async (body) => {}
const getContactById = async (contactId) => {
try {
return await Contact.findById(contactId);
} catch (error) {
console.error("Error getting contact by id:", error.message);
return null;
}
};

const updateContact = async (contactId, body) => {}
const removeContact = async (contactId) => {
try {
return await Contact.findByIdAndRemove(contactId);
} catch (error) {
console.error("Error removing contact:", error.message);
return null;
}
};

const addContact = async (body) => {
try {
const newContact = new Contact(body);
await newContact.save();
return newContact;
} catch (error) {
console.error("Error adding contact:", error.message);
return null;
}
};

const updateContact = async (contactId, body) => {
try {
return await Contact.findByIdAndUpdate(contactId, body, { new: true });
} catch (error) {
console.error("Error updating contact:", error.message);
return null;
}
};

const updateStatusContact = async (contactId, { favorite }) => {
try {
const updatedContact = await Contact.findByIdAndUpdate(
contactId,
{ favorite },
{ new: true }
);
return updatedContact;
} catch (error) {
console.error("Error updating contact status:", error.message);
return null;
}
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
updateStatusContact,
};
14 changes: 10 additions & 4 deletions models/contacts.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
{
"id": "AeHIrLTr6JkxGE6SN-0Rw",
"name": "Allen Raymond",
"email": "[email protected]",
"phone": "(992) 914-3792"
"name": "Sebastian",
"email": "[email protected]",
"phone": "123456789"
},
{
"id": "qdggE76Jtbfd9eWJHrssH",
Expand Down Expand Up @@ -58,5 +58,11 @@
"name": "Alec Howard",
"email": "[email protected]",
"phone": "(748) 206-2688"
},
{
"id": "c967f137-6519-4c36-a0a2-1110e98fce27",
"name": "Aaaaa Test",
"email": "[email protected]",
"phone": "11111111"
}
]
]
Loading