context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using MixERP.Net.Api.Framework;
  using MixERP.Net.ApplicationState.Cache;
  using MixERP.Net.Common.Extensions;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using Newtonsoft.Json;
  using Newtonsoft.Json.Linq;
  using PetaPoco;
  
  namespace MixERP.Net.Api.HRM
  {
      /// <summary>
      ///     Provides a direct HTTP access to perform various tasks such as adding, editing, and removing Leave Applications.
      /// </summary>
      [RoutePrefix("api/v1.5/hrm/leave-application")]
      public class LeaveApplicationController : ApiController
      {
          /// <summary>
          ///     The LeaveApplication data context.
          /// </summary>
          private readonly MixERP.Net.Core.Modules.HRM.Data.LeaveApplication LeaveApplicationContext;
  
          public LeaveApplicationController()
          {
              this._LoginId = AppUsers.GetCurrent().View.LoginId.ToLong();
              this._UserId = AppUsers.GetCurrent().View.UserId.ToInt();
              this._OfficeId = AppUsers.GetCurrent().View.OfficeId.ToInt();
              this._Catalog = AppUsers.GetCurrentUserDB();
  
              this.LeaveApplicationContext = new MixERP.Net.Core.Modules.HRM.Data.LeaveApplication
              {
                  _Catalog = this._Catalog,
                  _LoginId = this._LoginId,
                  _UserId = this._UserId
              };
          }
  
          public long _LoginId { get; }
          public int _UserId { get; private set; }
          public int _OfficeId { get; private set; }
          public string _Catalog { get; }
  
          /// <summary>
          ///     Creates meta information of "leave application" entity.
          /// </summary>
          /// <returns>Returns the "leave application" meta information to perform CRUD operation.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("meta")]
          [Route("~/api/hrm/leave-application/meta")]
          public EntityView GetEntityView()
          {
              return new EntityView
              {
                  PrimaryKey = "leave_application_id",
                  Columns = new List<EntityColumn>()
                                  {
                                          new EntityColumn { ColumnName = "leave_application_id",  PropertyName = "LeaveApplicationId",  DataType = "long",  DbDataType = "int8",  IsNullable = false,  IsPrimaryKey = true,  IsSerial = true,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "employee_id",  PropertyName = "EmployeeId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "leave_type_id",  PropertyName = "LeaveTypeId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "entered_by",  PropertyName = "EnteredBy",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "applied_on",  PropertyName = "AppliedOn",  DataType = "DateTime",  DbDataType = "date",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "reason",  PropertyName = "Reason",  DataType = "string",  DbDataType = "text",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "start_date",  PropertyName = "StartDate",  DataType = "DateTime",  DbDataType = "date",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "end_date",  PropertyName = "EndDate",  DataType = "DateTime",  DbDataType = "date",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "verification_status_id",  PropertyName = "VerificationStatusId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "verified_by_user_id",  PropertyName = "VerifiedByUserId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "verified_on",  PropertyName = "VerifiedOn",  DataType = "DateTime",  DbDataType = "date",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "verification_reason",  PropertyName = "VerificationReason",  DataType = "string",  DbDataType = "varchar",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 128 },
                                          new EntityColumn { ColumnName = "audit_user_id",  PropertyName = "AuditUserId",  DataType = "int",  DbDataType = "int4",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "audit_ts",  PropertyName = "AuditTs",  DataType = "DateTime",  DbDataType = "timestamptz",  IsNullable = true,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 }
                                  }
              };
          }
  
          /// <summary>
          ///     Counts the number of leave applications.
          /// </summary>
          /// <returns>Returns the count of the leave applications.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count")]
          [Route("~/api/hrm/leave-application/count")]
          public long Count()
          {
              try
              {
                  return this.LeaveApplicationContext.Count();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns collection of leave application for export.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("export")]
          [Route("~/api/hrm/leave-application/export")]
          public IEnumerable<MixERP.Net.Entities.HRM.LeaveApplication> Get()
          {
              try
              {
                  return this.LeaveApplicationContext.Get();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Returns an instance of leave application.
          /// </summary>
          /// <param name="leaveApplicationId">Enter LeaveApplicationId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("{leaveApplicationId}")]
          [Route("~/api/hrm/leave-application/{leaveApplicationId}")]
          public MixERP.Net.Entities.HRM.LeaveApplication Get(long leaveApplicationId)
          {
              try
              {
                  return this.LeaveApplicationContext.Get(leaveApplicationId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          [AcceptVerbs("GET", "HEAD")]
          [Route("get")]
          [Route("~/api/hrm/leave-application/get")]
          public IEnumerable<MixERP.Net.Entities.HRM.LeaveApplication> Get([FromUri] long[] leaveApplicationIds)
          {
              try
              {
                  return this.LeaveApplicationContext.Get(leaveApplicationIds);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
                      Content = new StringContent(ex.Message),
                      StatusCode = HttpStatusCode.InternalServerError
                  });
              }
              catch
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
              }
          }
  
          /// <summary>
          ///     Creates a paginated collection containing 10 leave applications on each page, sorted by the property LeaveApplicationId.
          /// </summary>
          /// <returns>Returns the first page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("")]
          [Route("~/api/hrm/leave-application")]
          public IEnumerable<MixERP.Net.Entities.HRM.LeaveApplication> GetPagedResult()
          {
              try
              {
                  return this.LeaveApplicationContext.GetPagedResult();
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
  
gt:                     Content = new StringContent(ex.Message),