{"size": 2773, "ext": "js", "max_stars_repo_path": "src/controllers/task.controller.js", "max_stars_repo_name": "superyaz/REST_API", "max_stars_repo_stars_event_min_datetime": "2022-02-13T03:15:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-17T00:57:52.000Z", "max_issues_repo_path": "src/controllers/task.controller.js", "max_issues_repo_name": "superyaz/REST_API", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/controllers/task.controller.js", "max_forks_repo_name": "superyaz/REST_API", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.4554455446, "max_line_length": 81, "alphanum_fraction": 0.5279480707}
import Task from '../models/Task'
import { getPagination } from '../libs/getPagination';

export const findAllTask = async (req, res) => {
    try {
        const { size, page, title } = req.query;
        const condition = title ? {
            title: { $regex: new RegExp(title), $options: "i" }
        } : {};
        const { limit, offset } = getPagination(page, size)
        const data = await Task.paginate(condition, { offset, limit });
        res.json({
            totalItems: data.totalDocs,
            tasks: data.docs,
            totalPages: data.totalPages,
            currentPage: data.page - 1
        });
    } catch (error) {
        res.status(500).json({
            message: error.message || 'Something goes wrong retrieving the task',
        });
    }
}

export const createTask = async (req, res) => {
    if (!req.body.title) {
        return res.status(400).send({ message: 'Content cannot be empty' });
    }
    try {
        const newTask = new Task({
            title: req.body.title,
            description: req.body.description,
            done: req.body.done ? req.body.done : false
        })
        const taskSave = await newTask.save();
        res.json(taskSave);
    } catch (error) {
        res.status(500).json({
            message: error.message || 'Something goes wrong creating a task',
        });
    }
}

export const findOnetask = async (req, res) => {
    const { id } = req.params
    try {
        const task = await Task.findById(id);
        if (!task)
            return res
                .status(404)
                .json({ message: `Task with id ${id} does not exists` });
        res.json(task);
    } catch (error) {
        res.status(500).json({
            message: error.message || 'Something goes wrong creating a task',
        });
    }
}

export const deleteTask = async (req, res) => {
    const { id } = req.params
    try {
        await Task.findByIdAndDelete(req.params.id);
        res.json({
            message: 'Task were deleted successfully',
        });
    } catch (error) {
        res.status(500).json({
            message: `Cannot delete task with id: ${id}`,
        })
    }
};


export const findAllDoneTask = async (req, res) => {
    try {
        const tasks = await Task.find({ done: true });
        res.json(tasks)
    } catch (error) {
        res.status(500).json({
            message: `Cannont exist tasks`,
        })
    }

}

export const updateTask = async (req, res) => {
    try {
        await Task.findByIdAndUpdate(req.params.id, req.body);
        res.json({
            message: 'Task was update Successfully'
        })
    } catch (error) {
        res.status(500).json({
            message: `Error update task`,
        })
    }

}

//Minuto 1:55:46