|
| 1 | +/** |
| 2 | + * Manage |entityPlural| |
| 3 | + * It will be your responsibility to fine tune this template, add validations, try/catch blocks, logging, etc. |
| 4 | + */ |
| 5 | +class extends="coldbox.system.EventHandler"{ |
| 6 | + |
| 7 | + // DI Virtual Entity Service |
| 8 | + property name="ORMService" inject="entityService:|entity|"; |
| 9 | + |
| 10 | + // HTTP Method Security |
| 11 | + this.allowedMethods = { |
| 12 | + index = "GET", |
| 13 | + new = "GET", |
| 14 | + edit = "GET", |
| 15 | + delete = "POST,DELETE", |
| 16 | + save = "POST,PUT" |
| 17 | + }; |
| 18 | + |
| 19 | + /** |
| 20 | + * Param incoming format, defaults to `html` |
| 21 | + */ |
| 22 | + function preHandler( event, rc, prc ){ |
| 23 | + event.paramValue( "format", "html" ); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Listing |
| 28 | + */ |
| 29 | + function index( event, rc, prc ){ |
| 30 | + // Get all |entityPlural| |
| 31 | + prc.|entityPlural| = ORMService.getAll(); |
| 32 | + // Multi-format rendering |
| 33 | + event.renderData( data=prc.|entityPlural|, formats="xml,json,html,pdf" ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * New Form |
| 38 | + */ |
| 39 | + function new( event, rc, prc ){ |
| 40 | + // get new |entity| |
| 41 | + prc.|entity| = ORMService.new(); |
| 42 | + |
| 43 | + event.setView( "|entityPlural|/new" ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Edit Form |
| 48 | + */ |
| 49 | + function edit( event, rc, prc ){ |
| 50 | + // get persisted |entity| |
| 51 | + prc.|entity| = ORMService.get( rc.|pk| ); |
| 52 | + |
| 53 | + event.setView( "|entityPlural|/edit" ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * View |entity| mostly used for RESTful services only. |
| 58 | + */ |
| 59 | + function show( event, rc, prc ){ |
| 60 | + // Default rendering. |
| 61 | + event.paramValue( "format", "json" ); |
| 62 | + // Get requested entity by id |
| 63 | + prc.|entity| = ORMService.get( rc.|pk| ); |
| 64 | + // Multi-format rendering |
| 65 | + event.renderData( data=prc.|entity|, formats="xml,json" ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Save and Update |
| 70 | + */ |
| 71 | + function save( event, rc, prc ){ |
| 72 | + // get |entity| to persist or update and populate it with incoming form |
| 73 | + prc.|entity| = populateModel( |
| 74 | + model = ORMService.get( rc.|pk| ), |
| 75 | + exclude = "|pk|", |
| 76 | + composeRelationships = true |
| 77 | + ); |
| 78 | + |
| 79 | + // Do your validations here |
| 80 | + |
| 81 | + // Save it |
| 82 | + ORMService.save( prc.|entity| ); |
| 83 | + |
| 84 | + // RESTful Handler |
| 85 | + switch(rc.format){ |
| 86 | + // xml,json,jsont are by default. Add your own or remove |
| 87 | + case "xml" : case "json" : case "jsont" :{ |
| 88 | + event.renderData( data=prc.|entity|, type=rc.format, location="/|entityPlural|/show/#prc.|entity|.get|pk|()#" ); |
| 89 | + break; |
| 90 | + } |
| 91 | + // HTML |
| 92 | + default:{ |
| 93 | + // Show a nice notice |
| 94 | + flash.put( "notice", { message="|entity| Created", type="success" } ); |
| 95 | + // Redirect to listing: change to `setNextEvent()` if using ColdBox <5 |
| 96 | + relocate( '|entityPlural|' ); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Delete |
| 103 | + */ |
| 104 | + function delete( event, rc, prc ){ |
| 105 | + // Delete record by ID |
| 106 | + var removed = ORMService.delete( ORMService.get( rc.|pk| ) ); |
| 107 | + |
| 108 | + // RESTful Handler |
| 109 | + switch( rc.format ){ |
| 110 | + // xml,json,jsont are by default. Add your own or remove |
| 111 | + case "xml" : case "json" : case "jsont" :{ |
| 112 | + var restData = { "deleted" = removed }; |
| 113 | + event.renderData( data=restData, type=rc.format ); |
| 114 | + break; |
| 115 | + } |
| 116 | + // HTML |
| 117 | + default:{ |
| 118 | + // Show a nice notice |
| 119 | + flash.put( "notice", { message="|entity| Poofed!", type="success" } ); |
| 120 | + // Redirect to listing: change to `setNextEvent()` if using ColdBox <5 |
| 121 | + relocate( '|entityPlural|' ); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments