So now we want to start with the users, right?
We want to be able to manage users as an admin.
So we need to first create some of the back end routes and controller functions.
So let's go to the user controller, which right now we have of course, we have our authentication,
our register, our logout, our get and update user profile.
Now we basically just want to get users, get user by ID, delete and update user.
So get users is going to be very, very simple.
Let's just get rid of this and let's say const users and let's set that to await and then we're going
to from our user model, we're going to call find and we just want to find with an empty object because
we want all users.
And then we're going to raise, although I don't want to send I want to do status.
200 and then Jason and then send the users.
Okay, so simple as that.
And then get user by ID.
So let's.
Set user to await user dot find by ID.
We're going to pass in the ID from the URL.
We don't want the password, so we're going to add select minus password and then check for the user.
If there's a user, we'll send it else.
Then we'll send a 404.
So pretty simple.
And then let's see.
To delete a user, we're going to get rid of that.
And let's do const user.
So first we want to get it and then we're going to check for the user.
If there's a user, then we're going to call.
Let's see.
One thing I don't I want to do is make sure that we can't delete a user if it's an admin.
So I'm going to put another if statement in here and we're going to say if the user dot is admin.
So if that's true, then we don't want to delete the user.
We'll say res dot status.
400, which is a client error and then we're going to throw a new error.
Cannot delete admin user.
Okay.
Now under that if statement, we're then going to await user dot and we're going to use delete one.
And we'll pass in here an object with the ID that matches the ID of the user that we fetch.
So user underscore ID.
Okay, After we do that, then we're going to just set a 201.
User deleted successfully.
And then let's just add an else on to this.
So if there is no user, then we want a 404 and the user is not found.
And then the last thing I want to do is the update.
So for that, we're going to.
Get the user just like we did above and then check for the user.
And if there is one, then we're going to set, let's say, user dot name equal to the request dot body
dot name.
Or if if there is no name in the body, use the user name that's already there.
Same with the email.
And let's do same with the is admin.
Although.
We're going to make sure that this is a Boolean, so we're going to wrap it in.
Boolean.
Okay.
And then let's say updated user.
So we're going to actually save the user here, put the result into this variable, then we're going
to respond.
So res dot status.
200 dot.
Jason.
And we're going to pass in here.
Let's do the ID of the updated user, the name, the email and the is admin of the updated user.
Okay.
Else then we're going to send a 404.
And that's it.
So if you want to test it through Postman, you can do that.
Just make sure you authenticate first and you include your cookie.
But we're just going to jump right into the front end and get working on that in the next video.