/SampleApplication/ColdBox3/handlers/artist.cfc

http://github.com/bobsilverberg/ValidateThisColdBoxPlugin · ColdFusion CFScript · 98 lines · 43 code · 20 blank · 35 comment · 1 complexity · ba46379217d363cec8aeb8d8eaf8edf5 MD5 · raw file

  1. /**
  2. * the artist handler
  3. */
  4. component extends="model.abstract.AbstractHandler"
  5. {
  6. /*
  7. -------------------------------------------------------------------------
  8. dependancy injection
  9. -------------------------------------------------------------------------
  10. */
  11. property name="ArtistService" type="model:ArtistService" scope="instance";
  12. /*
  13. -------------------------------------------------------------------------
  14. public handlers
  15. -------------------------------------------------------------------------
  16. */
  17. /**
  18. * I am the default event
  19. */
  20. void function index( event )
  21. {
  22. var rc = event.getCollection();
  23. // get all artists as an array
  24. rc.artists = instance.ArtistService.getArtists();
  25. // set view
  26. arguments.Event.setView( "artist/index" );
  27. }
  28. /**
  29. * I delete artists
  30. */
  31. void function delete( event )
  32. {
  33. var rc = event.getCollection();
  34. // delete the artist
  35. instance.ArtistService.deleteArtistById( rc.artistid );
  36. // save message to flash memory
  37. flash.put( "message", "Artist deleted" );
  38. // relocate
  39. setNextEvent( "artist.index" );
  40. }
  41. /**
  42. * I edit/add artists
  43. */
  44. void function maintain( event )
  45. {
  46. var rc = arguments.Event.getCollection();
  47. // get artist by id. returns a new artist if id not found
  48. arguments.event.paramValue( "artistid", "" );
  49. rc.Artist = instance.ArtistService.getArtistById( rc.artistid );
  50. // need an empty validation result object for display
  51. rc.ValidationResult = instance.ArtistService.newValidationResult();
  52. // get required fields
  53. rc.RequiredFields = instance.ArtistService.getArtistRequiredFields();
  54. // set view
  55. arguments.Event.setView( "artist/form" );
  56. }
  57. /**
  58. * I save an artist
  59. */
  60. void function save( event )
  61. {
  62. var rc = arguments.Event.getCollection();
  63. // note: using entityid instead of artistid so that populate() doesn't set the id to blank
  64. var rc.Artist = instance.ArtistService.getArtistById( rc.entityid );
  65. var SaveResult = instance.ArtistService.save( rc.Artist );
  66. if ( SaveResult.getIsSuccess() )
  67. {
  68. flash.put( "message", "Artist saved" );
  69. setNextEvent( "artist.index" );
  70. }
  71. else
  72. {
  73. rc.ValidationResult = SaveResult;
  74. rc.RequiredFields = instance.ArtistService.getArtistRequiredFields();
  75. arguments.Event.setView( "artist/form" );
  76. }
  77. }
  78. }