-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
2,194 additions
and
37 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
app/controllers/org/graceframework/samples/PersonController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.graceframework.samples | ||
|
||
import grails.validation.ValidationException | ||
import static org.springframework.http.HttpStatus.* | ||
|
||
class PersonController { | ||
|
||
PersonService personService | ||
|
||
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] | ||
|
||
def index(Integer max) { | ||
params.max = Math.min(max ?: 10, 100) | ||
respond personService.list(params), model:[personCount: personService.count()] | ||
} | ||
|
||
def show(Long id) { | ||
respond personService.get(id) | ||
} | ||
|
||
def create() { | ||
respond new Person(params) | ||
} | ||
|
||
def save(Person person) { | ||
if (person == null) { | ||
notFound() | ||
return | ||
} | ||
|
||
try { | ||
personService.save(person) | ||
} catch (ValidationException e) { | ||
respond person.errors, view:'create' | ||
return | ||
} | ||
|
||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), person.id]) | ||
redirect person | ||
} | ||
'*' { respond person, [status: CREATED] } | ||
} | ||
} | ||
|
||
def edit(Long id) { | ||
respond personService.get(id) | ||
} | ||
|
||
def update(Person person) { | ||
if (person == null) { | ||
notFound() | ||
return | ||
} | ||
|
||
try { | ||
personService.save(person) | ||
} catch (ValidationException e) { | ||
respond person.errors, view:'edit' | ||
return | ||
} | ||
|
||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.updated.message', args: [message(code: 'person.label', default: 'Person'), person.id]) | ||
redirect person | ||
} | ||
'*'{ respond person, [status: OK] } | ||
} | ||
} | ||
|
||
def delete(Long id) { | ||
if (id == null) { | ||
notFound() | ||
return | ||
} | ||
|
||
personService.delete(id) | ||
|
||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.deleted.message', args: [message(code: 'person.label', default: 'Person'), id]) | ||
redirect action:"index", method:"GET" | ||
} | ||
'*'{ render status: NO_CONTENT } | ||
} | ||
} | ||
|
||
protected void notFound() { | ||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), params.id]) | ||
redirect action: "index", method: "GET" | ||
} | ||
'*'{ render status: NOT_FOUND } | ||
} | ||
} | ||
} |
120 changes: 83 additions & 37 deletions
120
app/controllers/org/graceframework/samples/PetController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,99 @@ | ||
package org.graceframework.samples | ||
|
||
import grails.validation.ValidationException | ||
import static org.springframework.http.HttpStatus.* | ||
|
||
class PetController { | ||
|
||
def petclinicService | ||
PetService petService | ||
|
||
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"] | ||
|
||
def index(Integer max) { | ||
params.max = Math.min(max ?: 10, 100) | ||
respond petService.list(params), model:[petCount: petService.count()] | ||
} | ||
|
||
def show(Long id) { | ||
respond petService.get(id) | ||
} | ||
|
||
def create() { | ||
respond new Pet(params) | ||
} | ||
|
||
def add() { | ||
if (request.method == 'GET') { | ||
return [pet: new Pet(owner: Owner.get(params.owner?.id)), types: PetType.list()] | ||
} | ||
def save(Pet pet) { | ||
if (pet == null) { | ||
notFound() | ||
return | ||
} | ||
|
||
def pet = petclinicService.createPet(params.pet_name, params.pet?.birthDate, | ||
(params.pet?.type?.id ?: 0) as Long, (params.pet_owner_id ?: 0) as Long) | ||
try { | ||
petService.save(pet) | ||
} catch (ValidationException e) { | ||
respond pet.errors, view:'create' | ||
return | ||
} | ||
|
||
if (pet.hasErrors()) { | ||
return [pet: pet, types: PetType.list()] | ||
} | ||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.created.message', args: [message(code: 'pet.label', default: 'Pet'), pet.id]) | ||
redirect pet | ||
} | ||
'*' { respond pet, [status: CREATED] } | ||
} | ||
} | ||
|
||
redirect controller: 'owner', action: 'show', id: pet.owner.id | ||
} | ||
def edit(Long id) { | ||
respond petService.get(id) | ||
} | ||
|
||
def edit() { | ||
if (request.method == 'GET') { | ||
render view: 'add', model: [pet: Pet.get(params.id), types: PetType.list()] | ||
return | ||
} | ||
def update(Pet pet) { | ||
if (pet == null) { | ||
notFound() | ||
return | ||
} | ||
|
||
def pet = Pet.get(params.id) | ||
try { | ||
petService.save(pet) | ||
} catch (ValidationException e) { | ||
respond pet.errors, view:'edit' | ||
return | ||
} | ||
|
||
petclinicService.updatePet(pet, params.pet_name, params.pet?.birthDate, | ||
(params.pet?.type?.id ?: 0) as Long, (params.pet_owner_id ?: 0) as Long) | ||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.updated.message', args: [message(code: 'pet.label', default: 'Pet'), pet.id]) | ||
redirect pet | ||
} | ||
'*'{ respond pet, [status: OK] } | ||
} | ||
} | ||
|
||
if (pet.hasErrors()) { | ||
render view: 'add', model: [pet: pet, types: PetType.list()] | ||
} | ||
else { | ||
redirect controller: 'owner', action: 'show', id: pet.owner.id | ||
} | ||
} | ||
def delete(Long id) { | ||
if (id == null) { | ||
notFound() | ||
return | ||
} | ||
|
||
def addVisit() { | ||
if (request.method == 'GET') { | ||
return [visit: new Visit(pet: Pet.get(params.id))] | ||
} | ||
petService.delete(id) | ||
|
||
def visit = petclinicService.createVisit((params.visit?.pet?.id ?: 0) as Long, params.visit?.description, params.visit?.date) | ||
if (visit.hasErrors()) { | ||
return [visit: visit] | ||
} | ||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.deleted.message', args: [message(code: 'pet.label', default: 'Pet'), id]) | ||
redirect action:"index", method:"GET" | ||
} | ||
'*'{ render status: NO_CONTENT } | ||
} | ||
} | ||
|
||
redirect controller: 'owner', action: 'show', id: visit.pet.owner.id | ||
} | ||
protected void notFound() { | ||
request.withFormat { | ||
form multipartForm { | ||
flash.message = message(code: 'default.not.found.message', args: [message(code: 'pet.label', default: 'Pet'), params.id]) | ||
redirect action: "index", method: "GET" | ||
} | ||
'*'{ render status: NOT_FOUND } | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/services/org/graceframework/samples/PersonService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.graceframework.samples | ||
|
||
import grails.gorm.services.Service | ||
|
||
@Service(Person) | ||
interface PersonService { | ||
|
||
Person get(Serializable id) | ||
|
||
List<Person> list(Map args) | ||
|
||
Long count() | ||
|
||
void delete(Serializable id) | ||
|
||
Person save(Person person) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.graceframework.samples | ||
|
||
import grails.gorm.services.Service | ||
|
||
@Service(Pet) | ||
interface PetService { | ||
|
||
Pet get(Serializable id) | ||
|
||
List<Pet> list(Map args) | ||
|
||
Long count() | ||
|
||
void delete(Serializable id) | ||
|
||
Pet save(Pet pet) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta name="layout" content="main" /> | ||
<g:set var="entityName" value="${message(code: 'person.label', default: 'Person')}" /> | ||
<title><g:message code="default.create.label" args="[entityName]" /></title> | ||
</head> | ||
<body> | ||
<div id="content" role="main"> | ||
<div class="container"> | ||
<section class="row"> | ||
<a href="#create-person" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content…"/></a> | ||
<div class="col-12" role="navigation"> | ||
<ul class="nav nav-pills"> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="${createLink(uri: '/')}"> | ||
<i class="bi bi-house-fill"></i><g:message code="default.home.label"/> | ||
</a> | ||
</li> | ||
<li class="nav-item"> | ||
<g:link class="nav-link" action="index"> | ||
<i class="bi bi-journals"></i><g:message code="default.list.label" args="[entityName]" /> | ||
</g:link> | ||
</li> | ||
</ul> | ||
</div> | ||
</section> | ||
<section class="row"> | ||
<div id="create-person" class="col-12 scaffold scaffold-create" role="main"> | ||
<h1><g:message code="default.create.label" args="[entityName]" /></h1> | ||
<g:if test="${flash.message}"> | ||
<div class="alert alert-success" role="status"><i class="bi bi-info-circle">${flash.message}</div> | ||
</g:if> | ||
<g:hasErrors bean="${this.person}"> | ||
<div class="alert alert-danger" role="alert"> | ||
<ul class="errors"> | ||
<g:eachError bean="${this.person}" var="error"> | ||
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>> | ||
<i class="bi bi-exclamation-circle"></i><g:message error="${error}"/> | ||
</li> | ||
</g:eachError> | ||
</ul> | ||
</div> | ||
</g:hasErrors> | ||
<g:form resource="${this.person}" method="POST"> | ||
<fieldset class="form"> | ||
<f:all bean="person"/> | ||
</fieldset> | ||
<fieldset class="buttons offset-3"> | ||
<button name="create" class="btn btn-primary"> | ||
<i class="bi bi-journal-plus"></i> | ||
<g:message code="default.button.create.label" default="Create" /> | ||
</button> | ||
</fieldset> | ||
</g:form> | ||
</div> | ||
</section> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.