<commit_before>'use strict';

const _ = require('lodash');

const User = require('../models/user.js');

module.exports.getUsers = function(request, response, next) {
    User.find({})
    .then(function(users){
        return response.status(200).json(users)
    })
    .catch(function(error){ return next(error); });
};

module.exports.createUser = function(request, response, next) {
    const email = request.body.email;
    const password = request.body.password;

    if (!email || !password) { return response.status(422).end('You must provide both email and password'); }

    User.findOne({ email: email })
    .then(function(existingUser) {
        if (existingUser) { return response.status(422).end('Email is already in use by another user'); }

        const user = new User({ email: email, password: password });

        user.save()
        .then(function() { return response.status(200).end('Ok'); })
        .catch(function(error) { return next(error); });
    })
    .catch(function(error) { return next(error); });
};

module.exports.getUser = function(request, response, next) {
    const id = request.params.id;

    if (!id) { return response.status(422).end('You must provide user id'); }

    User.findById(id)
    .then(function(user) {
        if (!user) { return response.status(404).end('User not found'); }

        return response.status(200).json(user)
    })
    .catch(function(error) { return next(error); });
};

module.exports.updateUser = function(request, response, next) {
    const id = request.params.id;
    const userData = _.omit(_.extend({}, request.body), ['_id', 'password']);

    if (!id) { return response.status(422).end('You must provide user id'); }

    User.findByIdAndUpdate(id, userData, { new: true })
    .then(function(user) {
        if (!user) { return response.status(404).end('User not found'); }

        return response.status(200).json(user)
    })
    .catch(function(error) { return next(error); });
};

module.exports.deleteUser = function(request, response, next) {
    const id = request.params.id;<commit_msg>Added comments to users endpoint<commit_after>'use strict';

const _ = require('lodash');

const User = require('../models/user.js');

// Get all users
module.exports.getUsers = function(request, response, next) {
    User.find({})
    .then(function(users){
        return response.status(200).json(users)
    })
    .catch(function(error){ return next(error); });
};

// Create user
module.exports.createUser = function(request, response, next) {
    const email = request.body.email;
    const password = request.body.password;

    if (!email || !password) { return response.status(422).end('You must provide both email and password'); }

    User.findOne({ email: email })
    .then(function(existingUser) {
        if (existingUser) { return response.status(422).end('Email is already in use by another user'); }

        const user = new User({ email: email, password: password });

        user.save()
        .then(function() { return response.status(200).end('Ok'); })
        .catch(function(error) { return next(error); });
    })
    .catch(function(error) { return next(error); });
};

// Get user
module.exports.getUser = function(request, response, next) {
    const id = request.params.id;

    if (!id) { return response.status(422).end('You must provide user id'); }

    User.findById(id)
    .then(function(user) {
        if (!user) { return response.status(404).end('User not found'); }

        return response.status(200).json(user)
    })
    .catch(function(error) { return next(error); });
};

// Update user
module.exports.updateUser = function(request, response, next) {
    const id = request.params.id;
    const userData = _.omit(_.extend({}, request.body), ['_id', 'password']);

    if (!id) { return response.status(422).end('You must provide user id'); }

    User.findByIdAndUpdate(id, userData, { new: true })
    .then(function(user) {
        if (!user) { return response.status(404).end('User not found'); }

        return response.status(200).json(user)
    })
    .catch(function(error) { return next(error); });
};


// Delete user
module.exports.deleteUser = function(request, response, next) {
    const id = request.params.id;
{'fraction_non_alphanumeric': 0.16588345864661655, 'fraction_numerical': 0.014097744360902255, 'mean_word_length': 3.452928870292887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1644584', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1250, 'n_words': 302}