<!--- 
 Filename: FilmDataCFC1.cfc
 Author: Nate Weiss (NMW)
 Purpose: Creates the FilmDataCFC1 ColdFusion Component, which provides
 search and data retrieval services for films in the ows database.
--->

<!--- The <CFCOMPONENT> block defines the CFC --->
<!--- The filename of this file determines the CFC's name --->
<cfcomponent 
 hint="Provides a simple interface for searching for and getting detailed 
 information about films in the Orange Whip Studios database."
 output="false">

  <!--- ListFilms() method --->
  <cffunction name="listFilms" returnType="query"
 hint="Returns a query object containing film information." output="false">
 
    <!--- Optional SearchString argument ---> 
    <cfargument name="searchString" type="string" required="No"
    hint="Optional search criteria; if not given, all films are returned.">
 
    <!--- Optional SearchString argument ---> 
    <cfargument name="actorID" type="numeric" required="No"
    hint="Allows searching for films by actor.">
 
    <!--- var scope variables --->
    <cfset var getFilms = "">
    
    <!--- Run the query ---> 
    <cfquery name="getFilms" datasource="ows">
     SELECT FilmID, MovieTitle FROM Films
     WHERE 0=0
     <!--- If a search string has been specified --->
     <cfif isDefined("ARGUMENTS.searchString")>
     AND (MovieTitle LIKE '%#ARGUMENTS.searchString#%'
     OR Summary LIKE '%#ARGUMENTS.searchString#%')
     </cfif>
 
     <!--- If an actor's name has been specified --->
     <cfif isDefined("ARGUMENTS.actorID")>
     AND FilmID IN
     (SELECT FilmID FROM FilmsActors
     WHERE ActorID = #ARGUMENTS.actorID#)
     </cfif>
     ORDER BY MovieTitle
    </cfquery>
 
    <!--- Return the query results ---> 
    <cfreturn getFilms>
  </cffunction>
 
 
  <!--- GetFilmData() method --->
  <cffunction name="getFilmData" returnType="struct" output="false"
  hint="Returns structured information about the specified film.">

    <!--- FilmID argument --->
    <cfargument name="filmID" type="numeric" required="Yes"
    hint="The film that you want information about.">

    <!--- This is what this method originally returns ---> 
    <!--- The var keyword makes it local to the method --->
    <cfset var filmData = structNew()>
    <cfset var getFilm = "">
    <cfset var getActors = "">
    
    <cfset filmData.filmID = ARGUMENTS.filmID>
 
    <!--- Select data about the film from the database --->
    <cfquery name="getFilm" datasource="ows">
    SELECT MovieTitle, Summary FROM Films
    WHERE FilmID = #ARGUMENTS.filmID#
    </cfquery>
 
    <!--- Populate the FilmData structure with film info --->
    <cfset filmData.movieTitle = getFilm.MovieTitle>
    <cfset filmData.summary = getFilm.Summary>
 
    <!--- Run second query to get actor information --->
    <cfquery name="getActors" datasource="ows">
    SELECT ActorID, NameFirst, NameLast 
    FROM Actors
    WHERE ActorID IN
    (SELECT ActorID FROM FilmsActors
    WHERE FilmID = #ARGUMENTS.filmID#)
    </cfquery>
 
    <!--- Make the GetActors query results part of the returned structure --->
    <cfset filmData.actorsQuery = getActors>

    <!--- Return the final structure ---> 
    <cfreturn FilmData>
 </cffunction>

</cfcomponent>
