{"size": 884, "ext": "js", "max_stars_repo_path": "src/controller/delete.js", "max_stars_repo_name": "DaemonAlchemist/atp-rest", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/controller/delete.js", "max_issues_repo_name": "DaemonAlchemist/atp-rest", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/controller/delete.js", "max_forks_repo_name": "DaemonAlchemist/atp-rest", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.4827586207, "max_line_length": 116, "alphanum_fraction": 0.536199095}
/**
 * Created by Andrea on 10/17/2017.
 */

import restController from './rest';
import validator from 'atp-validator';
import {databaseError} from "../util";
import {identity} from 'atp-pointfree';

export default ({model, permission, idField = "id", validate = identity, preDelete = identity}) => restController({
    getValidator: req => validate(
        validator()
            .loggedIn(req)
            .hasPermission(permission, req)
            .required(req.params[idField], idField),
        req
    ),
    loadResource: req => new Promise((resolve, reject) => {
        Promise.resolve(preDelete(req)).then(() => {
            new model()
                .where({id: req.params[idField]})
                .limit(1)
                .delete()
                .then(resolve)
                .catch(databaseError(reject));
        });
    }),
});
