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

install monogdb #45

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
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ app.use((err, req, res, next) => {
res.status(500).json({ message: err.message })
})

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

const schema = Joi.object({
name: Joi.string().required(),
email: Joi.string()
.email({
minDomainSegments: 2,
tlds: { allow: ["com", "net"] },
})
.required(),
phone: Joi.number().integer().positive().required(),
favorite: Joi.bool(),
});

const get = async (req, res, next) => {
try {
const results = await service.getAllContacts();
res.json({
status: 200,
data: { contacts: results },
});
} catch (err) {
console.error("Error getting contacts list:", err);
next(err);
}
};

const getById = async (req, res, next) => {
const id = req.params.contactId;

try {
const result = await service.getContactById(id);
if (result) {
return res.json({
status: 200,
data: { contact: result },
});
}

res.status(404).json({
status: 404,
message: "Not found",
});
} catch (err) {
console.error("Error getting contact:", err);
next(err);
}
};

const remove = async (req, res, next) => {
const id = req.params.contactId;

try {
const result = await service.removeContact(id);
if (result) {
return res.json({
status: 200,
message: "Contact deleted",
});
}

res.status(404).json({
status: 404,
message: "Not found",
});
} catch (err) {
console.error("Error removing contact:", err);
next(err);
}
};

const create = async (req, res, next) => {
const { error } = schema.validate(req.body);

if (error) {
return res.status(400).json({
status: 400,
message: error.message,
});
}

try {
const result = await service.createContact(req.body);
res.status(201).json({
status: 201,
data: { newContact: result },
});
} catch (err) {
console.error("Error creating contact:", err);
next(err);
}
};

const update = async (req, res, next) => {
const id = req.params.contactId;
const { error } = schema.validate(req.body);

if (error) {
return res.status(400).json({
status: 400,
message: error.message,
});
}

try {
const result = await service.updateContact(id, req.body);
if (result) {
return res.json({
status: 200,
data: { newContact: result },
});
}

res.status(404).json({
status: 404,
message: "Not found",
});
} catch (err) {
console.error("Error updating contact:", err);
next(err);
}
};

const updateStatus = async (req, res, next) => {
const { favorite } = req.body;

if (favorite === undefined) {
return res.status(400).json({
message: "missing field favorite",
});
}

try {
const result = await service.updateStatusContact(req.params.contactId, { favorite });
if (result) {
return res.status(200).json(result);
}

res.status(404).json({ message: "Not found" });
} catch (error) {
next(error);
}
};

module.exports = {
get,
getById,
remove,
create,
update,
updateStatus,
};
49 changes: 42 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
// const fs = require('fs/promises')
const mongoose = require('mongoose');
const { Schema } = mongoose;

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

const getContactById = async (contactId) => {}
const Contact = mongoose.model('Contact', contactSchema);

const removeContact = async (contactId) => {}
const listContacts = async () => {
return await Contact.find();
};

const addContact = async (body) => {}
const getContactById = async (contactId) => {
return await Contact.findById(contactId);
};

const updateContact = async (contactId, body) => {}
const removeContact = async (contactId) => {
return await Contact.findByIdAndRemove(contactId);
};

const addContact = async (body) => {
return await Contact.create(body);
};

const updateContact = async (contactId, body) => {
return await Contact.findByIdAndUpdate(contactId, body, { new: true });
};

const updateStatusContact = async (contactId, body) => {
return Contact.findByIdAndUpdate(contactId, { favorite: body.favorite }, { new: true });
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
updateStatusContact,
};
Loading