Computer Magic Logo
Controller

Monday, March 14, 2016

Published by Aristotelis Pitaridis

The controller is going to execute the code which will collect the data from the client and database and send back the result of the data that we want to deliver. Usually there are three different ways of calling the actions of the controller.

The first possibility is when the client asks for data without giving any information to the action. We will use this method in order to get a list of all the products.

The second possibility is when we give an id which is useful for the action to have the minimum amount of data in order to get a record from the database. This method can be used when we want to get a record or when we want to delete a record.

The last possibility is when the client sends an entire object to the action. This can be used when we create a new record or when we update an existing record.

In our controller we will define one action for each of these methods so that we will see how to use them in our application.

using ComputerMagic.Models;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using Umbraco.Web.WebApi;

namespace ComputerMagic.Controllers
{
    public class ProductsApiController : UmbracoApiController     // it must end with ApiController
    {
        List<Product> products = new List<Product>()
        {
            new Product { ID = 1, Name = "Product 1", Price = 12 },
            new Product { ID = 2, Name = "Product 2", Price = 22 },
            new Product { ID = 3, Name = "Product 3", Price = 32 },
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public Product GetProductById(int id)
        {
            var product = products.FirstOrDefault((p) => p.ID == id);
            if (product == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            return product;
        }

        [HttpPost]
        public object Update([FromBody]Product model)
        {
            return new
            {
                ID = model.ID,
                ProductExistsInDatabase = true,
            };
        }
    }
}

The name of our controller must end with ApiController. In order to keep things simple we do not get the data from the database but we have a static list of information which is going to be the source of data that we will use for our example.

In our controller we have three different actions one for each of the methods we defined earlier.

The first action gets no parameters and it returns a list of all the products. The output is going to be an IEnumerable list of type products.

The second action gets one parameter which is going to be the id of the information that the client requests and it returns an object of type Product.

The final action gets an entire object as parameter of type Product and it returns an object. We did not use our model in order to return the data to the client so that we will see how to return a non-defined object to the client. We also used the HttpPost attribute which informs the action that it is going to be called using the post method.