<repo_name>lopezbraian/api-task<file_sep>/app.js
const express = require('express')
const app = express()
const cors = require('cors')
const bodyParser = require('body-parser')
const route = require('./routes')
const { errorHandler, wrapwithBoom, routeNoFound } = require('./middlewares/errorHandlers')
//Middlwares
app.use(cors())
app.use(bodyParser.json())
app.use(express.urlencoded({ extended: false }))

//Route
app.use('/api', route)
// Handle Route No found
app.use(routeNoFound)

app.use(wrapwithBoom)
app.use(errorHandler)

module.exports = app;<file_sep>/docker-compose.yml
version:  "3.9"
services:
  api: 
    container_name: api-task 
    build: .
    volumes: 
      - .:/usr/src/app
    ports: 
      - "3000:3000"
    links:
      - db
  db:
    container_name: db_task
    image: mongo
    volumes: 
      - ./db:/data/db
    ports: 
      - "27017:27017"<file_sep>/Dockerfile
FROM node:lts-alpine3.10

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY ./package.json ./package.json

RUN npm install
RUN npm install -g nodemon

COPY . /usr/src/app

EXPOSE 3000

CMD ["npm" ,"run", "dev"]
<file_sep>/components/tasks/controller/index.js
const TaskService = require("../../../services/task");
class TaskController {
  constructor() {
    this.taskService = new TaskService();
  }
  async getAll(req, res, next) {
    try {
      const Task = await this.taskService.getAll();
      return res.status(200).json({
        data: Task,
        message: "Todas las tareas",
      });
    } catch (err) {
      next(err);
    }
  }
  async create(req, res, next) {
    const { content, title } = req.body;
    const newProduct = {
      content,
      title,
    };
    try {
      const task = await this.taskService.create(newProduct);
      return res.status(201).json({
        data: task,
        message: "Task creada",
      });
    } catch (err) {
      next(err);
    }
  }
  async getBy(req, res, next) {
    const { id } = req.params;
    try {
      const task = await this.taskService.getBy(id);
      return res.status(200).json({
        data: task,
      });
    } catch (err) {
      next(err);
    }
  }
  async delete(req, res, next) {
    const { id } = req.params;
    try {
      const resp = await this.taskService.delete(id);
      if (resp.deletedCount !== 0) {
        return res.status(200).json({
          message: "Borrado correctamente",
        });
      }
    } catch (err) {
      next(err);
    }
  }
  async edit(req, res, next) {
    const { newTask, id } = req.body;

    try {
      const resp = await this.taskService.edit(id, newTask);
      return res.status(200).json({
        data: resp,
        message: "Task actualizada",
      });
    } catch (err) {
      next(err);
    }
  }
}
module.exports = TaskController;
<file_sep>/README.md
# api-task
## Routes

<strong>GET</strong> api/task

<strong>GET</strong> api/task/:id

<strong>POST</strong> api/task

<strong>DELETE</strong> api/task/:id

<strong>PUT</strong> api/task/ 
<file_sep>/config/index.js
require('dotenv').config()

const uriDb = process.env.URI_DB
const mode = process.env.NODE_ENV

module.exports = {
  uriDb,
  mode
}<file_sep>/utils/schema/task.js
const joi = require("joi");

const createTask = joi.object({
  content: joi.string().max(40).required(),
  title: joi.string().max(20).required(),
  done: joi.boolean(),
});

const idTask = joi.object({
  id: joi.string().regex(/^[0-9a-fA-F]{24}$/),
});

const updateTask = joi.object({
  newTask: {
    content: joi.string().max(40),
    title: joi.string().max(20),
    done: joi.boolean(),
  },
  id: joi.string().regex(/^[0-9a-fA-F]{24}$/),
});

module.exports = {
  createTask,
  idTask,
  updateTask,
};
<file_sep>/middlewares/errorHandlers.js
const boom = require("@hapi/boom");

function wrapwithBoom(err, req, res, next) {
  if (!err.isBoom) {
    next(boom.badImplementation(err));
  }
  next(err);
}
function errorHandler(err, req, res, next) {
  const {
    output: { statusCode, payload },
  } = err;
  res.status(statusCode).json(payload);
}

function routeNoFound(req, res) {
  const { output } = boom.notFound();
  res.status(output.statusCode).json(output.payload);
}

module.exports = { errorHandler, wrapwithBoom, routeNoFound };
