Skip to content

Commit

Permalink
prepare for persistent domain classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dierk Koenig committed Oct 21, 2024
1 parent c5086b3 commit a514743
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
40 changes: 40 additions & 0 deletions docs/week05/README-5-Persistence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# WebEngineering Module, Persistence

## Goals

### Abilities
- From a greenfield, being able to use a relational database with create-read-update-delete (CRUD)
functionality in a webapp.
- Use of simple entities, many-to-one, and many-to-many relations.

### Knowledge
- Understanding Grails domain classes as simple entities.
- Understanding Grails domain classes with references as relations.
- Understanding dynamic finder methods

## Resources

http://docs.grails.org/latest/guide/GORM.html

## Demo / Live-coding

- Creating a room reservation system
- Domain classes: Room, Person, Booking (very simple)
- Static scaffolding
- Setting up bootstrap data
- Use Controller actions to interact with the domain model

## practical work

- create an action that displays all bookings of a given person
- create an action that displays all available rooms for a requested time slot today

## Homework

Finish the practical work

Build on the practical work from above to
create a full Web MVC-Cycle (without scaffolding) that shows

- all bookings from today until eternity for a given person
- all available rooms for a requested day and time slot
40 changes: 39 additions & 1 deletion grails-app/init/rooms/BootStrap.groovy
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
package rooms

import grails.util.Environment

import java.time.LocalDate

class BootStrap {

static Date today = toUtilDate(LocalDate.now())
static Date tomorrow = toUtilDate(LocalDate.now().plusDays(1))
static Date yesterday = toUtilDate(LocalDate.now().minusDays(1))

/**
* This method is called when the server starts.
*/
def init = { servletContext ->

if (Environment.current == Environment.PRODUCTION) { // guard clause
return
}


}

def destroy = {
}
}

/**
* Convenience method to save a domain object and throw an exception if validation fails.
* @param domainObject
* @return Object - the saved domain object
*/
static <T> T save(T domainObject) {
domainObject.save(failOnError: true) // will throw an exception if validation fails
return domainObject
}

/**
* Convenience method to convert a LocalDate to a java.util.Date
* @param localDate
* @return Date - a java.util.Date
*/
static Date toUtilDate(LocalDate localDate) {
// this is not the best way to do it in general but it works for our purposes
return new Date(localDate.toEpochDay() * 24 * 60 * 60 * 1000)
}
}

0 comments on commit a514743

Please sign in to comment.