const express = require('express');
const router = express.Router();
const authorize = require('../../../../middlewares/authorize-sapps');
const userProjectService = require('./userProject.service');

router.get('/', authorize(), getAll);
router.get('/filter', authorize(), filter);
router.get('/get-user-ids', authorize(), getUserIds);
router.post('/create', authorize(), create);
router.delete('/:id', authorize(), _delete);

module.exports = router;

function getAll(req, res, next) {
    userProjectService.getAllUserProjects()
        .then(result => res.json(result))
        .catch(next);
}

function filter(req, res, next) {
    userProjectService.filter(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function getUserIds(req, res, next) {
    userProjectService.getUserIdsByProjectIds(req.query)
        .then(result => res.json(result))
        .catch(next);
}

function create(req, res, next) {
    userProjectService.assignProjectsToUser(req.body)
        .then(result => res.json(result))
        .catch(next);
}

function _delete(req, res, next) {
    userProjectService.deleteUserProjects(req.params.id)
        .then(result => res.json(result))
        .catch(next);
}
