Example Number 5.13

Example Name: ProductsAdmin.cfc

Language: CFML

Version: 1.0.0

Code:

<cfcomponent displayname="Administer Products" 
 hint="Add, update, delete, and search Northwind product list">

<!--- Search the products table in Northwind --->
  <cffunction name="getSearchResult" access="remote" 
   returnType="query" hint="Pass a search string to get a list of products, 
   or nothing to get all products">
    <cfargument name="search" type="string" default="">
    <cftry>
      <cfquery name="rsGetProducts" datasource="Northwind">
        SELECT ProductID, ProductName, UnitPrice, 
    QuantityPerUnit, CategoryID, SupplierID 
    FROM Products
<!--- If no argument is passed, all records returned--->
       <cfif search NEQ "">
        WHERE ProductName LIKE '%#search#%'
       </cfif> 
      </cfquery>
      <cfcatch type="Any">
        <cfthrow message="There was a database error">
      </cfcatch>
    </cftry> 
    <cfreturn rsGetProducts />
  </cffunction>

<!--- Add a product to the Northwind Products table --->
  <cffunction name="addProduct" returntype="string" 
   access="remote" hint="Pass a record to add a product">
    <cfargument name="ProductName" type="string" required="true" />
    <cfargument name="UnitPrice" type="numeric" default=0 />
    <cfargument name="QuantityPerUnit" type="string" default="0" />
    <cfargument name="CategoryID" type="numeric" default=0 />
    <cfargument name="SupplierID" type="numeric" default=0 />
    <cftry>
      <cfquery name="rsSuppliers" datasource="Northwind">
       INSERT INTO Products 
       (ProductName
        , UnitPrice
        , QuantityPerUnit
        , CategoryID
        , SupplierID) 
       VALUES 
       ('#ProductName#'
       , #UnitPrice#
       , '#QuantityPerUnit#'
       , #CategoryID#
       , #SupplierID#)
      </cfquery>
      <cfcatch type="Any">
        <cfthrow message="There was a database error">
      </cfcatch>
    </cftry>  
    <cfreturn "Record inserted" />
  </cffunction>
<!--- Update a product using a product record --->
  <cffunction name="updateProduct" 
   returntype="string" 
   access="remote"
   hint="Pass a record including the ProductID to update a product">
    <cfargument name="ProductName" type="string" required="true" />
    <cfargument name="UnitPrice" type="numeric" default=0 />
    <cfargument name="QuantityPerUnit" type="string" default="0" />
    <cfargument name="CategoryID" type="numeric" default=0 />
    <cfargument name="SupplierID" type="numeric" default=0 />
    <cfargument name="ProductID" type="numeric" required="true" />
    <cftry>
      <cfquery name="rsSuppliers" datasource="Northwind">
       UPDATE Products 
       SET ProductName='#ProductName#'
         , UnitPrice=#UnitPrice#
         , QuantityPerUnit='#QuantityPerUnit#'
         , CategoryID=#CategoryID#
         , SupplierID=#SupplierID#
       WHERE ProductID = #ProductID#
      </cfquery>
      <cfcatch type="Any">
        <cfthrow message="There was a database error">
      </cfcatch>
    </cftry>
    <cfreturn "Record updated" />
  </cffunction>

<!--- Delete products from a list of productids --->
  <cffunction name="deleteProducts" returntype="string" access="remote" 
   hint="Pass a ProductID or comma-separated list 
   of ProductIDs to delete records">
    <cfargument name="productids" type="string" default="0" />
    <cftry>
      <cfquery name="rsSuppliers" datasource="Northwind">
<!--- The following query will delete products. 
      The alternate query will merely set the Discontinued field to 1
       DELETE FROM Products WHERE ProductID IN (#productids#)--->
       UPDATE Products SET Discontinued = 1 WHERE ProductID in (#productids#)
      </cfquery>
      <cfcatch type="Any">
        <cfthrow message="There was a database error">
      </cfcatch>
    </cftry>  
    <cfreturn "Record deleted" />
  </cffunction>
 
<!--- Get a list of suppliers to feed a dropdown box --->
  <cffunction name="getSuppliers" returntype="query" 
   access="remote" hint="Get a list of all suppliers">
    <cftry>  
      <cfquery name="rsSuppliers" datasource="Northwind"
       cachedwithin=#CreateTimespan(7,0,0,0)#>
       SELECT SupplierID, CompanyName FROM Suppliers
      </cfquery>
      <cfcatch type="Any">
        <cfthrow message="There was a database error">
      </cfcatch>
    </cftry>  
    <cfreturn rsSuppliers />
  </cffunction>
     
<!--- Get a list of categories to feed a dropdown box --->
  <cffunction name="getCategories" returntype="query" 
   access="remote" hint="Get a list of product categories">
    <cftry>
      <cfquery name="rsCategories" datasource="Northwind"
       cachedwithin=#CreateTimespan(7,0,0,0)#>
       SELECT CategoryID, CategoryName FROM Categories
      </cfquery>  
      <cfcatch type="Any">
        <cfthrow message="There was a database error">
      </cfcatch>
    </cftry>
    <cfreturn rsCategories />
  </cffunction>

</cfcomponent>

Download code text

Download chapter example files