context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Dynamic;
  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;
  using MixERP.Net.Schemas.Policy.Data;
  
  namespace MixERP.Net.Api.Policy
  {
      /// <summary>
      ///     Provides a direct HTTP access to perform various tasks such as adding, editing, and removing Api Access Policies.
      /// </summary>
      [RoutePrefix("api/v1.5/policy/api-access-policy")]
      public class ApiAccessPolicyController : ApiController
      {
          /// <summary>
          ///     The ApiAccessPolicy repository.
          /// </summary>
          private readonly IApiAccessPolicyRepository ApiAccessPolicyRepository;
  
          public ApiAccessPolicyController()
          {
              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.ApiAccessPolicyRepository = new MixERP.Net.Schemas.Policy.Data.ApiAccessPolicy
              {
                  _Catalog = this._Catalog,
                  _LoginId = this._LoginId,
                  _UserId = this._UserId
              };
          }
  
          public ApiAccessPolicyController(IApiAccessPolicyRepository repository, string catalog, LoginView view)
          {
              this._LoginId = view.LoginId.ToLong();
              this._UserId = view.UserId.ToInt();
              this._OfficeId = view.OfficeId.ToInt();
              this._Catalog = catalog;
  
              this.ApiAccessPolicyRepository = repository;
          }
  
          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 "api access policy" entity.
          /// </summary>
          /// <returns>Returns the "api access policy" meta information to perform CRUD operation.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("meta")]
          [Route("~/api/policy/api-access-policy/meta")]
          public EntityView GetEntityView()
          {
              if (this._LoginId == 0)
              {
                  return new EntityView();
              }
  
              return new EntityView
              {
                  PrimaryKey = "api_access_policy_id",
                  Columns = new List<EntityColumn>()
                                  {
                                          new EntityColumn { ColumnName = "api_access_policy_id",  PropertyName = "ApiAccessPolicyId",  DataType = "long",  DbDataType = "int8",  IsNullable = false,  IsPrimaryKey = true,  IsSerial = true,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "user_id",  PropertyName = "UserId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "office_id",  PropertyName = "OfficeId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "poco_type_name",  PropertyName = "PocoTypeName",  DataType = "string",  DbDataType = "text",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "http_action_code",  PropertyName = "HttpActionCode",  DataType = "string",  DbDataType = "text",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "valid_till",  PropertyName = "ValidTill",  DataType = "DateTime",  DbDataType = "date",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          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 api access policies.
          /// </summary>
          /// <returns>Returns the count of the api access policies.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count")]
          [Route("~/api/policy/api-access-policy/count")]
          public long Count()
          {
              try
              {
                  return this.ApiAccessPolicyRepository.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 all collection of api access policy.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("all")]
          [Route("~/api/policy/api-access-policy/all")]
          public IEnumerable<MixERP.Net.Entities.Policy.ApiAccessPolicy> GetAll()
          {
              try
              {
                  return this.ApiAccessPolicyRepository.GetAll();
              }
              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 api access policy for export.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("export")]
          [Route("~/api/policy/api-access-policy/export")]
          public IEnumerable<dynamic> Export()
          {
              try
              {
                  return this.ApiAccessPolicyRepository.Export();
              }
              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 api access policy.
          /// </summary>
          /// <param name="apiAccessPolicyId">Enter ApiAccessPolicyId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("{apiAccessPolicyId}")]
          [Route("~/api/policy/api-access-policy/{apiAccessPolicyId}")]
          public MixERP.Net.Entities.Policy.ApiAccessPolicy Get(long apiAccessPolicyId)
          {
              try
              {
                  return this.ApiAccessPolicyRepository.Get(apiAccessPolicyId);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
  
gt:                     Content = new StringContent(ex.Message),