Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A great place to start when architecting an API is the model. Let's build out what we will be using.
-
0:00
Well, it is that time again.
-
0:01
It is time to define our model.
-
0:03
So we know we will have a course model object, and
-
0:06
we also know we will have a review model object.
-
0:09
Now each course will possibly have many reviews.
-
0:13
Now you might recognize this type of model relationship as one to many.
-
0:17
One course, many reviews.
-
0:19
Let's do some modeling.
-
0:22
Okay so let's make a new IntelliJ project and let's make it Gradle based.
-
0:25
So I'm gonna click Create New Project and
-
0:28
I'm gonna make sure that Gradle is checked over here.
-
0:31
And I'm gonna click Next.
-
0:34
And for the Group ID, let's make that com.teamtreehouse.courses.
-
0:39
And for the artifact we will call this course reviews.
-
0:42
Right, we're writing reviews for courses.
-
0:44
Click next.
-
0:45
We're going to go ahead and make sure that we check the create directories for
-
0:48
empty content routes automatically.
-
0:49
I’m going to click next.
-
0:52
And we will keep it in the same place in the home directory.
-
0:55
Click finish.
-
0:58
Okay, not let's create our model object.
-
1:00
So if you open up over here under the source directory main Java,
-
1:04
we'll right click.
-
1:05
Choose New, Java Class.
-
1:07
Then we wanna blow that all the way out.
-
1:09
We wanna say com.teamtreehouse.courses.model and
-
1:15
first we'll build a thing called Course in the model package.
-
1:20
Okay, so our database that we're going to eventually use is going to use an internal
-
1:24
identifier for a primary key.
-
1:26
But this is common amongst just about every one of these things.
-
1:30
So we're gonna make a thing here called id.
-
1:31
And this is common practice, it's either a long or an integer.
-
1:35
So we'll make an id, and, what else will a course have?
-
1:38
A course will have a name.
-
1:41
We'll also have a URL, right?
-
1:44
So, it'll be the name of the course and the URL of where to find it.
-
1:47
All right. So let's go ahead and
-
1:49
generate our constructor.
-
1:51
So I'm going to go Code Generate and again that's Command N on a Mac
-
1:58
and we are going to generate a Constructor.
-
2:03
And for now let's just leave the ID out.
-
2:04
Right. We can just make a new course with name in
-
2:06
url, because it's an integer it will just be zero until it get's set properly.
-
2:11
Okay and then let's go ahead and let's add our getter's and our setters So,
-
2:14
we'll generate getters, and setters, and we will generate for all of them.
-
2:20
That's fine.
-
2:21
And, let;s go ahead, and while we're here, let's also do the equals, and hash code.
-
2:25
Might as well, right?
-
2:26
It's gonna write the code for us, might as well let it.
-
2:28
So, we're just gonna use the IntelliJ default.
-
2:31
We will use all of those.
-
2:33
Those are all expected in the fields that are non null.
-
2:38
They're both not null right?
-
2:39
We should always have those.
-
2:43
Okay, and now very similarly, let's make our review object.
-
2:46
So we'll do new, Java Class and we're in that same model directory, model.
-
2:52
And we'll say review and again this will also have an id.
-
2:56
So I'll do private int id.
-
2:59
And for now, lets just store the parent courses id.
-
3:03
You know you never want to go over board unless you end up needing it.
-
3:06
Like we could actually put a course in here, but for now,
-
3:09
let's just stick with the id.
-
3:10
That's what we're going to do, right?
-
3:12
We're just going to use this id.
-
3:13
So remember, keep it simple, smarty pants, K-I-S-S.
-
3:18
All right.
-
3:18
So it's private int, and it's a course id.
-
3:22
And that's going to refer to the course that we are writing a review about.
-
3:25
So we've got a rating.
-
3:28
And we also have a comment here.
-
3:33
Okay and very similarly lets go ahead and lets do our generating.
-
3:37
We'll generate a constructor.
-
3:39
Lets make the course id required, but not the id itself.
-
3:42
We'll let the dao objects take care of that later here in a bit.
-
3:46
So and now we will make getters and setters.
-
3:50
Lets get for all of them.
-
3:52
And also let's make the equals in hash code when we're in here just to play nice.
-
3:58
Okay those should all be included,
-
4:01
all those should be included the non null field.
-
4:05
Yeah comments could probably be null.
-
4:07
All right.
-
4:09
So now you can see that the review has a reference to the course id
-
4:13
simply by the way of the id.
-
4:15
Now, we have made it required for creation.
-
4:19
You can't create a review without having a course id.
-
4:22
All right, so we just created our plain, old Java objects, and now,
-
4:26
we need to create a way to serialize those.
-
4:28
I had mentioned that we are going to be using Sequel, and more specifically,
-
4:31
we'll embrace the classical design pattern of data access objects, or dao.
-
4:36
Let's get that all ready to rock right after this quick break.
You need to sign up for Treehouse in order to download course files.
Sign up