var uuid = require('uuid');
var Todo = require('./todo.model');

exports.findAllByUserId = findAllByUserId;
exports.create = create;
exports.update = update;
exports.remove = remove;
exports.sync = sync;

function findAllByUserId(userId) {
    return Todo.findAll({
        where: {
            userId: userId
        }
    });
}

function create(id, description, done, userId) {
    console.log(id);
    return Todo.create({
        id: id || uuid.v4(),
        description: description,
        done: done,
        userId: userId
    });
}

function update(id, description, done, userId) {
    return Todo.update({
        description: description,
        done: done
    }, {
        where: {
            id : id,
            userId : userId
        }
    });
}

function remove(id, userId) {
    return Todo.destroy({
        where: {
            id: id,
            userId: userId
        }
    });
}

function sync() {
    return Todo.sync({force: true});
}

@@ -304,32 +304,65 @@
 userId%0A        %7D
+,%0A        order: 'createdAt DESC'
 %0A    %7D);%0A%7D%0A%0Afunc

add ordering of todos