Full Web Apps with FastAPI Transcripts
Chapter: View models and services
Lecture: Services for view models

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we had our little random fake data here,
0:02 but let's move over to using our services to provide data here.
0:05 So what I wanna do is, I want to go to the package_service.
0:09 And I wanna have it answer a couple questions. And I can hit Ctrl + space two
0:12 times and PyCharm will automatically import it up
0:15 there. Awesome, and I wanna have just a couple of functions that'll tell me
0:18 how many releases for packages are there and how many packages are there.
0:22 So we'll say release_count something like that and we'll do right there
0:30 package_count. Let's spell that right: package_count.
0:35 Now, those don't exist yet, as you can see and we'll have user_service. Again
0:40 double Ctrl + space puts it at the top,
0:43 and what we want to do is say,
0:45 user_count. And down here,
0:46 let's say package_service.latest_releases()
0:52 and let's pass in limit=5.
0:57 We don't want to get all 200,000,
0:59 we just want the five latest releases.
1:02 This is what we want our services so far to look like, to provide data over
1:06 here. We can have PyCharm,
1:08 write it for us, and we can say this returns an int, which is great.
1:13 We're gonna need that again. So I'll just copy it.
1:15 And down here user_count and create that function there, returns an int. And this one is
1:23 going to return an int, this is where we're going to write our SQLAlchemy
1:26 queries in a minute, when we get to there. But for now,
1:30 eventually it's gonna be a list of package classes
1:31 but we don't have those yet.
1:33 This is gonna be an int,
1:35 and the default will be five.
1:38 Now, here's where we would go and do a count against a query for the
1:42 release table. We do a count against query for package table or we do an order
1:47 by and then a limit over on.
1:50 I said latest_releases. We want that to be latest_packages.
1:54 Sorry about that. We'll do a
1:56 some kind of query that orders by the release dates and then does some kind of
2:01 limit on that. That's where we're going, when we get to the SQLAlchemy section.
2:05 For now, let's just go and put these numbers back in there.
2:09 So, which one was that? That was package_count.
2:11 So when you get package count, we're returning that number. When we get to the release_count,
2:16 it's that two million. Again,
2:19 this is just fake for now,
2:20 just to get the layers in place.
2:21 I wanna keep going from there,
2:22 right? So user_count is going to be over there,
2:27 clean this whole thing up. Perfect.
2:30 And lastly, here's what we're gonna pass back for the packages and let's go and
2:38 use that limit. Obviously, we're gonna go against the database,
2:41 but because slicing is so easy, we can say go from the first up to however many
2:45 they pass in. That way, if we say two or whatever, we'll be good,
2:50 let's go and run this one more time and
2:51 make sure our app is hanging together. Starts,
2:54 that's a good sign. There you go.
2:55 Look, now we're back to what we started with. Great refactoring,
3:00 huh? So here we've got our projects,
3:02 our releases, our users and here are our latest packages again.
3:05 The difference, though, is this time, around
3:08 the view method. Again, this one's pretty simple
3:10 but when we do like registration stuff,
3:12 there's still gonna be a decent amount of things happening here.
3:15 So this lets us isolate that data exchange,
3:18 This class has one job. Its job is to know what this one temple needs.
3:23 It needs package_count release_count,
3:26 user_count and so on. Its job is to go get that data and then provide it
3:30 to it. And if it were some kind of form,
3:32 accept that data back, validate it, convert it,
3:34 and so on. Cool. So now we've got our app converted over to use view models.