In this video, learn how to reuse the collection classes to return a collection of users in the system.
- [Narrator] Let's create a resource to represent a user. I'll create a new model called User, and this will be a Resource. A User will have a email address, a first and last name, and a creation timestamp. DateTimeOffset CreatedAt.
I've added a user service and a users controller to this project. You can find these classes in the controllers and services folders in the exercise files for this video. I'll go ahead and open up the UsersController. There's a few methods in here, but they're all just stubs right now, they don't really do anything. I'm gonna work on this get visible users method. When we add authorization to the API later on, we'll add a check here so that only administrators can see the whole collection of users in this system. For now we'll return the whole collection for anyone who calls this method.
We'll do that here, and we'll say users equals await talk to the UserService.GetUsersAsync, We'll pass along pagingOptions and sortOptions and searchOptions. Then we'll create a collection response, PagedCollection of type user, create a link to this collection, return the items that we got back from the service, TotalSize here and pagingOptions.
And then we need to return that collection. I'll update the method header to return ActionResult of, this will be a PagedCollection of User. Now over to the service. Let's open up user service, or the DefaultUserService, rather, and take a look at the GetUsersAsync method. Nothing much here yet. I'll fill out this method along the same pattern as most of our other service methods.
I'll say, Iqueryable of type UserEntity, create a query from userManager.Users. I'll apply the searchOptions and do the same thing with the sortOptions, get the size, import that namespace, grab the items next, we'll say query dot skip pagingOptions dot offset dot Value, do the same thing for Take for pagingOptions.Limit.
Then we'll use AutoMapper and do ProjectTo, import that AutoMapper namespace. We're projecting these all to type User, the resource type. We need to pass in the mappingConfiguration for this to work. Then we'll do ToArrayAsync. Now I'll return a new PageResults of type User, pick Items and TotalSize.
We also need to tell AutoMapper how to map from user entity to the user resource I'll do that in the MappingProfile. I'll go to the MappingProfile class, and scroll down, I guess the bottom is fine. We'll do CreatMap for UserEntity to User, ForMember Self, this will be the self link, and what we'll do, we'll say map that from, and this will be created from Link.To, we need a route name, this will be Controller dot UsersController dot GetUserById.
We also need to pass a new anonymous object and say userId equals source dot Id. All right, that was a bit of code, so let's try this out and see what we get. I'll switch over to Postman and send a request to slash users. I forgot one thing. It's telling me I don't have that service activated. What I forgot to do is go to Startup and add this to configure services. I need to do services.AddScoped the IUserService and map that to the DefaultUserService.
All right, let's try that again. The users route returns a list of all the users in the system, and, as I mentioned, later we'll add some authorization so not everyone can just pull down the whole list of users in the system. Other than that it'll act exactly like any of our other RESTful collections. If getting the users collection gives you user resources like this, then it would make sense that posting to the users collection would create a new user. In the next video we'll create a route that allows the user to register and create a new account by posting to this resource.
- What is RESTful design?
- Building a new API with ASP.NET Core
- Using HTTP methods
- Returning JSON
- Creating RESTful routing with templates
- Securing RESTful APIs with HTTPS
- Representing resources
- Representing links
- Representing collections
- Sorting and searching collections
- Building forms
- Adding caching to an ASP.NET Core API
- Configuring user authentication and authorization