using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using NorthwindRazorAPI.BusinessObject;
using NorthwindRazorAPI.Models;
using NorthwindRazorAPI.Domain;
using System.Collections.Generic;
using System.Text.Json;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
 
namespace NorthwindRazorWebAPI.ApiControllers.Base
{
     /// <summary>
     /// Base class for ProductsApiController.  Do not make changes to this class,
     /// instead, put additional code in the ProductsApiController class
     /// </summary>
     public class ProductsApiControllerBase : ControllerBase
     {
 
         /// <summary>
         /// Inserts/Adds/Creates a new record in the database
         /// </summary>
         /// <param name="model">Pass the ProductsModel here.  Arrives as ProductsFields which automatically strips the data annotations from the ProductsModel.</param>
         /// <returns>Task of IActionResult</returns>
         [HttpPost]
         public async Task<IActionResultInsert([FromBody]ProductsModel modelbool isForListInlineOrListCrud = false)
         {
             // add a new record
             return await AddEditProductsAsync(modelCrudOperation.Add, isForListInlineOrListCrud);
         }
 
         /// <summary>
         /// Updates an existing record in the database by primary key.  Pass the primary key in the ProductsModel
         /// </summary>
         /// <param name="model">Pass the ProductsModel here.  Arrives as ProductsFields which automatically strips the data annotations from the ProductsModel.</param>
         /// <returns>Task of IActionResult</returns>
         [HttpPost]
         public async Task<IActionResultUpdate([FromBody]ProductsModel modelbool isForListInlineOrListCrud = false)
         {
             // update existing record
             return await AddEditProductsAsync(modelCrudOperation.Update, isForListInlineOrListCrud);
         }
 
         /// <summary>
         /// Deletes an existing record by primary key
         /// </summary>
         /// <param name="id">ProductID</param>
         /// <returns>Task of IActionResult</returns>
         [HttpDelete]
         public async Task<IActionResultDelete(int id)
         {
             try
             {
                 // delete a record based on id(s)
                 await Products.DeleteAsync(id);
 
                 // everthing went well
                 return Ok();
             }
             catch (Exception ex)
             {
 
                 // something went wrong
                 return BadRequest("Error Message: " + ex.Message + " Stack Trace: " + ex.StackTrace);
             }
         }
 
         /// <summary>
         /// Deletes multiple records based on the comma-delimited ids (primary keys)
         /// </summary>
         /// <param name="ids">List of ProductID</param>
         /// <returns>Task of IActionResult</returns>
         [HttpDelete]
         public async Task<IActionResultDeleteMultiple(string ids)
         {
             try
             {
                 // split ids into a List
                 List<Int32productIDList = ids.Split(",").Select(Int32.Parse).ToList();
 
                 // delete multiple records based on a list of ids (primary keys)
                 await Products.DeleteMultipleAsync(productIDList);
 
                 // everthing went well
                 return Ok();
             }
             catch (Exception ex)
             {
                 // something went wrong
                 return BadRequest("Error Message: " + ex.Message + " Stack Trace: " + ex.StackTrace);
             }
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="page">Current page</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTake(string sidxstring sordint pageint rows)
         {
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="_search">true or false</param>
         /// <param name="nd">nd</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <param name="page">Current page</param>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="filters">Optional.  Filters used in search</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeWithFilters(string _searchstring ndint rowsint pagestring sidxstring sordstring filters = "")
         {
             intproductID = null;
             string productName = String.Empty;
             intsupplierID = null;
             intcategoryID = null;
             string quantityPerUnit = String.Empty;
             decimalunitPrice = null;
             Int16unitsInStock = null;
             Int16unitsOnOrder = null;
             Int16reorderLevel = null;
             booldiscontinued = null;
 
             if (!String.IsNullOrEmpty(filters))
             {
                 // deserialize filters and get values being searched
                 var jsonResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<stringdynamic>>(filters);
 
                 foreach (var rule in jsonResult["rules"])
                 {
                     if (rule["field"].Value.ToLower() == "productid")
                         productID = Convert.ToInt32(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "productname")
                         productName = rule["data"].Value;
 
                     if (rule["field"].Value.ToLower() == "supplierid")
                         supplierID = Convert.ToInt32(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "categoryid")
                         categoryID = Convert.ToInt32(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "quantityperunit")
                         quantityPerUnit = rule["data"].Value;
 
                     if (rule["field"].Value.ToLower() == "unitprice")
                         unitPrice = Convert.ToDecimal(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "unitsinstock")
                         unitsInStock = Convert.ToInt16(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "unitsonorder")
                         unitsOnOrder = Convert.ToInt16(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "reorderlevel")
                         reorderLevel = Convert.ToInt16(rule["data"].Value);
 
                     if (rule["field"].Value.ToLower() == "discontinued")
                         discontinued = Convert.ToBoolean(rule["data"].Value);
 
                 }
 
                 // sometimes jqgrid assigns a -1 to numeric fields when no value is assigned
                 // instead of assigning a null, we'll correct this here
                 if (productID == -1)
                     productID = null;
 
                 if (supplierID == -1)
                     supplierID = null;
 
                 if (categoryID == -1)
                     categoryID = null;
 
                 if (unitPrice == -1)
                     unitPrice = null;
 
                 if (unitsInStock == -1)
                     unitsInStock = null;
 
                 if (unitsOnOrder == -1)
                     unitsOnOrder = null;
 
                 if (reorderLevel == -1)
                     reorderLevel = null;
 
             }
 
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records based on filters
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeDynamicWhereAsync(productIDproductNamesupplierIDcategoryIDquantityPerUnitunitPriceunitsInStockunitsOnOrderreorderLeveldiscontinuedrowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="page">Current page</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeWithTotals(string sidxstring sordint pageint rows)
         {
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="page">Current page</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeGroupedBySupplierID(string sidxstring sordint pageint rows)
         {
             // using a groupBy field in the jqgrid passes that field
             // along with the field to sort, remove the groupBy field
             string groupBy = "CompanyName asc, ";
             sidx = sidx.Replace(groupBy"");
 
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="page">Current page</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeGroupedByCategoryID(string sidxstring sordint pageint rows)
         {
             // using a groupBy field in the jqgrid passes that field
             // along with the field to sort, remove the groupBy field
             string groupBy = "CategoryName asc, ";
             sidx = sidx.Replace(groupBy"");
 
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="page">Current page</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeTotalsGroupedBySupplierID(string sidxstring sordint pageint rows)
         {
             // using a groupBy field in the jqgrid passes that field
             // along with the field to sort, remove the groupBy field
             string groupBy = "CompanyName asc, ";
             sidx = sidx.Replace(groupBy"");
 
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="sidx">Field to sort.  Can be an empty string.</param>
         /// <param name="sord">asc or an empty string = ascending.  desc = descending</param>
         /// <param name="page">Current page</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeTotalsGroupedByCategoryID(string sidxstring sordint pageint rows)
         {
             // using a groupBy field in the jqgrid passes that field
             // along with the field to sort, remove the groupBy field
             string groupBy = "CategoryName asc, ";
             sidx = sidx.Replace(groupBy"");
 
             // get the index where to start retrieving records from
             // 0 = starts from the beggining, 10 means skip the first 10 records and start from record 11
             int startRowIndex = ((page * rows) - rows);
 
             // get records
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsidx + " " + sord);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects a record by primary key(s)
         /// </summary>
         /// <param name="id">ProductID</param>
         /// <returns>Returns one Products record</returns>
         [HttpGet]
         public async Task<ProductsSelectByPrimaryKey(int id)
         {
             Products objProducts = await Products.SelectByPrimaryKeyAsync(id);
             return objProducts;
         }
 
         /// <summary>
         /// Gets the total number of records in the Products table
         /// </summary>
         /// <returns>Returns the Total number of records in the Products table</returns>
         [HttpGet]
         public async Task<intGetRecordCount()
         {
             return await Products.GetRecordCountAsync();
         }
 
         /// <summary>
         /// Gets the total number of records in the Products table by SupplierID
         /// </summary>
         /// <param name="id">supplierID</param>
         /// <returns>Returns the Total number of records in the Products table by supplierID</returns>
         [HttpGet]
         public async Task<intGetRecordCountBySupplierID(intid)
         {
             return await Products.GetRecordCountBySupplierIDAsync(id);
         }
 
         /// <summary>
         /// Gets the total number of records in the Products table by CategoryID
         /// </summary>
         /// <param name="id">categoryID</param>
         /// <returns>Returns the Total number of records in the Products table by categoryID</returns>
         [HttpGet]
         public async Task<intGetRecordCountByCategoryID(intid)
         {
             return await Products.GetRecordCountByCategoryIDAsync(id);
         }
 
         /// <summary>
         /// Gets the total number of records in the Products table based on search parameters
         /// </summary>
         /// <param name="productID">ProductID</param>
         /// <param name="productName">ProductName</param>
         /// <param name="supplierID">SupplierID</param>
         /// <param name="categoryID">CategoryID</param>
         /// <param name="quantityPerUnit">QuantityPerUnit</param>
         /// <param name="unitPrice">UnitPrice</param>
         /// <param name="unitsInStock">UnitsInStock</param>
         /// <param name="unitsOnOrder">UnitsOnOrder</param>
         /// <param name="reorderLevel">ReorderLevel</param>
         /// <param name="discontinued">Discontinued</param>
         /// <returns>Returns the Total number of records in the Products table based on the search parameters</returns>
         [HttpGet]
         public Task<intGetRecordCountDynamicWhere(intproductIDstring productNameintsupplierIDintcategoryIDstring quantityPerUnitdecimalunitPriceInt16unitsInStockInt16unitsOnOrderInt16reorderLevelbooldiscontinued)
         {
             return Products.GetRecordCountDynamicWhereAsync(productIDproductNamesupplierIDcategoryIDquantityPerUnitunitPriceunitsInStockunitsOnOrderreorderLeveldiscontinued);
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression.
         /// </summary>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <param name="startRowIndex">Zero-based.  Row index where to start taking rows from</param>
         /// <param name="sortByExpression">Field to sort and sort direction.  E.g. "FieldName asc" or "FieldName desc"</param>
         /// <returns>Returns Products (List of) collection.</returns>
         public async Task<List<Products>> SelectSkipAndTake(int rowsint startRowIndexstring sortByExpression)
         {
             sortByExpression = GetSortExpression(sortByExpression);
             return await Products.SelectSkipAndTakeAsync(rowsstartRowIndexsortByExpression);
         }
 
         /// <summary>
         /// Selects records by SupplierID as a collection (List) of Products sorted by the sortByExpression starting from the startRowIndex
         /// </summary>
         /// <param name="supplierID">Supplier ID</param>
         /// <param name="sidx">Column to sort</param>
         /// <param name="sord">Sort direction</param>
         /// <param name="page">Page of the grid to show</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeBySupplierID(intsupplierIDstring sidxstring sordint pageint rows)
         {
             string sortByExpression = GetSortExpression(sidx + " " + sord);
             int startRowIndex = ((page * rows) - rows);
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeBySupplierIDAsync(rowsstartRowIndexsortByExpressionsupplierID);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records by CategoryID as a collection (List) of Products sorted by the sortByExpression starting from the startRowIndex
         /// </summary>
         /// <param name="categoryID">Category ID</param>
         /// <param name="sidx">Column to sort</param>
         /// <param name="sord">Sort direction</param>
         /// <param name="page">Page of the grid to show</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeByCategoryID(intcategoryIDstring sidxstring sordint pageint rows)
         {
             string sortByExpression = GetSortExpression(sidx + " " + sord);
             int startRowIndex = ((page * rows) - rows);
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeByCategoryIDAsync(rowsstartRowIndexsortByExpressioncategoryID);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records as a collection (List) of Products sorted by the sortByExpression starting from the startRowIndex, based on the search parameters
         /// </summary>
         /// <param name="productID">ProductID</param>
         /// <param name="productName">ProductName</param>
         /// <param name="supplierID">SupplierID</param>
         /// <param name="categoryID">CategoryID</param>
         /// <param name="quantityPerUnit">QuantityPerUnit</param>
         /// <param name="unitPrice">UnitPrice</param>
         /// <param name="unitsInStock">UnitsInStock</param>
         /// <param name="unitsOnOrder">UnitsOnOrder</param>
         /// <param name="reorderLevel">ReorderLevel</param>
         /// <param name="discontinued">Discontinued</param>
         /// <param name="rows">Number of rows to retrieve</param>
         /// <param name="startRowIndex">Zero-based.  Row index where to start taking rows from</param>
         /// <param name="sortByExpression">Field to sort and sort direction.  E.g. "FieldName asc" or "FieldName desc"</param>
         /// <param name="page">Page of the grid to show</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectSkipAndTakeDynamicWhere(intproductIDstring productNameintsupplierIDintcategoryIDstring quantityPerUnitdecimalunitPriceInt16unitsInStockInt16unitsOnOrderInt16reorderLevelbooldiscontinuedint rowsint startRowIndexstring sortByExpression)
         {
             sortByExpression = GetSortExpression(sortByExpression);
             List<ProductsobjProductsCol = await Products.SelectSkipAndTakeDynamicWhereAsync(productIDproductNamesupplierIDcategoryIDquantityPerUnitunitPriceunitsInStockunitsOnOrderreorderLeveldiscontinuedrowsstartRowIndexsortByExpression);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects all records as a collection (List) of Products sorted by the sort expression.  Data returned is not sorted when sortByExpression is not passed, null, or empty.
         /// </summary>
         /// <param name="sortByExpression">Field to sort and sort direction.  E.g. "FieldName asc" or "FieldName desc".  sortByExpression is not required.</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectAll(string sortByExpression = null)
         {
             List<ProductsobjProductsCol;
 
             if (String.IsNullOrEmpty(sortByExpression))
             {
                 objProductsCol = await Products.SelectAllAsync();
             }
             else
             {
                 sortByExpression = GetSortExpression(sortByExpression);
                 objProductsCol = await Products.SelectAllAsync(sortByExpression);
             }
 
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects records based on the passed filters as a collection (List) of Products.
         /// </summary>
         /// <param name="productID">ProductID</param>
         /// <param name="productName">ProductName</param>
         /// <param name="supplierID">SupplierID</param>
         /// <param name="categoryID">CategoryID</param>
         /// <param name="quantityPerUnit">QuantityPerUnit</param>
         /// <param name="unitPrice">UnitPrice</param>
         /// <param name="unitsInStock">UnitsInStock</param>
         /// <param name="unitsOnOrder">UnitsOnOrder</param>
         /// <param name="reorderLevel">ReorderLevel</param>
         /// <param name="discontinued">Discontinued</param>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectAllDynamicWhere(intproductIDstring productNameintsupplierIDintcategoryIDstring quantityPerUnitdecimalunitPriceInt16unitsInStockInt16unitsOnOrderInt16reorderLevelbooldiscontinued)
         {
             List<ProductsobjProductsCol = await Products.SelectAllDynamicWhereAsync(productIDproductNamesupplierIDcategoryIDquantityPerUnitunitPriceunitsInStockunitsOnOrderreorderLeveldiscontinued);
             return objProductsCol;
         }
 
         /// <summary>
         /// Selects ProductID and ProductName columns for use with a DropDownList web control, ComboBox, CheckedBoxList, ListView, ListBox, etc
         /// </summary>
         /// <returns>Returns a collection (List) of Products</returns>
         [HttpGet]
         public async Task<List<Products>> SelectProductsDropDownListData()
         {
             List<ProductsobjProductsCol = await Products.SelectProductsDropDownListDataAsync();
             return objProductsCol;
         }
 
         /// <summary>
         /// Gets the default sort expression when no sort expression is provided
         /// </summary>
         private string GetSortExpression(string sortByExpression)
         {
             // when no sort expression is provided, ProductID is set as the default in ascending order
             // for ascending order, "asc" is not needed, so it is removed
             if (String.IsNullOrEmpty(sortByExpression) || sortByExpression == " asc")
                 sortByExpression = "ProductID";
             else if (sortByExpression.Contains(" asc"))
                 sortByExpression = sortByExpression.Replace(" asc""");
 
             return sortByExpression;
         }
 
         /// <summary>
         /// Used when adding a new record or updating an existing record
         /// </summary>
         /// <param name="model">ProductsModel</param>
         /// <param name="operation">Operation to Add a new record or Update an existing record</param>
         /// <param name="isForListInlineOrListCrud">Used by the razor Views with ListInline or ListCrud when true</param>
         /// <returns>Task of IActionResult</returns>
         private async Task<IActionResultAddEditProductsAsync(ProductsModel modelCrudOperation operationbool isForListInlineOrListCrud = false)
         {
             try
             {
                 Products objProducts;
 
                 // create a new instance of the Products when adding a new record
                 // or, retrieve the record that needs to be updated
                 if (operation == CrudOperation.Add)
                    objProducts = new Products();
                 else
                    objProducts = await Products.SelectByPrimaryKeyAsync(model.ProductID);
 
                 // assign values to the Products instance
                 objProducts.ProductID = model.ProductID;
                 objProducts.ProductName = model.ProductName;
                 objProducts.SupplierID = model.SupplierID;
                 objProducts.CategoryID = model.CategoryID;
                 objProducts.QuantityPerUnit = model.QuantityPerUnit;
                 objProducts.UnitPrice = model.UnitPrice;
                 objProducts.Discontinued = model.Discontinued;
 
                 if (isForListInlineOrListCrud)
                 {
                     objProducts.UnitsInStock = model.UnitsInStock;
                     objProducts.UnitsOnOrder = model.UnitsOnOrder;
                     objProducts.ReorderLevel = model.ReorderLevel;
                 }
                 else
                 {
                     if(!String.IsNullOrEmpty(model.UnitsInStockHidden))
                        objProducts.UnitsInStock = Convert.ToInt16(model.UnitsInStockHidden);
                     else
                        objProducts.UnitsInStock = null;
 
                     if(!String.IsNullOrEmpty(model.UnitsOnOrderHidden))
                        objProducts.UnitsOnOrder = Convert.ToInt16(model.UnitsOnOrderHidden);
                     else
                        objProducts.UnitsOnOrder = null;
 
                     if(!String.IsNullOrEmpty(model.ReorderLevelHidden))
                        objProducts.ReorderLevel = Convert.ToInt16(model.ReorderLevelHidden);
                     else
                        objProducts.ReorderLevel = null;
 
                 }
 
                 // save the new record, or the updated values of the current record
                 if (operation == CrudOperation.Add)
                    await objProducts.InsertAsync();
                 else
                    await objProducts.UpdateAsync();
 
                 // everthing went well
                 return Ok();
             }
             catch (Exception ex)
             {
                 // something went wrong
                 return BadRequest("Error Message: " + ex.Message + " Stack Trace: " + ex.StackTrace);
             }
         }
     }
}