Skip to content

Commit

Permalink
error fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinabel committed Jun 25, 2018
1 parent 25f2cc4 commit b96b98c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 65 deletions.
4 changes: 2 additions & 2 deletions zelp-angular/src/app/admin-view/admin-view.component.html
Expand Up @@ -109,7 +109,7 @@ <h4>Edit User Information</h4>
<i class="fa fa-plus fa-md"></i>
</button>
<button *ngIf="userUpdate" (click)="updateUser(createForm.username.value, createForm.password.value, createForm.firstName.value,
createForm.lastName.value, createForm.email.value)" class="btn">
createForm.lastName.value, createForm.email.value, this.userRole)" class="btn">
<i class="fa fa-check fa-md"></i>
</button>
</td>
Expand Down Expand Up @@ -190,4 +190,4 @@ <h4>Response Information</h4>
</div>
</div>
</div>
</div>
</div>
13 changes: 10 additions & 3 deletions zelp-angular/src/app/admin-view/admin-view.component.ts
Expand Up @@ -180,6 +180,7 @@ export class AdminViewComponent implements OnInit {
if (response.status != 400) {
this.users = this.users.filter(user => user._id != userId);
this.displayUser = this.displayUser.filter(user => user._id != userId);
this.userView = false;
}
});
}
Expand All @@ -189,6 +190,7 @@ export class AdminViewComponent implements OnInit {
{if (posts.status != 400) {
this.posts = this.posts.filter(post => post._id != postId);
this.displayPost = this.displayPost.filter(post => post._id != postId);
this.postView = false;
}}
)
}
Expand All @@ -215,14 +217,19 @@ export class AdminViewComponent implements OnInit {
this.userRole = this.user.role;
}

