In this video.
Let’s go ahead and create a transaction for a specific account.
Let’s say that we want to keep track about expenses for a particular account.
Let’s say building a house for my mom or mom’s building.
So let’s see how we’re going to create a transaction for that.
So inside the controller is definitely we may need the controller for transaction and here we go and
we may need the account, the user and then the transaction.
You may need these three models.
So let’s go ahead and require those models and I’ll explain to you when we need it.
So the user model here again, let’s go to the file.
We save it and now less import it here as user.
Great.
Next one, I’m going to be the account again.
Let’s go to the model.
Save it and now let’s require it here as account.
Perfect.
And lastly, is the actual transaction, save it and let’s require it as transaction A, we have all
the models.
So perfect.
Let’s collapse this one and let’s collapse the user in the account.
Let’s take to the transaction and then the transaction controller.
So let’s lay out these steps here.
Perfect.
So let’s start with a fine user.
Remember, we can get an idea of the user and request that user.
So here we go.
Cost user found is equal to await the user model dot by ID they pass in the red dot user.
Step number two is you want to find the account.
How are you going to do that one?
Well, we can get a count from the request object.
The other option is that we can get a count from the user found.
But here is a problem.
Remember on the user found, we are saving a race of account.
So what account are we going to retrieve?
Well, so to make it pretty easy, I want to pass the ID of the account as params or as a request body.
So I’ll prefer to pass it from the request body than from the URL.
So here we go.
So let’s structure the payload from the request object as that request, that body.
And for this one we need the name of the transaction, the amount of the transaction, and then the
type of transaction and then the nodes by the transaction and then the transaction type.
Great.
So here we go.
Let’s find the accounts as account found.
Is equal to await a count dirt find by id a the account.
I want to send the ID as an account.
So that is it.
Let’s also structure that from the request body as account.
So for this one, in case there is no user, we can go ahead and then through an error as that meaning
that user not found.
So we may need the app error.
So let’s go ahead and then receive it so that we can acquire it.
So inside the transaction controller here, let’s bring in the app error.
Perfect.
So here you go.
If user is not found, we can return and then we are going to call next.
So let’s provide next as a third element.
And for next we’re going to create instance of our error plus.
They pass in the message as user note found.
They pass in the status code as 404.
So the same thing for the account.
We can also check so I can copy this one.
And here let’s check meaning that if account is not found, you will see that account not found.
So below here, let’s go ahead and create our transaction.
So once the transaction is equal to wait, then the transaction don’t create the pass in the payload
as the amount of that, the type of transaction.
After that, the node where the transaction and then the account and then the transaction type and then
the category need to specify that one to and name.
Let’s provide the category as here.
So.
So you would be thinking the difference between the type and transaction type.
So let’s see more about that.
We have the name transaction type.
Yeah.
So let’s remove the type.
There is no type.
It is transaction type.
So let’s remove this one and remove this one.
Yes.
So this what we want to create next step is that let’s push the transaction to the account found as
transactions dot push.
And I’m going to push the transaction, created dirt and score it.
Lastly, let’s receive the account found as dot C and let’s go ahead and send the response to the user.
So here is going to be status here and then you’re going to be success.
And we need this one in the front end and in data as transaction.
Moment of truth.
So let’s get back to the root.
So here and that transaction, which is this one.
Great.
So let’s provide the payload.
Definitely you may need the token.
So let’s go to look in and now let’s copy the token.
And inside the route of transaction crates and the headers here, let’s add the authorization and then
barer and paste it to Queen and let’s provide the body.
So here we go.
The name of the transaction, I would say bind roofing sheet.
And this way I’m going to be the transaction type.
And for this, let’s see, the types is going to be income or expenses.
So for this one, I’m going to be expenses as that is x pane sis.
So the next property is going to be the note, meaning the description about that.
Great.
In this way, I’m going to be the account that we want to create the transaction for.
So if you go to the transaction, sorry, the account, which is this one, so here I think we can implement
a route to fetch or account.
So let’s do the one quickly before we get back to that.
So inside the account controller here is going to be pretty simple to fetch all.
So here are going to be the get.
All right.
So you’re going to make a request as cost and then account is account is equal to a weight and then
the account dot find as that.
And let’s say the account I want to see.
Okay, so save it.
And now let’s get back to the account.
Let’s fetch all the accounts.
That is where is my account.
Transaction account.
So fetch all.
So I copy this one for my mom’s Jordan and then inside the crate I provide as an ID to the account.
And next is going to be the category for that as category and for the category.
Let’s see what we have.
We have food, utility, personal shopping.
I think we can use project or building.
Let’s add billion also in the mix here.
Great.
So I can specify it here as a payload so category going to be built and I think your is the name transaction
node account.
And then yeah.
So let’s say and let’s see what we like now we are going to see the error.
So let’s check it out and let’s see where we can see that user not found, meaning that I’m not passing
the slogan material.
Let’s go to the transaction route here and let’s bring in the log image.
So here is a mature save it and now let’s get to the transaction roots and let’s require it is logged
in.
So here we go then I’ll put it here and has that.
So let’s send the request again and let’s see this time around the token is not valid, so let’s log
in again.
And the users, I think it has expired.
So log in and now let’s copy this one and let’s go to the transaction, create and the headers.
Let’s copy this one and paste it here and let’s send.
And you can see when providing the amount sometimes is how I do it because I want to know a feel which
I forgot to provide and when to be the amount.
So here I provide ice $5,000.
And I think that’s all what we need.
Yeah.
So let’s send a, we have our transaction being added to our account, but we are lacking something,
meaning the user who created this transaction.
So it means that on the field we are not making use of required.
Yes.
So let’s make it a required field to true.
So if I try to create again, I’m not providing the user, so I’ll get an error and there you go.
So let’s go ahead and provide it.
So inside the transaction controller, let’s add the created by and we can get it for rec dot user.
Perfect.
So now we are able to create a transaction for a specific account.
So let’s go ahead and then fetch our account and let’s see if we have a transaction for that which is
this one.
So let’s get st and let’s see and here we go.
You can see that for this account called Month Building, I’ve added one transaction.
So to be able to get the actual data we are going to use populate on that.
So let’s go to the account and getting all we are going to use the populates to provide the transactions,
save it and now let’s make a request again and let’s see the magic.
And there we go, guys.
We can see we have a building account with this transaction.
The transaction is expenses.
So let’s go ahead and also add one more transaction.
So inside this one and a transaction, let’s go ahead and add one more.
This time around, you’re going to be income and I would see funding.
The project.
So.
And note I will say.
Salary for funding this project.
Eh, let’s get sent and let’s check it out.
And here we go.
We have back the transaction.
So if you fetch all account, let’s see.
You can see that for this particular account, we have two transactions.
One is this one and then the other one is this one, which is funding the project with amount and then
transaction type going to be income.
So now I can tell you that we are done with the entire project API creation.
So what has let is about the crowd operation for example, deleting, updating and that one to isn’t
that difficult.