<commit_before>// Dependencies
var express         = require( 'express' ),
    logger          = require( 'morgan' ),
    bodyParser      = require( 'body-parser' ),
    cookieParser    = require( 'cookie-parser' ),
    cookieSession   = require( 'cookie-session' ),
    fs              = require( 'fs' ),
    crypto          = require( 'crypto' ),
    slug            = require( 'slug' ),
    server          = express(),
    port            = process.env.PORT || 3030;

// Secret tokens
var cookieParserSecret = crypto.randomBytes( 64 ).toString( 'base64' ),
    cookieSessionSecret = crypto.randomBytes( 64 ).toString( 'base64' );

var studentsFile = __dirname + '/students.json';

// Middleware configuration
server.set( 'view engine', 'jade' );
// server.use( logger( 'combined' ) );
server.use( '/assets', express.static( __dirname + '/assets' ) );
server.use( bodyParser.urlencoded({ extended: false }) );
server.use( cookieParser( cookieParserSecret ) );
server.use( cookieSession({ secret: cookieSessionSecret }) );

fs.exists( studentsFile, function( e ) {
    if ( ! e ) {
        var db = { students: [] };
        fs.writeFile( studentsFile, JSON.stringify( db ), function( err ) {
            if ( err ) {
                console.log( '[ERROR]: Unable to create new students.json', err );
            } else {
                console.log( '[SERVER]: Created new students.json' );
            }
        });
    }
});

function trimSSN( ssn ) {
    ssn = ssn.trim();

    if ( ssn.indexOf( '-' ) < 0 ) {
        ssn = ssn.slice( 0, 6 ) + '-' + ssn.slice( 6, 10 );
    }

    return ssn;
}

// Routes
server.get( '/', function( req, res ) {
    res.render( 'index', {
        error: req.query.error,
        success: req.query.success
    });
});

server.post( '/', function( req, res ) {
    var db = {},
        exists = false;

    if ( req.body.program == '0' || req.body.firstname == '' ||
         req.body.lastname == '' || req.body.ssn == '' ||
         req.body.phone == '' || req.body.email == '' ) {
        res.redirect( '/?error=1' );
        return;
    }

    req.body.ssn = trimSSN( req.body.ssn )
    
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        if ( err ) {
            console.log( '[ERROR]: Unable to open students.json.', err );
            res.redirect( '/?error=1' );
            return;
        }

        db = JSON.parse( data );
        db.students = db.students || [];

        var i;

        db.students.forEach( function( student, index ) {
            if ( student.ssn == req.body.ssn ) {
                exists = true;
                i = index;
            }
        });

        if ( exists ) {
            console.log( '[SERVER]: Student already exists - updating.' );
            db.students[i] = req.body;
        } else {
            console.log( '[SERVER]: Creating new student.' );
            req.body.image = slug( req.body.lastname + ' ' + req.body.firstname, '_' ).toLowerCase() + '_thumb.jpg';
            db.students.push( req.body );
        }

        fs.writeFile( studentsFile, JSON.stringify( db ), function( err ) {
            if ( err ) {
                console.log( '[ERROR]: Unable to write to students file.', err );
                res.redirect( '/?error=1' );
                return;
            }

            res.redirect( '/?success=' + ( exists ? '2' : '1' ) );
        });
    });
});

server.get( '/student', function( req, res ) {
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        var db = JSON.parse( data );

        db.students.forEach( function( student ) {
            if ( student.ssn == req.query.ssn ) {
                res.status( 200 ).end();
            }
        });

        res.status( 418 ).end();
    });
});

server.get( '/clear', function( req, res ) {
    var db = { students: [] },
        date = new Date();

    fs.rename( studentsFile, 'students_backup_' + date.toString() + '.json', function( err ) {
        if ( err ) {
            console.log( '[ERROR]: Unable to rename students.json', err );
            res.redirect( '/list' );
        } else {
            console.log( '[SYSTEM]: Backup created!' );

            fs.writeFile( studentsFile, JSON.stringify( db ), function( err ) {
                if ( err ) {
                    console.log( '[ERROR]: Unable to create new students.json', err );
                } else {
                    console.log( '[SERVER]: Created new students.json' );
                }

                res.redirect( '/list' );
            });
        }
    });
});

server.get( '/list', function( req, res ) {
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        if ( err ) {
            console.log( '[ERROR]:', err );
            return;
        }

        var db = JSON.parse( data );

        db.students.sort( function( a, b ) {
            if ( a.lastname < b.lastname ) {
                return -1;
            }
            if ( a.lastname > b.lastname ) {
                return 1;
            }
            return 0;
        });

        res.render( 'list', { students: db.students } );
    });
});

