var DemoDbService = function () {

};

DemoDbService.prototype.init = function (callback) {
    var db = new DemoDataContext();
    db.init(callback);
};

DemoDbService.prototype.getAllUsers = function(callback) {
    new DemoDataContext().users.toArray(function(users) {
        alert(users.length);
        callback(users);
    });
};

DemoDbService.prototype.getUserByName = function(name, callback) {
    new DemoDataContext().users.where("name='" + name + "'").toArray(function(users) {
        callback(users.firstOrDefault());
    });
};

DemoDbService.prototype.addRole = function(roleName, callback) {
    var role = new Role();
    role.name = roleName;
    var db = new DemoDataContext();
    db.roles.add(role);
    db.saveChanges(callback);
};

DemoDbService.prototype.updateUserPassword = function(username, password, callback) {
    this.getUserByName(username, function(user) {
        if (user == null) {
            throw "no user found.";
        }
        user.password = password;
        var db = new DemoDataContext();
        db.users.update(user);
        db.saveChanges(callback);
    });
};

DemoDbService.prototype.deleteUserByName = function(name, callback) {
    this.getUserByName(name, function(user) {
        if (user == null) {
            throw "no user found.";
        }
        var db = new DemoDataContext();
        db.users.remove(user);
        db.saveChanges(callback);
    });
};

Last edited Mar 19, 2013 at 2:33 PM by leo_2010, version 1

Comments

No comments yet.