Skip to content

Commit

Permalink
Phase 4 - Grails Todo Rest Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
basejump committed May 7, 2017
1 parent 2ae74fa commit 2c252c1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,68 @@ https://github.com/basejump/grails-vue-todomvc/tree/Phase2-todomvc-example-copy
Copy setup from https://vuejs.org/v2/examples/todomvc.html

## Phase 3 - Stock Grails Rest Application
https://github.com/basejump/grails-vue-todomvc/tree/Phase3-Stock-Grails-Rest-App

``` bash
$ grails create-app -profile rest-api -features hibernate5 grails-server

| Grails Version: 3.2.9
```

## Phase 4 - Grails Todo Rest Setup

create the Todo domain

``` bash
$ grails create-domain-class Todo
```

``` groovy
import grails.rest.Resource
@Resource(uri = '/todo', formats = ["json"])
class Todo {
String title
Boolean completed = false
Boolean archived = false
static constraints = {
title nullable: false
completed nullable: false
archived nullable: false
}
}
```

turn on cors in the application.yml

```
grails:
cors:
enabled: true
```

add some rows for sanity check
``` groovy
class BootStrap {
def init = { servletContext ->
new Todo(title:"Buy beer",completed:true).save(failOnError:true,flush: true)
new Todo(title:"Drink beer").save(failOnError:true,flush: true)
new Todo(title:"Create Vue/Grails TodoMVC example").save(failOnError:true,flush: true)
}
...
```

run-app and sanity check it with curl

``` bash
$ curl -i -X GET -H "Content-Type: application/json" localhost:8080/todo
HTTP/1.1 200
X-Application-Context: application:development
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 07 May 2017 06:01:57 GMT

[{"id":1,"archived":false,"completed":false,"title":"Buy beer"},{"id":2,"archived":false,"completed":false,"title":"Drink beer"}]
```
2 changes: 2 additions & 0 deletions grails-server/grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ grails:
# Whether to translate GORM events into Reactor events
# Disabled by default for performance reasons
events: false
cors:
enabled: true
info:
app:
name: '@info.app.name@'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package grails.server
class UrlMappings {

static mappings = {

group("/api") {

delete "/$controller/$id(.$format)?"(action:"delete")
get "/$controller(.$format)?"(action:"index")
get "/$controller/$id(.$format)?"(action:"show")
post "/$controller(.$format)?"(action:"save")
put "/$controller/$id(.$format)?"(action:"update")
patch "/$controller/$id(.$format)?"(action:"patch")

}

"/"(controller: 'application', action:'index')
"500"(view: '/error')
Expand Down
17 changes: 17 additions & 0 deletions grails-server/grails-app/domain/grails/server/Todo.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package grails.server

import grails.rest.Resource

@Resource(uri = '/todo', namespace = '/api', formats = ["json"])
class Todo {

String title
Boolean completed = false
Boolean archived = false

static constraints = {
title nullable: false
completed nullable: false
archived nullable: false
}
}
4 changes: 3 additions & 1 deletion grails-server/grails-app/init/grails/server/BootStrap.groovy
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package grails.server

class BootStrap {

def init = { servletContext ->
new Todo(title:"Buy beer",completed:true).save(failOnError:true,flush: true)
new Todo(title:"Drink beer").save(failOnError:true,flush: true)
new Todo(title:"Create Vue/Grails TodoMVC example").save(failOnError:true,flush: true)
}
def destroy = {
}
Expand Down

0 comments on commit 2c252c1

Please sign in to comment.