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.Core
  {
      /// <summary>
      ///     Provides a direct HTTP access to perform various tasks such as adding, editing, and removing Kanbans.
      /// </summary>
      [RoutePrefix("api/v1.5/core/kanban")]
      public class KanbanController : ApiController
      {
          /// <summary>
          ///     The Kanban data context.
          /// </summary>
          private readonly MixERP.Net.Schemas.Core.Data.Kanban KanbanContext;
  
          public KanbanController()
          {
              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.KanbanContext = new MixERP.Net.Schemas.Core.Data.Kanban
              {
                  _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 "kanban" entity.
          /// </summary>
          /// <returns>Returns the "kanban" meta information to perform CRUD operation.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("meta")]
          [Route("~/api/core/kanban/meta")]
          public EntityView GetEntityView()
          {
              return new EntityView
              {
                  PrimaryKey = "kanban_id",
                  Columns = new List<EntityColumn>()
                                  {
                                          new EntityColumn { ColumnName = "kanban_id",  PropertyName = "KanbanId",  DataType = "long",  DbDataType = "int8",  IsNullable = false,  IsPrimaryKey = true,  IsSerial = true,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "object_name",  PropertyName = "ObjectName",  DataType = "string",  DbDataType = "varchar",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 128 },
                                          new EntityColumn { ColumnName = "user_id",  PropertyName = "UserId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "kanban_name",  PropertyName = "KanbanName",  DataType = "string",  DbDataType = "varchar",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 128 },
                                          new EntityColumn { ColumnName = "description",  PropertyName = "Description",  DataType = "string",  DbDataType = "text",  IsNullable = true,  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 kanbans.
          /// </summary>
          /// <returns>Returns the count of the kanbans.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count")]
          [Route("~/api/core/kanban/count")]
          public long Count()
          {
              try
              {
                  return this.KanbanContext.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 kanban for export.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("export")]
          [Route("all")]
          [Route("~/api/core/kanban/export")]
          [Route("~/api/core/kanban/all")]
          public IEnumerable<MixERP.Net.Entities.Core.Kanban> Get()
          {
              try
              {
                  return this.KanbanContext.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 kanban.
          /// </summary>
          /// <param name="kanbanId">Enter KanbanId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("{kanbanId}")]
          [Route("~/api/core/kanban/{kanbanId}")]
          public MixERP.Net.Entities.Core.Kanban Get(long kanbanId)
          {
              try
              {
                  return this.KanbanContext.Get(kanbanId);
              }
              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/core/kanban/get")]
          public IEnumerable<MixERP.Net.Entities.Core.Kanban> Get([FromUri] long[] kanbanIds)
          {
              try
              {
                  return this.KanbanContext.Get(kanbanIds);
              }
              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 kanbans on each page, sorted by the property KanbanId.
          /// </summary>
          /// <returns>Returns the first page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("")]
          [Route("~/api/core/kanban")]
          public IEnumerable<MixERP.Net.Entities.Core.Kanban> GetPaginatedResult()
          {
              try
              {
                  return this.KanbanContext.GetPaginatedResult();
              }
              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 kanbans on each page, sorted by the property KanbanId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset.</param>
          /// <returns>Returns the requested page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("page/{pageNumber}")]
          [Route("~/api/core/kanban/page/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Core.Kanban> GetPaginatedResult(long pageNumber)
          {
              try
              {
                  return this.KanbanContext.GetPaginatedResult(pageNumber);
              }
              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>
          ///     Counts the number of kanbans using the supplied filter(s).
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns the count of filtered kanbans.</returns>
          [AcceptVerbs("POST")]
          [Route("count-where")]
          [Route("~/api/core/kanban/count-where")]
          public long CountWhere([FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.KanbanContext.CountWhere(f);
              }
              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 filtered and paginated collection containing 10 kanbans on each page, sorted by the property KanbanId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns the requested page from the collection using the supplied filters.</returns>
          [AcceptVerbs("POST")]
          [Route("get-where/{pageNumber}")]
          [Route("~/api/core/kanban/get-where/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Core.Kanban> GetWhere(long pageNumber, [FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.KanbanContext.GetWhere(pageNumber, f);
              }
              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>
          ///     Counts the number of kanbans using the supplied filter name.
          /// </summary>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns the count of filtered kanbans.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count-filtered/{filterName}")]
          [Route("~/api/core/kanban/count-filtered/{filterName}")]
          public long CountFiltered(string filterName)
          {
              try
              {
                  return this.KanbanContext.CountFiltered(filterName);
              }
              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 filtered and paginated collection containing 10 kanbans on each page, sorted by the property KanbanId.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the resultset. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns the requested page from the collection using the supplied filters.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("get-filtered/{pageNumber}/{filterName}")]
          [Route("~/api/core/kanban/get-filtered/{pageNumber}/{filterName}")]
          public IEnumerable<MixERP.Net.Entities.Core.Kanban> GetFiltered(long pageNumber, string filterName)
          {
              try
              {
                  return this.KanbanContext.GetFiltered(pageNumber, filterName);
              }
              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>
          ///     Displayfield is a lightweight key/value collection of kanbans.
          /// </summary>
          /// <returns>Returns an enumerable key/value collection of kanbans.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("display-fields")]
          [Route("~/api/core/kanban/display-fields")]
          public IEnumerable<DisplayField> GetDisplayFields()
          {
              try
              {
                  return this.KanbanContext.GetDisplayFields();
              }
              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>
          ///     A custom field is a user defined field for kanbans.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection of kanbans.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("custom-fields")]
          [Route("~/api/core/kanban/custom-fields")]
          public IEnumerable<PetaPoco.CustomField> GetCustomFields()
          {
              try
              {
                  return this.KanbanContext.GetCustomFields(null);
              }
              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>
          ///     A custom field is a user defined field for kanbans.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection of kanbans.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("custom-fields/{resourceId}")]
          [Route("~/api/core/kanban/custom-fields/{resourceId}")]
          public IEnumerable<PetaPoco.CustomField> GetCustomFields(string resourceId)
          {
              try
              {
                  return this.KanbanContext.GetCustomFields(resourceId);
              }
              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>
          ///     Adds or edits your instance of Kanban class.
          /// </summary>
          /// <param name="kanban">Your instance of kanbans class to add or edit.</param>
          [AcceptVerbs("PUT")]
          [Route("add-or-edit")]
          [Route("~/api/core/kanban/add-or-edit")]
          public object AddOrEdit([FromBody]Newtonsoft.Json.Linq.JArray form)
          {
              MixERP.Net.Entities.Core.Kanban kanban = form[0].ToObject<MixERP.Net.Entities.Core.Kanban>(JsonHelper.GetJsonSerializer());
              List<EntityParser.CustomField> customFields = form[1].ToObject<List<EntityParser.CustomField>>(JsonHelper.GetJsonSerializer());
  
              if (kanban == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  return this.KanbanContext.AddOrEdit(kanban, customFields);
              }
              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>
          ///     Adds your instance of Kanban class.
          /// </summary>
          /// <param name="kanban">Your instance of kanbans class to add.</param>
          [AcceptVerbs("POST")]
          [Route("add/{kanban}")]
          [Route("~/api/core/kanban/add/{kanban}")]
          public void Add(MixERP.Net.Entities.Core.Kanban kanban)
          {
              if (kanban == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  this.KanbanContext.Add(kanban);
              }
              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>
          ///     Edits existing record with your instance of Kanban class.
          /// </summary>
          /// <param name="kanban">Your instance of Kanban class to edit.</param>
          /// <param name="kanbanId">Enter the value for KanbanId in order to find and edit the existing record.</param>
          [AcceptVerbs("PUT")]
          [Route("edit/{kanbanId}")]
          [Route("~/api/core/kanban/edit/{kanbanId}")]
          public void Edit(long kanbanId, [FromBody] MixERP.Net.Entities.Core.Kanban kanban)
          {
              if (kanban == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  this.KanbanContext.Update(kanban, kanbanId);
              }
              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));
              }
          }
  
          private List<MixERP.Net.Entities.Core.Kanban> ParseCollection(dynamic collection)
          {
              return JsonConvert.DeserializeObject<List<MixERP.Net.Entities.Core.Kanban>>(collection.ToString(), JsonHelper.GetJsonSerializerSettings());
          }
  
          /// <summary>
          ///     Adds or edits multiple instances of Kanban class.
          /// </summary>
          /// <param name="collection">Your collection of Kanban class to bulk import.</param>
          /// <returns>Returns list of imported kanbanIds.</returns>
          /// <exception cref="MixERPException">Thrown when your any Kanban class in the collection is invalid or malformed.</exception>
          [AcceptVerbs("PUT")]
          [Route("bulk-import")]
          [Route("~/api/core/kanban/bulk-import")]
          public List<object> BulkImport([FromBody]dynamic collection)
          {
              List<MixERP.Net.Entities.Core.Kanban> kanbanCollection = this.ParseCollection(collection);
  
              if (kanbanCollection == null || kanbanCollection.Count.Equals(0))
              {
                  return null;
              }
  
              try
              {
                  return this.KanbanContext.BulkImport(kanbanCollection);
              }
              catch (UnauthorizedException)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
              }
              catch (MixERPException ex)
              {
                  throw new HttpResponseException(new HttpResponseMessage
                  {
  
gt:                     Content = new StringContent(ex.Message),