Tuesday, 21 February 2017

Controller

using BusinessServices;
using BusinessEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApiAzure.Controllers
{
    public class EducationCategoryController : ApiController
    {
        private readonly IEducationCategoryService _educationcategoryServices;

        #region Public Constructor

        public EducationCategoryController()
        {
            _educationcategoryServices = new EducationCategoryService();
        }

        #endregion

        // GET: api/EducationCategory
        public HttpResponseMessage Get()
        {
            var educationcategory = _educationcategoryServices.GetAllEducationCategory();
            if (educationcategory != null)
            {
                var educationcategoryEntities = educationcategory as List<EducationCategory> ?? educationcategory.ToList().Where(i => i.IsActive == true);
                if (educationcategoryEntities.Any())
                    return Request.CreateResponse(HttpStatusCode.OK, educationcategoryEntities);
            }

            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Education Category not found");
        }

        // GET: api/EducationCategory/5
        public HttpResponseMessage Get(int id)
        {
            var educationcategory = _educationcategoryServices.GetEducationCategoryById(id);
            if (educationcategory != null)
                return Request.CreateResponse<EducationCategory>(HttpStatusCode.OK, educationcategory);
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Education Category found for this id");
        }

        // POST: api/EducationCategory
        public int Post([FromBody]EducationCategory educationcategoryEntities)
        {
            return _educationcategoryServices.CreateEducationCategory(educationcategoryEntities);
        }

        // PUT: api/EducationCategory/5
        public bool Put(int id,[FromBody]EducationCategory educationcategoryEntities)
        {
            if (id > 0)
            {
                if (educationcategoryEntities.IsActive == true)
                {
                    return _educationcategoryServices.UpdateEducationCategory(id, educationcategoryEntities);
                }
                else
                {
                    return _educationcategoryServices.DeleteEducationCategory(id, educationcategoryEntities);
                }
            }
            return false;
        }

        // DELETE: api/EducationCategory/5
        public bool Delete(int id, [FromBody]EducationCategory educationcategoryEntities)
        {
            if (id > 0)
                return _educationcategoryServices.DeleteEducationCategory(id, educationcategoryEntities);
            return false;
        }
    }
}

No comments:

Post a Comment