Permalink
Browse files

improvements on controllers to be more 'REST' ready

  • Loading branch information...
1 parent d9cc9e8 commit 79b6d0fd03edf878e572fb7f1b0299dbd9a93475 @vbarrier vbarrier committed May 3, 2012
@@ -125,5 +125,12 @@ class ProductUrlMappings {
}
}
+ "/ws/p/$product/$controller?/$id?/$type?"(parseRequest: true) {
+ constraints {
+ product(matches: /[0-9A-Z]*/)
+ id(matches: /\d*/)
+ }
+ }
+
}
}
@@ -32,6 +32,7 @@ import grails.converters.XML
import grails.plugins.springsecurity.Secured
import grails.plugin.springcache.annotations.Cacheable
+@Secured('inProduct()')
class ReleaseController {
def releaseService
@@ -40,42 +41,33 @@ class ReleaseController {
@Secured('(productOwner() or scrumMaster()) and !archivedProduct()')
def update = {
- if (!params.release?.id) {
- returnError(text:message(code: 'is.release.error.not.exist'))
- return
- }
- def release = Release.getInProduct(params.long('product'),params.long('release.id')).list()[0]
-
- if (!release) {
- returnError(text:message(code: 'is.release.error.not.exist'))
- return
- }
-
- // If the version is different, the release has been modified since the last loading
- if (params.long('release.version') != release.version) {
- returnError(text:message(code: 'is.release.object', args: [message(code: 'is.release')]))
- return
- }
-
- def startDate = params.startDate ? new Date().parse(message(code: 'is.date.format.short'), params.startDate) : release.startDate
- def endDate = new Date().parse(message(code: 'is.date.format.short'), params.endDate)
- release.properties = params.release
-
- try {
- releaseService.update(release, startDate, endDate)
- def next = null
- if (params.continue) {
- next = release.parentProduct.releases.find {it.orderNumber == release.orderNumber + 1}
+ withRelease{ Release release ->
+ // If the version is different, the release has been modified since the last loading
+ if (params.long('release.version') != release.version) {
+ returnError(text:message(code: 'is.release.object', args: [message(code: 'is.release')]))
+ return
}
- withFormat {
- html { render status: 200, contentType: 'application/json', text: [release: release, next: next?.id ?: null] as JSON }
- json { render status: 200, contentType: 'application/json', text: release as JSON }
- xml { render status: 200, contentType: 'text/xml', text: release as XML }
+
+ def startDate = params.startDate ? new Date().parse(message(code: 'is.date.format.short'), params.startDate) : release.startDate
+ def endDate = new Date().parse(message(code: 'is.date.format.short'), params.endDate)
+ release.properties = params.release
+
+ try {
+ releaseService.update(release, startDate, endDate)
+ def next = null
+ if (params.continue) {
+ next = release.parentProduct.releases.find {it.orderNumber == release.orderNumber + 1}
+ }
+ withFormat {
+ html { render status: 200, contentType: 'application/json', text: [release: release, next: next?.id ?: null] as JSON }
+ json { render status: 200, contentType: 'application/json', text: release as JSON }
+ xml { render status: 200, contentType: 'text/xml', text: release as XML }
+ }
+ } catch (IllegalStateException e) {
+ returnError(exception:e)
+ }catch (RuntimeException e) {
+ returnError(object:release, exception:e)
}
- } catch (IllegalStateException e) {
- returnError(exception:e)
- }catch (RuntimeException e) {
- returnError(object:release, exception:e)
}
}
@@ -176,9 +168,8 @@ class ReleaseController {
}
}
- @Secured('inProduct()')
@Cacheable(cache = 'releaseCache', keyGenerator='releaseKeyGenerator')
- def show = {
+ def index = {
if (request?.format == 'html'){
render(status:404)
return
@@ -191,4 +182,9 @@ class ReleaseController {
}
}
}
+
+ def show = {
+ redirect(action:'index', controller: controllerName, params:params)
+ }
+
}
@@ -32,6 +32,7 @@ import grails.converters.XML
import grails.plugins.springsecurity.Secured
import grails.plugin.springcache.annotations.Cacheable
+@Secured('inProduct()')
class SprintController {
def releaseService
@@ -152,9 +153,8 @@ class SprintController {
}
}
- @Secured('inProduct()')
@Cacheable(cache = 'sprintCache', keyGenerator='sprintKeyGenerator')
- def show = {
+ def index = {
if (request?.format == 'html'){
render(status:404)
return
@@ -167,4 +167,8 @@ class SprintController {
}
}
}
+
+ def show = {
+ redirect(action:'index', controller: controllerName, params:params)
+ }
}
@@ -100,34 +100,40 @@ class StoryController {
} else if (product.preferences.hidden && !securityService.inProduct(story.backlog, springSecurityService.authentication) && !securityService.stakeHolder(story.backlog,springSecurityService.authentication,false)) {
render(status: 403)
} else {
- def permalink = createLink(absolute: true, mapping: "shortURL", params: [product: product.pkey], id: story.uid)
- def criteria = FollowLink.createCriteria()
- def isFollower = false
- if (user) {
- isFollower = criteria.get {
- projections {
- rowCount()
+ withFormat {
+ json { render(status: 200, contentType: 'application/json', text: story as JSON) }
+ xml { render(status: 200, contentType: 'text/xml', text: story as XML) }
+ html {
+ def permalink = createLink(absolute: true, mapping: "shortURL", params: [product: product.pkey], id: story.uid)
+ def criteria = FollowLink.createCriteria()
+ def isFollower = false
+ if (user) {
+ isFollower = criteria.get {
+ projections {
+ rowCount()
+ }
+ eq 'followRef', story.id
+ eq 'followerId', user.id
+ eq 'type', GrailsNameUtils.getPropertyName(Story.class)
+ cache true
+ }
+ isFollower = isFollower == 1
}
- eq 'followRef', story.id
- eq 'followerId', user.id
- eq 'type', GrailsNameUtils.getPropertyName(Story.class)
- cache true
+
+ render(view: 'details', model: [
+ story: story,
+ tasksDone: Task.countByParentStoryAndState(story, Task.STATE_DONE),
+ typeCode: BundleUtils.storyTypes[story.type],
+ storyStateCode: BundleUtils.storyStates[story.state],
+ taskStateBundle: BundleUtils.taskStates,
+ user: user,
+ pkey: product.pkey,
+ permalink: permalink,
+ locale: RequestContextUtils.getLocale(request),
+ isFollower: isFollower,
+ ])
}
- isFollower = isFollower == 1
}
-
- render(view: 'details', model: [
- story: story,
- tasksDone: Task.countByParentStoryAndState(story, Task.STATE_DONE),
- typeCode: BundleUtils.storyTypes[story.type],
- storyStateCode: BundleUtils.storyStates[story.state],
- taskStateBundle: BundleUtils.taskStates,
- user: user,
- pkey: product.pkey,
- permalink: permalink,
- locale: RequestContextUtils.getLocale(request),
- isFollower: isFollower,
- ])
}
}
}
@@ -169,7 +175,7 @@ class StoryController {
}
def update = {
- withStory('story.id') { Story story ->
+ withStory{ Story story ->
def user = springSecurityService.currentUser
if (story.backlog.preferences.archived){
render(status: 403, contentType: 'application/json')
@@ -450,7 +456,7 @@ class StoryController {
@Secured('(isAuthenticated()) and !archivedProduct()')
def associateFeature = {
- withStory('story.id'){ Story story ->
+ withStory{ Story story ->
withFeature('feature.id'){ Feature feature ->
storyService.associateFeature(feature, story)
withFormat {
@@ -524,18 +530,8 @@ class StoryController {
}
@Secured('inProduct()')
- @Cacheable(cache = 'storyCache', keyGenerator='storyKeyGenerator')
def show = {
- if (request?.format == 'html'){
- render(status:404)
- return
- }
- withStory { story ->
- withFormat {
- json { render(status: 200, contentType: 'application/json', text: story as JSON) }
- xml { render(status: 200, contentType: 'text/xml', text: story as XML) }
- }
- }
+ redirect(action:'index', controller: controllerName, params:params)
}
@Secured('inProduct()')
@@ -68,7 +68,7 @@ class TaskController {
def index = {
withTask { Task task ->
- Product product = (Product.get(params.long('product')))
+ def product = task.parentProduct
def user = springSecurityService.currentUser
if (product.preferences.hidden && !user) {
redirect(controller: 'login', params: [ref: "p/${product.pkey}@task/$story.id"])
@@ -77,11 +77,17 @@ class TaskController {
render(status: 403)
return
} else {
- render(view: 'details', model: [
- task: task,
- taskStateCode: BundleUtils.taskStates[task.state],
- taskTypeCode: BundleUtils.taskTypes[task.type]
- ])
+ withFormat {
+ json { render(status: 200, contentType: 'appplication/json', text: task as JSON) }
+ xml { render(status: 200, contentType: 'text/xml', text: task as XML) }
+ html {
+ render(view: 'details', model: [
+ task: task,
+ taskStateCode: BundleUtils.taskStates[task.state],
+ taskTypeCode: BundleUtils.taskTypes[task.type]
+ ])
+ }
+ }
}
}
}
@@ -98,7 +104,7 @@ class TaskController {
task.properties = params.task
User user = (User) springSecurityService.currentUser
- def sprint = Sprint.load(params.id)
+ def sprint = Sprint.load(params.long('sprint.id'))
if (!sprint) {
returnError(text: message(code: 'is.sprint.error.not.exist'))
return
@@ -129,7 +135,7 @@ class TaskController {
@Secured('inProduct() and !archivedProduct()')
def update = {
- withTask('task.id'){Task task ->
+ withTask{Task task ->
def story = !(params.story?.id in ['recurrent', 'urgent']) ? Story.getInProduct(params.long('product'), params.long('story.id')).list()[0] : null
if (!story && !(params.story?.id in ['recurrent', 'urgent'])) {
@@ -334,10 +340,10 @@ class TaskController {
@Secured('inProduct() and !archivedProduct()')
def state = {
// params.id represent the targeted state (STATE_WAIT, STATE_INPROGRESS, STATE_DONE)
- if (!params.id) {
+ if (!params.state) {
returnError(text: message(code: 'is.ui.sprintPlan.state.no.exist'))
}
- withTask('task.id'){Task task ->
+ withTask{Task task ->
User user = (User) springSecurityService.currentUser
// If the task was moved to another story
if (params.story?.id && task.parentStory && params.story.id != task.parentStory.id) {
@@ -357,7 +363,7 @@ class TaskController {
} else if (params.task.type && params.int('task.type') != task.type) {
taskService.changeType(task, params.int('task.type'), user)
}
- taskService.state(task, params.int('id'), user)
+ taskService.state(task, params.int('state'), user)
taskService.rank(task, params.int('position'))
withFormat {
html { render(status: 200, contentType: 'application/json', text: [task:task, story:task.parentStory?.state == Story.STATE_DONE ? task.parentStory : null] as JSON) }
@@ -367,18 +373,8 @@ class TaskController {
}
}
- @Cacheable(cache = 'taskCache', keyGenerator='taskKeyGenerator')
def show = {
- if (request?.format == 'html') {
- render(status: 404)
- return
- }
- withTask('task.id'){Task task ->
- withFormat {
- json { render(status: 200, contentType: 'appplication/json', text: task as JSON) }
- xml { render(status: 200, contentType: 'text/xml', text: task as XML) }
- }
- }
+ redirect(action:'index', controller: controllerName, params:params)
}
@Cacheable(cache = 'taskCache', keyGenerator='tasksKeyGenerator')
@@ -101,11 +101,11 @@ class UserController {
@Secured('isAuthenticated()')
def update = {
- if (params.long('user.id') != springSecurityService.principal.id) {
+ if (params.long('id') != springSecurityService.principal.id) {
returnError(text: message(code: 'is.stale.object', args: [message(code: 'is.user')]))
return
}
- withUser('user.id') { User currentUser ->
+ withUser{ User currentUser ->
if ((params.confirmPassword || params.user.password) && (params.confirmPassword != params.user.password)) {
returnError(text: message(code: 'is.user.error.password.check'))
return
@@ -75,7 +75,7 @@ class ActorController {
@Secured('productOwner() and !archivedProduct()')
def update = {
- withActor('actor.id'){Actor actor ->
+ withActor{Actor actor ->
actor.properties = params.actor
actorService.update(actor)
this.manageAttachments(actor)
@@ -219,8 +219,12 @@ class ActorController {
}
}
- @Cacheable(cache = 'actorCache', keyGenerator='actorKeyGenerator')
def show = {
+ redirect(action:'index', controller: controllerName, params:params)
+ }
+
+ @Cacheable(cache = 'actorCache', keyGenerator='actorKeyGenerator')
+ def index = {
if (request?.format == 'html'){
render(status:404)
return
Oops, something went wrong.

0 comments on commit 79b6d0f

Please sign in to comment.