<commit_before>var db = require('../lib/db.js');
var resultModel = require('../lib/models/result.js');

// return user activities
exports.activity = function(req, res){
    var user_id = req.params.id;
    db.execute('SELECT * FROM activity WHERE user_id = ?', [user_id], function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, 'Error while getting word lookup data!'));

        else{
            res.send(new resultModel.result(true, result));
        }
    });
};

// return word lookups
exports.lookup = function(req, res){
    var user_id = req.params.id;
    db.execute('SELECT word_lookup_id, word_lookup_word, word_lookup_quiz_taken, word_lookup_quiz_result, ' +
        'word_lookup_created_time, book_name, user_id, COUNT(word_lookup_id) AS word_lookup_count ' +
        'FROM word_lookup WHERE user_id = ? GROUP BY word_lookup_word', [user_id], function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, 'Error while getting word lookup data!'));

        else{
            res.send(new resultModel.result(true, result));
        }
    });
};

// return books that user read
exports.books = function(req, res){
    var user_id = req.params.id;
    db.execute('SELECT DISTINCT(book_name) FROM activity WHERE activity_type_id = 1 AND user_id = ?', [user_id], function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, 'Error while getting books!'));

        else{
            res.send(new resultModel.result(true, result));
        }
    });
};<commit_msg>added login & registration routes - server<commit_after>var db = require('../lib/db.js');
var resultModel = require('../lib/models/result.js');

// register a user
exports.register = function(req, res){

    // user to be created
    var user = {
        user_first_name: req.body.user_first_name,
        user_last_name: req.body.user_last_name,
        user_email: req.body.user_email,
        user_password: req.body.user_password,
        user_gender: req.body.user_gender,
        user_created_date: new Date()
    }


    db.execute('INSERT INTO user SET ?', user, function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, 'Error while creating the user!'));

        else{
            user.user_id = result.insertId;
            res.send(new resultModel.result(true, user));
        }
    });

};

// login user
exports.login = function(req, res){

    // user to be authenticated
    var user = {
        user_email: req.body.user_email,
        user_password: <PASSWORD>
    }

    db.execute('SELECT * FROM user WHERE user_email = ? AND user_password = ?', [user.user_email, user.user_password], function(err, result){

        console.log(result);

        if(err)
            res.send(500, new resultModel.result(false, {}, ['Error while logging in!]']));

        else{
            if(result.length != 1){
                res.send(401, new resultModel.result(false, {}, ['Wrong credentials, please try again!']));
            }

            else
                res.send(new resultModel.result(true, result[0]));
        }
    });
};

// return user activities
exports.activity = function(req, res){
    var user_id = req.params.id;
    db.execute('SELECT * FROM activity WHERE user_id = ?', [user_id], function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, ['Error while getting word lookup data!']));

        else{
            res.send(new resultModel.result(true, result));
        }
    });
};

// return word lookups
exports.lookup = function(req, res){
    var user_id = req.params.id;
    db.execute('SELECT word_lookup_id, word_lookup_word, word_lookup_quiz_taken, word_lookup_quiz_result, ' +
        'word_lookup_created_time, book_name, user_id, COUNT(word_lookup_id) AS word_lookup_count ' +
        'FROM word_lookup WHERE user_id = ? GROUP BY word_lookup_word', [user_id], function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, ['Error while getting word lookup data!']));

        else{
            res.send(new resultModel.result(true, result));
        }
    });
};

// return books that user read
exports.books = function(req, res){
    var user_id = req.params.id;
    db.execute('SELECT DISTINCT(book_name) FROM activity WHERE activity_type_id = 1 AND user_id = ?', [user_id], function(err, result){
        if(err)
            res.send(500, new resultModel.result(false, {}, ['Error while getting books!']));

        else{
            res.send(new resultModel.result(true, result));
        }
    });
};
{'fraction_non_alphanumeric': 0.13635387588576336, 'fraction_numerical': 0.006656645909383724, 'mean_word_length': 2.7264, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '6861533', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1401, 'n_words': 333}