server.get( '/edit', function( req, res ) {
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        if ( err ) {
            console.log( '[ERROR]:', err );
            return;
        }

        var db = JSON.parse( data ),
            student = {};

        db.students.forEach( function( s ) {
            if ( req.query.ssn = s.ssn ) {
                student = s;
            }
        });<commit_msg>fixed issue with editing<commit_after>// Dependencies
var express         = require( 'express' ),
    logger          = require( 'morgan' ),
    bodyParser      = require( 'body-parser' ),
    cookieParser    = require( 'cookie-parser' ),
    cookieSession   = require( 'cookie-session' ),
    fs              = require( 'fs' ),
    crypto          = require( 'crypto' ),
    slug            = require( 'slug' ),
    server          = express(),
    port            = process.env.PORT || 3030;

// Secret tokens
var cookieParserSecret = crypto.randomBytes( 64 ).toString( 'base64' ),
    cookieSessionSecret = crypto.randomBytes( 64 ).toString( 'base64' );

var studentsFile = __dirname + '/students.json';

// Middleware configuration
server.set( 'view engine', 'jade' );
// server.use( logger( 'combined' ) );
server.use( '/assets', express.static( __dirname + '/assets' ) );
server.use( bodyParser.urlencoded({ extended: false }) );
server.use( cookieParser( cookieParserSecret ) );
server.use( cookieSession({ secret: cookieSessionSecret }) );

fs.exists( studentsFile, function( e ) {
    if ( ! e ) {
        var db = { students: [] };
        fs.writeFile( studentsFile, JSON.stringify( db ), function( err ) {
            if ( err ) {
                console.log( '[ERROR]: Unable to create new students.json', err );
            } else {
                console.log( '[SERVER]: Created new students.json' );
            }
        });
    }
});

function trimSSN( ssn ) {
    ssn = ssn.trim();

    if ( ssn.indexOf( '-' ) < 0 ) {
        ssn = ssn.slice( 0, 6 ) + '-' + ssn.slice( 6, 10 );
    }

    return ssn;
}

// Routes
server.get( '/', function( req, res ) {
    res.render( 'index', {
        error: req.query.error,
        success: req.query.success
    });
});

server.post( '/', function( req, res ) {
    var db = {},
        exists = false;

    if ( req.body.program == '0' || req.body.firstname == '' ||
         req.body.lastname == '' || req.body.ssn == '' ||
         req.body.phone == '' || req.body.email == '' ) {
        res.redirect( '/?error=1' );
        return;
    }

    req.body.ssn = trimSSN( req.body.ssn )
    
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        if ( err ) {
            console.log( '[ERROR]: Unable to open students.json.', err );
            res.redirect( '/?error=1' );
            return;
        }

        db = JSON.parse( data );
        db.students = db.students || [];

        var i;

        db.students.forEach( function( student, index ) {
            if ( student.ssn == req.body.ssn ) {
                exists = true;
                i = index;
            }
        });

        if ( exists ) {
            console.log( '[SERVER]: Student already exists - updating.' );
            db.students[i] = req.body;
        } else {
            console.log( '[SERVER]: Creating new student.' );
            req.body.image = slug( req.body.lastname + ' ' + req.body.firstname, '_' ).toLowerCase() + '_thumb.jpg';
            db.students.push( req.body );
        }

        fs.writeFile( studentsFile, JSON.stringify( db ), function( err ) {
            if ( err ) {
                console.log( '[ERROR]: Unable to write to students file.', err );
                res.redirect( '/?error=1' );
                return;
            }

            res.redirect( '/?success=' + ( exists ? '2' : '1' ) );
        });
    });
});

server.get( '/student', function( req, res ) {
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        var db = JSON.parse( data );

        db.students.forEach( function( student ) {
            if ( student.ssn == req.query.ssn ) {
                res.status( 200 ).end();
            }
        });

        res.status( 418 ).end();
    });
});

server.get( '/clear', function( req, res ) {
    var db = { students: [] },
        date = new Date();

    fs.rename( studentsFile, 'students_backup_' + date.toString() + '.json', function( err ) {
        if ( err ) {
            console.log( '[ERROR]: Unable to rename students.json', err );
            res.redirect( '/list' );
        } else {
            console.log( '[SYSTEM]: Backup created!' );

            fs.writeFile( studentsFile, JSON.stringify( db ), function( err ) {
                if ( err ) {
                    console.log( '[ERROR]: Unable to create new students.json', err );
                } else {
                    console.log( '[SERVER]: Created new students.json' );
                }

                res.redirect( '/list' );
            });
        }
    });
});

server.get( '/list', function( req, res ) {
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        if ( err ) {
            console.log( '[ERROR]:', err );
            return;
        }

        var db = JSON.parse( data );

        db.students.sort( function( a, b ) {
            if ( a.lastname < b.lastname ) {
                return -1;
            }
            if ( a.lastname > b.lastname ) {
                return 1;
            }
            return 0;
        });

        res.render( 'list', { students: db.students } );
    });
});

server.get( '/edit', function( req, res ) {
    fs.readFile( studentsFile, 'utf8', function( err, data ) {
        if ( err ) {
            console.log( '[ERROR]:', err );
            return;
        }

        var db = JSON.parse( data ),
            student = {};

        db.students.forEach( function( s ) {
            if ( req.query.ssn == s.ssn ) {
                student = s;
            }
        });
{'fraction_non_alphanumeric': 0.16269626421223607, 'fraction_numerical': 0.006677495036996932, 'mean_word_length': 1.6102213848327838, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5024690', 'n_tokens_mistral': 3382, 'n_tokens_neox': 3251, 'n_words': 795}