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
49 changes: 33 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
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')
require("dotenv").config();

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

const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'
const app = express();

app.use(logger(formatsLogger))
app.use(cors())
app.use(express.json())
const formatsLogger = app.get("env") === "development" ? "dev" : "short";

app.use('/api/contacts', contactsRouter)
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 })
})

module.exports = app
res.status(500).json({ message: err.message });
});

const { DB_HOST, PORT = 3000 } = process.env;

mongoose
.connect(DB_HOST)
.then(() => {
console.log("Database connection succesful^_^");
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
})
.catch((error) => {
console.error("Database connection error:", error.message);
});

module.exports = app;
99 changes: 99 additions & 0 deletions controllers/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const Contact = require("../models/contact");

//GET /api/contacts
const listContacts = async (req, res, next) => {
try {
const contacts = await Contact.find();
res.status(200).json(contacts);
} catch (error) {
next(error);
}
};

//GET /api/contacts/:id
const getContactById = async (req, res, next) => {
try {
const { id } = req.params;
const contact = await Contact.findById(id);
if (!contact) {
return res.status(404).json({ message: "Contact not found ^_^" });
}
res.status(200).json(contact);
} catch (error) {
next(error);
}
};

//POST /api/contacts
const addContact = async (req, res, next) => {
try {
const { name, email, phone } = req.body;
if (!name || !email || !phone) {
return res.status(400).json({ message: "Missing required fields!^_^" });
}
const newContact = new Contact({ name, email, phone });
await newContact.save();
res.status(201).json(newContact);
} catch (error) {
next(error);
}
};

//DELETE /api/contacts/:id
const removeContact = async (req, res, next) => {
try {
const { id } = req.params;
const contact = await Contact.findByIdAndDelete(id);
if (!contact) {
return res.status(404).json({ message: "Contact not found ^_^" });
}
res.status(200).json({ message: "Contact deleted ^_^" });
} catch (error) {
next(error);
}
};

//PUT /api/contacts/:id
const updateContact = async (req, res, next) => {
try {
const { id } = req.params;
const updateData = req.body;
if (!Object.keys(updateData).length) {
return res.status(400).json({ message: "Missing fileds! ^_^" });
}
const contact = await Contact.findByIdAndUpdate(id, updateData, { new: true });
if (!contact) {
res.status(404).json({ message: "Contact not found ^_^" });
}
res.status(200).json(contact);
} catch (error) {
next(error);
}
};

//PATCH /api/contacts/:id/favorite
const updateFavorite = async (req, res, next) => {
try {
const { id } = req.params;
const { favorite } = req.body;
if (typeof favorite !== "boolean") {
return res.status(400).json({ message: "Missing field favorite! ^_^" });
}
const contact = await Contact.findByIdAndUpdate(id, { favorite }, { new: true });
if (!contact) {
return res.status(404).json | { message: "Contact not found ^_^" };
}
res.status(200).json(contact);
} catch (error) {
next(error);
}
};

module.exports = {
listContacts,
getContactById,
addContact,
removeContact,
updateContact,
updateFavorite,
};
24 changes: 24 additions & 0 deletions models/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require("mongoose");

const contactSchema = new mongoose.Schema({
name: {
type: String,
required: [true, "Set the contact name"],
},
email: {
type: String,
required: [true, "Set the contact email"],
},
phone: {
type: String,
required: [true, "Set the contact phone"],
},
favorite: {
type: Boolean,
default: false,
},
});

const Contact = mongoose.model("Contact", contactSchema);

module.exports = Contact;
19 changes: 0 additions & 19 deletions models/contacts.js

This file was deleted.

62 changes: 0 additions & 62 deletions models/contacts.json

This file was deleted.

Loading