Thursday, November 22, 2018

Part 10 - Server Side Setup of a Controller for your Mobile App

Now that we have our database setup and our repositories defined, we can create the first controller that will accept and respond to requests from our mobile application.  As part of building the controller, we will also define a standard response type for the mobile application such that we can provide some extra context with any data that is returned.

Open Visual Studio 2017 and expand the Controllers folder in the Invoicing.API project.  The Invoicing.API project is the project that will be directly accessed via our mobile application, and the controllers are where these requests will be accepted and from which responses will be made.

You should see the ValuesController already in there.  This can be deleted.

Right-click on the Controllers folder and Add a new controller.

image

Select the API Controller – Empty template and click the Add button.

image

The first Controller we will create will be our API controller base class from which our other controllers will inherit.  This will provide us with some flexibility down the road, but for now, we will leave this empty.

image

image

Next, add another controller and name it AccountController.  Have it inherit from the ApiBaseController.  This controller will be used to handle requests from our login page.

image

Since we will be handling the login in the AccountController, the first end point (i.e. method) we need is the Login method.  We will specify this as an HttpPost via an attribute and additionally specify the end point path to this method as Login.  These methods will always return a string which will eventually be a JSON object.

Quick snapshot of where we are with this method so far.

image

We will add a parameter to this method for the LoginViewModel, we created in a previous post, which we will be sending from the mobile app with the user's login credentials.  We will specify that it will be in the body of the HttpPost as well.

image

And now let's add a simple username and password logic such that we can verify a single user and password.

image

Now that we have a basic outline for this end point, we will define our standard response types.

Create a new class in the Inventory.Core project's Base folder.  We need to add "State", "Message", and "Data" properties that we will return for each response to a request.  We will also add a shortcut to the JsonConvert.SerializeObject method in here such that we don’t have to import this into all of our controllers.  If you haven't added package yet, you can add it via NuGet Package Manager under Newtonsoft.Json namespace.

image

Now, let's head over to our AccountController and use this response object to return the success of failure of the login request.

image

That should be all we need for our first crack at the Login method and a standardized response.

One thing that we will clean up in our next post is moving this business logic out of the controller and into a service in our Invoicing.Core project.  We will create our first business logic service in the next part.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.