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

Talk Python's Mastodon Michael Kennedy's Mastodon