Skip to content

Commit

Permalink
static scaffold Person Views
Browse files Browse the repository at this point in the history
  • Loading branch information
Dierk Koenig committed Oct 30, 2024
1 parent 6ffcd77 commit d2736ba
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 89 deletions.
92 changes: 3 additions & 89 deletions grails-app/controllers/rooms/PersonController.groovy
Original file line number Diff line number Diff line change
@@ -1,99 +1,13 @@
package rooms

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)
}
static scaffold = Person // dynamic scaffold

def save(Person person) {
if (person == null) {
notFound()
return
}
def save(Person person) { // static scaffold - keep only the overridden pieces

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] }
}
}
render text: "save Person is no longer allowed via web"

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 }
}
}
}
46 changes: 46 additions & 0 deletions grails-app/views/person/create.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!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&hellip;"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
</ul>
</div>
</section>
<section class="row">
<div id="create-person" class="col-12 content scaffold-create" role="main">
<h1><g:message code="default.create.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<g:hasErrors bean="${this.person}">
<ul class="errors" role="alert">
<g:eachError bean="${this.person}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</g:hasErrors>
<g:form resource="${this.person}" method="POST">
<fieldset class="form">
<f:all bean="person"/>
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
</div>
</section>
</div>
</div>
</body>
</html>
48 changes: 48 additions & 0 deletions grails-app/views/person/edit.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!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.edit.label" args="[entityName]" /></title>
</head>
<body>
<div id="content" role="main">
<div class="container">
<section class="row">
<a href="#edit-person" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
</ul>
</div>
</section>
<section class="row">
<div id="edit-person" class="col-12 content scaffold-edit" role="main">
<h1><g:message code="default.edit.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<g:hasErrors bean="${this.person}">
<ul class="errors" role="alert">
<g:eachError bean="${this.person}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</g:hasErrors>
<g:form resource="${this.person}" method="PUT">
<g:hiddenField name="version" value="${this.person?.version}" />
<fieldset class="form">
<f:all bean="person"/>
</fieldset>
<fieldset class="buttons">
<input class="save" type="submit" value="${message(code: 'default.button.update.label', default: 'Update')}" />
</fieldset>
</g:form>
</div>
</section>
</div>
</div>
</body>
</html>
39 changes: 39 additions & 0 deletions grails-app/views/person/index.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!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.list.label" args="[entityName]" /></title>
</head>
<body>
<div id="content" role="main">
<div class="container">
<section class="row">
<a href="#list-person" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
</ul>
</div>
</section>
<section class="row">
<div id="list-person" class="col-12 content scaffold-list" role="main">
<h1><g:message code="default.list.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>

<f:table collection="${personList}" />

<g:if test="${personCount > params.int('max')}">
<div class="pagination">
<g:paginate total="${personCount ?: 0}" />
</div>
</g:if>
</div>
</section>
</div>
</div>
</body>
</html>
39 changes: 39 additions & 0 deletions grails-app/views/person/show.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!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.show.label" args="[entityName]" /></title>
</head>
<body>
<div id="content" role="main">
<div class="container">
<section class="row">
<a href="#show-person" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
<div class="nav" role="navigation">
<ul>
<li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
<li><g:link class="list" action="index"><g:message code="default.list.label" args="[entityName]" /></g:link></li>
<li><g:link class="create" action="create"><g:message code="default.new.label" args="[entityName]" /></g:link></li>
</ul>
</div>
</section>
<section class="row">
<div id="show-person" class="col-12 content scaffold-show" role="main">
<h1><g:message code="default.show.label" args="[entityName]" /></h1>
<g:if test="${flash.message}">
<div class="message" role="status">${flash.message}</div>
</g:if>
<f:display bean="person" />
<g:form resource="${this.person}" method="DELETE">
<fieldset class="buttons">
<g:link class="edit" action="edit" resource="${this.person}"><g:message code="default.button.edit.label" default="Edit" /></g:link>
<input class="delete" type="submit" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" />
</fieldset>
</g:form>
</div>
</section>
</div>
</div>
</body>
</html>

0 comments on commit d2736ba

Please sign in to comment.