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.Core.Data;
  
  namespace MixERP.Net.Api.Core
  {
      /// <summary>
      ///     Provides a direct HTTP access to perform various tasks such as adding, editing, and removing Week Days.
      /// </summary>
      [RoutePrefix("api/v1.5/core/week-day")]
      public class WeekDayController : ApiController
      {
          /// <summary>
          ///     The WeekDay repository.
          /// </summary>
          private readonly IWeekDayRepository WeekDayRepository;
  
          public WeekDayController()
          {
              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.WeekDayRepository = new MixERP.Net.Schemas.Core.Data.WeekDay
              {
                  _Catalog = this._Catalog,
                  _LoginId = this._LoginId,
                  _UserId = this._UserId
              };
          }
  
          public WeekDayController(IWeekDayRepository repository, string catalog, LoginView view)
          {
              this._LoginId = view.LoginId.ToLong();
              this._UserId = view.UserId.ToInt();
              this._OfficeId = view.OfficeId.ToInt();
              this._Catalog = catalog;
  
              this.WeekDayRepository = 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 "week day" entity.
          /// </summary>
          /// <returns>Returns the "week day" meta information to perform CRUD operation.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("meta")]
          [Route("~/api/core/week-day/meta")]
          public EntityView GetEntityView()
          {
              if (this._LoginId == 0)
              {
                  return new EntityView();
              }
  
              return new EntityView
              {
                  PrimaryKey = "week_day_id",
                  Columns = new List<EntityColumn>()
                                  {
                                          new EntityColumn { ColumnName = "week_day_id",  PropertyName = "WeekDayId",  DataType = "int",  DbDataType = "int4",  IsNullable = false,  IsPrimaryKey = true,  IsSerial = false,  Value = "",  MaxLength = 0 },
                                          new EntityColumn { ColumnName = "week_day_code",  PropertyName = "WeekDayCode",  DataType = "string",  DbDataType = "varchar",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 12 },
                                          new EntityColumn { ColumnName = "week_day_name",  PropertyName = "WeekDayName",  DataType = "string",  DbDataType = "varchar",  IsNullable = false,  IsPrimaryKey = false,  IsSerial = false,  Value = "",  MaxLength = 50 }
                                  }
              };
          }
  
          /// <summary>
          ///     Counts the number of week days.
          /// </summary>
          /// <returns>Returns the count of the week days.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count")]
          [Route("~/api/core/week-day/count")]
          public long Count()
          {
              try
              {
                  return this.WeekDayRepository.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 week day.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("all")]
          [Route("~/api/core/week-day/all")]
          public IEnumerable<MixERP.Net.Entities.Core.WeekDay> GetAll()
          {
              try
              {
                  return this.WeekDayRepository.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 week day for export.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("export")]
          [Route("~/api/core/week-day/export")]
          public IEnumerable<dynamic> Export()
          {
              try
              {
                  return this.WeekDayRepository.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 week day.
          /// </summary>
          /// <param name="weekDayId">Enter WeekDayId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("{weekDayId}")]
          [Route("~/api/core/week-day/{weekDayId}")]
          public MixERP.Net.Entities.Core.WeekDay Get(int weekDayId)
          {
              try
              {
                  return this.WeekDayRepository.Get(weekDayId);
              }
              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/week-day/get")]
          public IEnumerable<MixERP.Net.Entities.Core.WeekDay> Get([FromUri] int[] weekDayIds)
          {
              try
              {
                  return this.WeekDayRepository.Get(weekDayIds);
              }
              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 the first instance of week day.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("first")]
          [Route("~/api/core/week-day/first")]
          public MixERP.Net.Entities.Core.WeekDay GetFirst()
          {
              try
              {
                  return this.WeekDayRepository.GetFirst();
              }
              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 the previous instance of week day.
          /// </summary>
          /// <param name="weekDayId">Enter WeekDayId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("previous/{weekDayId}")]
          [Route("~/api/core/week-day/previous/{weekDayId}")]
          public MixERP.Net.Entities.Core.WeekDay GetPrevious(int weekDayId)
          {
              try
              {
                  return this.WeekDayRepository.GetPrevious(weekDayId);
              }
              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 the next instance of week day.
          /// </summary>
          /// <param name="weekDayId">Enter WeekDayId to search for.</param>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("next/{weekDayId}")]
          [Route("~/api/core/week-day/next/{weekDayId}")]
          public MixERP.Net.Entities.Core.WeekDay GetNext(int weekDayId)
          {
              try
              {
                  return this.WeekDayRepository.GetNext(weekDayId);
              }
              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 the last instance of week day.
          /// </summary>
          /// <returns></returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("last")]
          [Route("~/api/core/week-day/last")]
          public MixERP.Net.Entities.Core.WeekDay GetLast()
          {
              try
              {
                  return this.WeekDayRepository.GetLast();
              }
              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 week days on each page, sorted by the property WeekDayId.
          /// </summary>
          /// <returns>Returns the first page from the collection.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("")]
          [Route("~/api/core/week-day")]
          public IEnumerable<MixERP.Net.Entities.Core.WeekDay> GetPaginatedResult()
          {
              try
              {
                  return this.WeekDayRepository.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 week days on each page, sorted by the property WeekDayId.
          /// </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/week-day/page/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Core.WeekDay> GetPaginatedResult(long pageNumber)
          {
              try
              {
                  return this.WeekDayRepository.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 week days using the supplied filter(s).
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns the count of filtered week days.</returns>
          [AcceptVerbs("POST")]
          [Route("count-where")]
          [Route("~/api/core/week-day/count-where")]
          public long CountWhere([FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.WeekDayRepository.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 week days on each page, sorted by the property WeekDayId.
          /// </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/week-day/get-where/{pageNumber}")]
          public IEnumerable<MixERP.Net.Entities.Core.WeekDay> GetWhere(long pageNumber, [FromBody]JArray filters)
          {
              try
              {
                  List<EntityParser.Filter> f = filters.ToObject<List<EntityParser.Filter>>(JsonHelper.GetJsonSerializer());
                  return this.WeekDayRepository.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 week days using the supplied filter name.
          /// </summary>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns the count of filtered week days.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("count-filtered/{filterName}")]
          [Route("~/api/core/week-day/count-filtered/{filterName}")]
          public long CountFiltered(string filterName)
          {
              try
              {
                  return this.WeekDayRepository.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 week days on each page, sorted by the property WeekDayId.
          /// </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/week-day/get-filtered/{pageNumber}/{filterName}")]
          public IEnumerable<MixERP.Net.Entities.Core.WeekDay> GetFiltered(long pageNumber, string filterName)
          {
              try
              {
                  return this.WeekDayRepository.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 week days.
          /// </summary>
          /// <returns>Returns an enumerable key/value collection of week days.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("display-fields")]
          [Route("~/api/core/week-day/display-fields")]
          public IEnumerable<DisplayField> GetDisplayFields()
          {
              try
              {
                  return this.WeekDayRepository.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 week days.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection of week days.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("custom-fields")]
          [Route("~/api/core/week-day/custom-fields")]
          public IEnumerable<PetaPoco.CustomField> GetCustomFields()
          {
              try
              {
                  return this.WeekDayRepository.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 week days.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection of week days.</returns>
          [AcceptVerbs("GET", "HEAD")]
          [Route("custom-fields/{resourceId}")]
          [Route("~/api/core/week-day/custom-fields/{resourceId}")]
          public IEnumerable<PetaPoco.CustomField> GetCustomFields(string resourceId)
          {
              try
              {
                  return this.WeekDayRepository.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 WeekDay class.
          /// </summary>
          /// <param name="weekDay">Your instance of week days class to add or edit.</param>
          [AcceptVerbs("POST")]
          [Route("add-or-edit")]
          [Route("~/api/core/week-day/add-or-edit")]
          public object AddOrEdit([FromBody]Newtonsoft.Json.Linq.JArray form)
          {
              dynamic weekDay = form[0].ToObject<ExpandoObject>(JsonHelper.GetJsonSerializer());
              List<EntityParser.CustomField> customFields = form[1].ToObject<List<EntityParser.CustomField>>(JsonHelper.GetJsonSerializer());
  
              if (weekDay == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  return this.WeekDayRepository.AddOrEdit(weekDay, 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 WeekDay class.
          /// </summary>
          /// <param name="weekDay">Your instance of week days class to add.</param>
          [AcceptVerbs("POST")]
          [Route("add/{weekDay}")]
          [Route("~/api/core/week-day/add/{weekDay}")]
          public void Add(MixERP.Net.Entities.Core.WeekDay weekDay)
          {
              if (weekDay == null)
              {
                  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.MethodNotAllowed));
              }
  
              try
              {
                  this.WeekDayRepository.Add(weekDay);
              }
              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 WeekDay class.
          /// </summary>
          /// <param name="weekDay">Your instance of WeekDay class to edit.</param>
          /// <param name="weekDayId">Enter the value for WeekDayId in order to find and edit the existing record.</param>
          [AcceptVerbs("PUT")]
          [Route("edit/{weekDayId}")]
          [Route("~/api/core/week-day/edit/{weekDayId}")]
          public void Edit(int weekDayId, [FromBody] MixERP.Net.Entities.Core.WeekDay weekDay)
          {
  
gt:             if (weekDay == null)