
No Comments Yet
Be the first to share your thoughts and start the conversation.
Be the first to share your thoughts and start the conversation.
Complete source code available till this point of lesson is available at
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
In this lesson, we learn how to implement dynamic account routes similar to the user routes. The focus is on creating the necessary API endpoints for account management, which includes methods for retrieving, deleting, and updating accounts.
00:00:02 In the same way how we created the user routes, we also created the account routes.
00:00:07 But for users, we had dynamic routes as well.
00:00:11 And now we have to do the same dynamic routes for accounts.
00:00:15 So let me create a new folder.
00:00:18 which is going to be a square bracket ID folder, dynamic for the ID parameter, within which we can create a new route.ts file.
00:00:26 Within it, we need to create the same three routes we had in the users.
00:00:30 GetUsers, DeleteUsers, and PutUsers.
00:00:33 We have to be able to do the same things for the accounts, so I will simply copy and paste all of them there.
00:00:41 I'll save the file, and believe it or not, automatically my ESLint and Prettier will have brought all of the imports from that file right here as well,
00:00:49 which is very nice to see.
00:00:52 If they didn't bring them for you, you might need to manually import them.
00:00:55 But don't worry because we'll still go over them and see if we have to make any changes.
00:01:01 First, we have to get an account by ID.
00:01:04 So instead of saying we cannot get a user, I'll say cannot get account.
00:01:08 Then we connect to the database.
00:01:10 We try to fetch an account by saying await account.findById.
00:01:18 If no account, throw new not found error for the account and finally return the account.
00:01:27 Okay, that was a super simple change, right?
00:01:30 The next one is a delete.
00:01:32 So in the delete, we again try to fetch an account, not a user.
00:01:37 Then we try to delete this account, not the user.
00:01:41 And then we can check whether we have deleted it.
00:01:44 If not, we say not found account.
00:01:46 And finally, we return the account.
00:01:50 That was super simple.
00:01:51 But what about the put or in other words, the update?
00:01:56 That's also pretty simple to implement.
00:01:58 If not found, we didn't find an account.
00:02:01 Next, we get the account schema.
00:02:04 Not the user schema that we want to validate.
00:02:07 And in this case, we'll save parse it because in the body, we might have some things that are better kept private.
00:02:13 Next, we can do an if statement and check if no validated data dot success.
00:02:21 In that case, we can throw a new validation error to which we can pass the validated data dot error dot flatten.
00:02:34 and then dot field errors, like this.
00:02:37 If that succeeds, we know that we're going to get the updated account by calling the account dot find by ID and update, to which we pass the ID of the account,
00:02:48 the validated data, and the new is set to true.
00:02:51 If we don't have an updated account, we return not found account.
00:02:56 Finally, at the end, we return that updated account.
00:02:59 And this is it.
00:03:00 Super simple, right?
00:03:02 Once we have the structure and what you know what we're doing.
00:03:05 Now, don't get me wrong.
00:03:07 I totally get that we have created a lot of different route handlers, or in other words, API routes, that it makes it a bit hard to understand why we're
00:03:17 creating them in the first place, because we're not yet putting them to use.
00:03:21 Trust me, I totally get that.
00:03:24 I just wanted to make sure that we write a couple of these user-related route handlers, and then very, very soon we'll put them to use on the front-end side.
00:03:32 Just bear with me for one more lesson.
00:03:35 Let me push it by saying something like implement dynamic account routes.
00:03:41 Commit, sync, and we're good.