Advertisement
MelonLupi

controller

Sep 9th, 2019
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.22 KB | None | 0 0
  1. package intern__grails
  2.  
  3. import static org.springframework.http.HttpStatus.*
  4. import grails.transaction.Transactional
  5. import grails.converters.*
  6.  
  7. @Transactional(readOnly = true)
  8. class DriverController {
  9.  
  10.     static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
  11.  
  12.     def index(Integer max) {
  13.         params.max = Math.min(max ?: 10, 100)
  14.         respond Driver.list(params), model:[driverCount: Driver.count()]
  15.     }
  16.  
  17.     def show(Driver driver) {
  18.         respond driver
  19.     }
  20.  
  21.     def create() {
  22.         respond new Driver(params)
  23.     }
  24.  
  25.     @Transactional
  26.     def save(Driver driver) {
  27.         if (driver == null) {
  28.             transactionStatus.setRollbackOnly()
  29.             notFound()
  30.             return
  31.         }
  32.  
  33.         if (driver.hasErrors()) {
  34.             transactionStatus.setRollbackOnly()
  35.             respond driver.errors, view:'create'
  36.             return
  37.         }
  38.  
  39.         driver.save flush:true
  40.  
  41.         request.withFormat {
  42.             form multipartForm {
  43.                 flash.message = message(code: 'default.created.message', args: [message(code: 'driver.label', default: 'Driver'), driver.id])
  44.                 redirect driver
  45.             }
  46.             '*' { respond driver, [status: CREATED] }
  47.         }
  48.     }
  49.  
  50.     def edit(Driver driver) {
  51.         respond driver
  52.     }
  53.  
  54.     @Transactional
  55.     def update(Driver driver) {
  56.         if (driver == null) {
  57.             transactionStatus.setRollbackOnly()
  58.             notFound()
  59.             return
  60.         }
  61.  
  62.         if (driver.hasErrors()) {
  63.             transactionStatus.setRollbackOnly()
  64.             respond driver.errors, view:'edit'
  65.             return
  66.         }
  67.  
  68.         driver.save flush:true
  69.  
  70.         request.withFormat {
  71.             form multipartForm {
  72.                 flash.message = message(code: 'default.updated.message', args: [message(code: 'driver.label', default: 'Driver'), driver.id])
  73.                 redirect driver
  74.             }
  75.             '*'{ respond driver, [status: OK] }
  76.         }
  77.     }
  78.  
  79.     @Transactional
  80.     def delete(Driver driver) {
  81.  
  82.         if (driver == null) {
  83.             transactionStatus.setRollbackOnly()
  84.             notFound()
  85.             return
  86.         }
  87.  
  88.         driver.delete flush:true
  89.  
  90.         request.withFormat {
  91.             form multipartForm {
  92.                 flash.message = message(code: 'default.deleted.message', args: [message(code: 'driver.label', default: 'Driver'), driver.id])
  93.                 redirect action:"index", method:"GET"
  94.             }
  95.             '*'{ render status: NO_CONTENT }
  96.         }
  97.     }
  98.  
  99.     def testFunc(){
  100.         print("tes")
  101.     }
  102.  
  103.     def list(Integer max){
  104. //        render Driver.list() as JSON
  105.         def drivers = Driver.withCriteria{
  106.             maxResults params.int( 'max' ) ?: 10
  107.         }
  108.  
  109.         render drivers as JSON
  110.  
  111.     }
  112.  
  113.     protected void notFound() {
  114.         request.withFormat {
  115.             form multipartForm {
  116.                 flash.message = message(code: 'default.not.found.message', args: [message(code: 'driver.label', default: 'Driver'), params.id])
  117.                 redirect action: "index", method: "GET"
  118.             }
  119.             '*'{ render status: NOT_FOUND }
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement