72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
const ContactService = require('../services/contact.service');
|
|
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
|
const { insertContactSchema, updateContactSchema } = require('../validate/contact.schema');
|
|
|
|
class ContactController {
|
|
// Get all contact
|
|
static async getAll(req, res) {
|
|
const queryParams = req.query;
|
|
|
|
const results = await ContactService.getAllContact(queryParams);
|
|
const response = await setResponsePaging(queryParams, results, 'Contact found')
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Get contact by ID
|
|
static async getById(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const results = await ContactService.getContactById(id);
|
|
const response = await setResponse(results, 'Contact found')
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Create contact
|
|
static async create(req, res) {
|
|
const { error, value } = await checkValidate(insertContactSchema, req)
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.userId = req.user.user_id
|
|
|
|
const results = await ContactService.createContact(value);
|
|
const response = await setResponse(results, 'Contact created successfully')
|
|
|
|
return res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Update contact
|
|
static async update(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const { error, value } = checkValidate(updateContactSchema, req)
|
|
|
|
if (error) {
|
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
|
}
|
|
|
|
value.userId = req.user.user_id
|
|
|
|
const results = await ContactService.updateContact(id, value);
|
|
const response = await setResponse(results, 'Contact updated successfully')
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
|
|
// Soft delete contact
|
|
static async delete(req, res) {
|
|
const { id } = req.params;
|
|
|
|
const results = await ContactService.deleteContact(id, req.user.user_id);
|
|
const response = await setResponse(results, 'Contact deleted successfully')
|
|
|
|
res.status(response.statusCode).json(response);
|
|
}
|
|
}
|
|
|
|
module.exports = ContactController;
|