00:07
let's extract this organization logic that we have here into a reusable fashion as we're going to need it on our access levels and statuses here as well. So we can extract out our organization ID type into a separate file,
00:17
the exist function into a separate file, and then also the metadata provider where we're calling the function. So inside of our validators folder, let's right-click that new file and let's add in
00:26
a helpers folder with a file called organizations.ts. Inside of here, we can start by exporting a type organization metadata,
00:34
set that equal to organization ID of type number. Inside of our difficulty validator, this will replace this extracted portion right here.
00:42
So we can now import our organization metadata from that helper. While we're in here, let's go ahead and copy the async function in its entirety, and let's cut that out of our exist function,
00:51
jump back into our organization helper, and instead, let's add in an export function called existsInOrganization,
00:58
that will accept in a table of type string and a column of type string defaulted to ID as we're most likely going to want to use that primary key here for all of these checks.
01:08
Then we'll return and plop that function in. Then we need to set types for our database value and field. The type of our database is database.
01:16
The type of our value here is going to be a string or number, and our field is going to be the type field context from vine.js types.
01:24
Rather than reaching for our difficulties in particular, we instead want to reach for the table being provided into our existsInOrganization function. So we'll pass the table into the from there,
01:34
and then we can do the exact same thing here for our select and where, except using the column parameter instead of our table. So we'll pass column in there and there,
01:42
and then everything else here can remain the same for our existsInOrganization. In order to actually make use of this, we'll jump back to our difficulty validator. We just need to call our existsInOrganization function,
01:52
specifying that this is for our difficulties table. This will use our ID as the default column, and then it will return back that query function
02:00
automatically for usage inside of the exists function. Last thing to do is to shore up the metadata being provided into this validator inside of our destroy method from our difficulties controller.
02:10
So right here, we can go ahead and give this whole options object there a cut, as we're not going to need to provide in any other options beyond this metadata in our use case.
02:19
So we can create a function that just returns that entire thing back for reusability. Let's jump back into that organization validator helper,
02:26
and add in an export function called withOrganizationMetadata, that takes in that organization's ID as a type of number,
02:34
and then returns that entire options object. Now, we no longer have organization ID, but just ID, so we'll reference that as such.
02:41
We can now jump back into our difficulty controller and just provide in that withOrganizationMetadata as the entire options object, passing in our organization ID to that.
02:51
Everything should now work the exact same as it did before. The only difference is, we now have ready to go functions to reuse for our access levels and statuses. Before we call this done though, let's jump back into our browser.
03:01
Our expert is the one that has our course bound to it. Let's just grab the hex code of this expert, add in a new difficulty. We're just going to call this expert2,
03:10
and plop that hex code in, add that difficulty, and we can now delete our expert, and set the course to now go on our expert2, hit delete,
03:19
and all seem to work okay. We can right-click, inspect, check out the view dev tools, go into our index page, and now jump down to our expert2,
03:27
check out the metadata, and boom, there is the course's count. So everything still worked a-okay with that entire flow, and now we can edit our expert2,
03:36
just call it expert, and that'll do it.