Skip to content

Commit

Permalink
Enhance RestController to support multiple datasources (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
saba-mo authored Sep 13, 2024
1 parent 53d7240 commit ad09233
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 66 deletions.
138 changes: 74 additions & 64 deletions grails-app/controllers/io/xh/hoist/RestController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

package io.xh.hoist

import grails.gorm.transactions.Transactional
import grails.validation.ValidationException
import io.xh.hoist.json.JSONParser

@Transactional
abstract class RestController extends BaseController {

def trackService
Expand All @@ -20,92 +18,104 @@ abstract class RestController extends BaseController {
static restTarget = null // Implementations set to value of GORM domain class they are editing.

def create() {
def data = parseRequestJSON().data
preprocessSubmit(data)
restTargetVal.withTransaction {
def data = parseRequestJSON().data
preprocessSubmit(data)

def obj = restTargetVal.newInstance(data)
doCreate(obj, data)
noteChange(obj, 'CREATE')
renderJSON(success:true, data:obj)
def obj = restTargetVal.newInstance(data)
doCreate(obj, data)
noteChange(obj, 'CREATE')
renderJSON(success:true, data:obj)
}
}

def read() {
def query = params.query ? JSONParser.parseObject(params.query) : [:],
ret = params.id ? [restTargetVal.get(params.id)] : doList(query)
renderJSON(success:true, data:ret)
restTargetVal.withTransaction {
def query = params.query ? JSONParser.parseObject(params.query) : [:],
ret = params.id ? [restTargetVal.get(params.id)] : doList(query)
renderJSON(success:true, data:ret)
}
}

def update() {
def data = parseRequestJSON().data
preprocessSubmit(data)
restTargetVal.withTransaction {
def data = parseRequestJSON().data
preprocessSubmit(data)

def obj = restTargetVal.get(data.id)
try {
doUpdate(obj, data)
noteChange(obj, 'UPDATE')
renderJSON(success:true, data:obj)
} catch (ValidationException ex) {
obj.discard()
throw ex
def obj = restTargetVal.get(data.id)
try {
doUpdate(obj, data)
noteChange(obj, 'UPDATE')
renderJSON(success:true, data:obj)
} catch (ValidationException ex) {
obj.discard()
throw ex
}
}
}

def bulkUpdate() {
def body = parseRequestJSON(),
ids = body.ids,
newParams = body.newParams,
successCount = 0,
failCount = 0,
target = restTargetVal,
obj = null

ids.each { id ->
try {
obj = target.get(id)
doUpdate(obj, newParams)
noteChange(obj, 'UPDATE')
successCount++
} catch (Exception e) {
failCount++
if (e instanceof ValidationException) {
e = new io.xh.hoist.exception.ValidationException(e)
logDebug("Validation exception updating ${obj}", e)
} else {
logError("Unexpected exception updating ${obj}", e)
restTargetVal.withTransaction {
def body = parseRequestJSON(),
ids = body.ids,
newParams = body.newParams,
successCount = 0,
failCount = 0,
target = restTargetVal,
obj = null

ids.each { id ->
try {
obj = target.get(id)
doUpdate(obj, newParams)
noteChange(obj, 'UPDATE')
successCount++
} catch (Exception e) {
failCount++
if (e instanceof ValidationException) {
e = new io.xh.hoist.exception.ValidationException(e)
logDebug("Validation exception updating ${obj}", e)
} else {
logError("Unexpected exception updating ${obj}", e)
}
}
}
}

renderJSON(success:successCount, fail:failCount)
renderJSON(success:successCount, fail:failCount)
}
}

def delete() {
def obj = restTargetVal.get(params.id)
doDelete(obj)
noteChange(obj, 'DELETE')
renderJSON(success:true)
restTargetVal.withTransaction {
def obj = restTargetVal.get(params.id)
doDelete(obj)
noteChange(obj, 'DELETE')
renderJSON(success:true)
}
}

def bulkDelete() {
def ids = params.list('ids'),
successCount = 0,
failCount = 0,
target = restTargetVal

ids.each {id ->
try {
target.withTransaction{ status ->
def obj = target.get(id)
doDelete(obj)
noteChange(obj, 'DELETE')
successCount++
restTargetVal.withTransaction {
def ids = params.list('ids'),
successCount = 0,
failCount = 0,
target = restTargetVal

ids.each {id ->
try {
target.withTransaction{ status ->
def obj = target.get(id)
doDelete(obj)
noteChange(obj, 'DELETE')
successCount++
}
} catch (Exception ignored) {
failCount++
}
} catch (Exception ignored) {
failCount++
}
}

renderJSON(success:successCount, fail:failCount)
renderJSON(success:successCount, fail:failCount)
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

package io.xh.hoist.admin

import grails.gorm.transactions.Transactional
import io.xh.hoist.RestController
import io.xh.hoist.security.Access

@Transactional
@Access(['HOIST_ADMIN_READER'])
abstract class AdminRestController extends RestController{

Expand Down

0 comments on commit ad09233

Please sign in to comment.