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<IActionResult> Insert([FromBody]ProductsModel model, bool isForListInlineOrListCrud = false) { // add a new record return await AddEditProductsAsync(model, CrudOperation.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<IActionResult> Update([FromBody]ProductsModel model, bool isForListInlineOrListCrud = false) { // update existing record return await AddEditProductsAsync(model, CrudOperation.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<IActionResult> Delete(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<IActionResult> DeleteMultiple(string ids) { try { // split ids into a List List<Int32> productIDList = 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 sidx, string sord, int page, int 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<Products> objProductsCol = await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sidx + " " + 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 _search, string nd, int rows, int page, string sidx, string sord, string filters = "") { int? productID = null; string productName = String.Empty; int? supplierID = null; int? categoryID = null; string quantityPerUnit = String.Empty; decimal? unitPrice = null; Int16? unitsInStock = null; Int16? unitsOnOrder = null; Int16? reorderLevel = null; bool? discontinued = null; if (!String.IsNullOrEmpty(filters)) { // deserialize filters and get values being searched var jsonResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(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<Products> objProductsCol = await Products.SelectSkipAndTakeDynamicWhereAsync(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued, rows, startRowIndex, sidx + " " + 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 sidx, string sord, int page, int 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<Products> objProductsCol = await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sidx + " " + 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 sidx, string sord, int page, int 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<Products> objProductsCol = await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sidx + " " + 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 sidx, string sord, int page, int 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<Products> objProductsCol = await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sidx + " " + 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 sidx, string sord, int page, int 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<Products> objProductsCol = await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sidx + " " + 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 sidx, string sord, int page, int 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<Products> objProductsCol = await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sidx + " " + 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<Products> SelectByPrimaryKey(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<int> GetRecordCount() { 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<int> GetRecordCountBySupplierID(int? id) { 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<int> GetRecordCountByCategoryID(int? id) { 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<int> GetRecordCountDynamicWhere(int? productID, string productName, int? supplierID, int? categoryID, string quantityPerUnit, decimal? unitPrice, Int16? unitsInStock, Int16? unitsOnOrder, Int16? reorderLevel, bool? discontinued) { return Products.GetRecordCountDynamicWhereAsync(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued); } /// <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 rows, int startRowIndex, string sortByExpression) { sortByExpression = GetSortExpression(sortByExpression); return await Products.SelectSkipAndTakeAsync(rows, startRowIndex, sortByExpression); } /// <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(int? supplierID, string sidx, string sord, int page, int rows) { string sortByExpression = GetSortExpression(sidx + " " + sord); int startRowIndex = ((page * rows) - rows); List<Products> objProductsCol = await Products.SelectSkipAndTakeBySupplierIDAsync(rows, startRowIndex, sortByExpression, supplierID); 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(int? categoryID, string sidx, string sord, int page, int rows) { string sortByExpression = GetSortExpression(sidx + " " + sord); int startRowIndex = ((page * rows) - rows); List<Products> objProductsCol = await Products.SelectSkipAndTakeByCategoryIDAsync(rows, startRowIndex, sortByExpression, categoryID); 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(int? productID, string productName, int? supplierID, int? categoryID, string quantityPerUnit, decimal? unitPrice, Int16? unitsInStock, Int16? unitsOnOrder, Int16? reorderLevel, bool? discontinued, int rows, int startRowIndex, string sortByExpression) { sortByExpression = GetSortExpression(sortByExpression); List<Products> objProductsCol = await Products.SelectSkipAndTakeDynamicWhereAsync(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued, rows, startRowIndex, sortByExpression); 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<Products> objProductsCol; 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(int? productID, string productName, int? supplierID, int? categoryID, string quantityPerUnit, decimal? unitPrice, Int16? unitsInStock, Int16? unitsOnOrder, Int16? reorderLevel, bool? discontinued) { List<Products> objProductsCol = await Products.SelectAllDynamicWhereAsync(productID, productName, supplierID, categoryID, quantityPerUnit, unitPrice, unitsInStock, unitsOnOrder, reorderLevel, discontinued); 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<Products> objProductsCol = 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<IActionResult> AddEditProductsAsync(ProductsModel model, CrudOperation operation, bool 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); } } } }