123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- const Contact = require("../model/contact");
- const listContacts = async (req, res, next) => {
- try {
- const userId = req.user.id;
- const contacts = await Contact.getList(userId, req.query);
- return res.json({
- status: "success",
- code: 200,
- data: {
- ...contacts,
- },
- });
- } catch (e) {
- next(e);
- }
- };
- const getContactById = async (req, res, next) => {
- try {
- const id = req.params.id;
- const userId = req.user.id;
- const contact = await Contact.getById(id, userId);
- if (contact)
- return res.json({
- status: "success",
- code: 200,
- data: {
- contact,
- },
- });
- return res.status(404).json({
- status: "error",
- code: 404,
- data: "Not Found",
- });
- } catch (e) {
- next(e);
- }
- };
- const addContact = async (req, res, next) => {
- try {
- const contact = req.body;
- const userId = req.user.id;
- if (contact) {
- await Contact.add({ ...contact, owner: userId });
- return res.status(201).json({
- status: "success",
- code: 201,
- data: {
- contact,
- },
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const removeContact = async (req, res, next) => {
- try {
- const id = req.params.id;
- const userId = req.user.id;
- const contact = await Contact.remove(id, userId);
- if (contact) {
- return res.json({
- status: "success",
- code: 200,
- data: {
- contact,
- },
- });
- } else {
- return res.status(404).json({
- status: "error",
- code: 404,
- data: "Not Found",
- });
- }
- } catch (e) {
- next(e);
- }
- };
- const updateContact = async (req, res, next) => {
- try {
- const id = req.params.id;
- const userId = req.user.id;
- const contact = await Contact.update(id, userId, req.body);
- if (contact) {
- return res.status(200).json({
- status: "success",
- code: 200,
- data: {
- contact,
- },
- });
- } else {
- return res.status(404).json({
- status: "error",
- code: 404,
- data: "Not Found",
- });
- }
- } catch (e) {
- next(e);
- }
- };
- module.exports = {
- listContacts,
- getContactById,
- addContact,
- removeContact,
- updateContact,
- };
|