updateUser(username, password, firstName, lastName, email) {
updateUser(username, password, firstName, lastName, email, role) {
this.submitted = true;

// stop if form is invalid
if (this.registerForm.invalid) {
return;
}

if (this.userRole !== "1" && this.userRole !== "2") {
this.alertService.error("Please set roles for the user", false);
return;
}

if (this.user.username === "admin") {
this.alertService.error("The username 'admin' cannot be used.", false);
return;
Expand All @@ -238,7 +245,7 @@ export class AdminViewComponent implements OnInit {
firstName: firstName,
lastName: lastName,
email: email,
role: this.user.role
role: this.userRole
};

if (index > -1) {
Expand All @@ -251,7 +258,7 @@ export class AdminViewComponent implements OnInit {
this.user = updatedUser;

this.userService
.updateUser(this.user._id, username, password, firstName, lastName, email)
.updateUser(this.user._id, username, password, firstName, lastName, email, role)
.then(response => {
this.alertService.success("User updated!", false);
this.userUpdate = false;
Expand Down
3 changes: 2 additions & 1 deletion zelp-angular/src/app/profile/profile.component.ts
Expand Up @@ -51,7 +51,8 @@ export class ProfileComponent implements OnInit {
this.user.password,
this.user.firstName,
this.user.lastName,
this.user.email
this.user.email,
this.userRole
)
.then(response => this.updateSuccess(response),
err => this.alertService.error(err, false));
Expand Down
5 changes: 3 additions & 2 deletions zelp-angular/src/app/services/user.service.client.ts
Expand Up @@ -92,13 +92,14 @@ export class UserServiceClient {

// PUT

updateUser(id, username, password, first, last, email) {
updateUser(id, username, password, first, last, email, role) {
const user = {
username: username,
password: password,
firstName: first,
lastName: last,
email: email
email: email,
role: role
};
return fetch(API_URL + "user/" + id, {
body: JSON.stringify(user),
Expand Down
115 changes: 58 additions & 57 deletions zelp-nodejs/services/user.service.server.js
@@ -1,62 +1,61 @@
module.exports = function(app) {
app.get("/api/user", findAllUsers);
app.get("/api/user/:userId", findUserById);
app.post("/api/user", createUser);
app.get("/api/profile", profile);
app.post("/api/logout", logout);
app.post("/api/login", login);
app.put("/api/user/:userId", updateUser);
// FIXME need to change later
app.get("/api/admin/create", createAdmin);
app.delete("/api/user/:userId", deleteUser);
app.get("/api/user", findAllUsers);
app.get("/api/user/:userId", findUserById);
app.post("/api/user", createUser);
app.get("/api/profile", profile);
app.post("/api/logout", logout);
app.post("/api/login", login);
app.put("/api/user/:userId", updateUser);
// FIXME need to change later
app.get("/api/admin/create", createAdmin);
app.delete("/api/user/:userId", deleteUser);

var userModel = require("../models/user/user.model.server");
var userModel = require("../models/user/user.model.server");

function findAllUsers(req, res) {
if (req.session["currentUser"].role !== "0") {
res.send(404);
function findAllUsers(req, res) {
if (req.session["currentUser"].role !== "0") {
res.send(404);
}
userModel.findAllUsers().then(users => res.send(users));
}
userModel.findAllUsers().then(users => res.send(users));
}

function findUserById(req, res) {
var id = req.params["userId"];
userModel.findUserById(id).then(function(user) {
res.json(user);
});
}
function findUserById(req, res) {
var id = req.params["userId"];
userModel.findUserById(id).then(function(user) {
res.json(user);
});
}

function profile(req, res) {
res.send(req.session["currentUser"]);
}
function profile(req, res) {
res.send(req.session["currentUser"]);
}

function logout(req, res) {
req.session.destroy();
res.sendStatus(200);
}
function logout(req, res) {
req.session.destroy();
res.sendStatus(200);
}

function login(req, res) {
var credentials = req.body;
userModel.findUserByCredentials(credentials).then(function(user) {
req.session["currentUser"] = user;
res.json(user);
});
}
function login(req, res) {
var credentials = req.body;
userModel.findUserByCredentials(credentials).then(function(user) {
req.session["currentUser"] = user;
res.json(user);
});
}

function createUser(req, res) {
var user = req.body;
userModel.findUserByUsername(user.username).then(checkUser => {
if(checkUser.length !== 0) {
res.sendStatus(409)
} else {
userModel.createUser(user)
.then(function (user) {
req.session['currentUser'] = user;
res.send(user);
})
}
});
}
function createUser(req, res) {
var user = req.body;
userModel.findUserByUsername(user.username).then(checkUser => {
if(checkUser.length !== 0) {
res.sendStatus(409)
} else {
userModel.createUser(user)
.then(function (user) {
res.send(user);
})
}
});
}

function createAdmin(req, res){
userModel.findAdmin().then(result => {if(result.length < 1){
Expand All @@ -72,12 +71,12 @@ module.exports = function(app) {
res.sendStatus(409)
}})
}


function deleteUser(req, res) {
var userId = req.params["userId"];
userModel.deleteUser(userId).then(response => res.send(response));
}

function deleteUser(req, res) {
var userId = req.params["userId"];
userModel.deleteUser(userId).then(response => res.send(response));
}

function updateUser(req, res) {
var userId = req.params["userId"];
Expand All @@ -89,7 +88,7 @@ module.exports = function(app) {
_id: userId
};

if(beforeUpdate.username !== newUser.username) {
if(beforeUpdate.username !== "admin" && beforeUpdate.username !== newUser.username) {
userModel.findUserByUsername(user.username).then(checkUser => {
if(checkUser.length !== 0) {
res.sendStatus(409)
Expand All @@ -101,7 +100,9 @@ module.exports = function(app) {
}
})
} else {
req.session["currentUser"] = newUser;
if (req.session["currentUser"].role !== "0") {
req.session["currentUser"] = newUser;
}
userModel.updateUser(userId, newUser).then(function (user) {
res.send(user);
});
Expand Down

0 comments on commit b96b98c

Please sign in to comment.