Practice WOD: DigitsCRUD

The purpose of this WOD is to show how with slight changes to your last WOD, you can create a CRUD application (well, almost, we won’t do Delete).

The UI will change as follows. The home page table should now include an “edit” link for each entry:

Clicking that edit link will pre-fill the New Contact page form with the data from that entry:

Editing that entry (such as changing ”Joe” to “Joseph”, then clicking “Add” results in the Joe Smith entry being updated (not the creation of an additional entry):

To get ready for this WOD, create a new branch off of a successfully completed branch from the DigitsModel WOD. Name this new branch “Attempt-N-N-N-N-1″, where N-N-N-N is the branch you’re going to base this WOD on. For example, if the branch “Attempt-2-2-2-2″ contains the DigitsModel code base that you want to build this WOD on, then create a branch called “Attempt-2-2-2-2-1″ to indicate that this is the first attempt (1) to extend the code from “Attempt-2-2-2-2″ to support models and dynamic output. (Yes, I know this naming scheme has gotten ridiculous. I will choose a better one in the next module.)

Instructions

Support Create, Read, and Update for your digits application. You have already implemented Create and Read, so this WOD simply requires you to modify the existing code to support Update. Follow the approach described in the screencast of using an ID field to distinguish between new and pre-existing instances.

  1. Start your timer.
  2. Enable checkstyle.
  3. Update the Contact and ContactFormData classes with a long ID field.
  4. Update the ContactFormData class with a default no-arg constructor (required by Play) as well as a constructor that accepts a Contact.
  5. Update the ContactDB class to use a Map to store the contacts (indexed by ID), and to provide a getContact(id) method to retrieve a Contact based upon its ID.
  6. Update the ContactDB.addContact method to either create a new Contact (and ID) if the form data’s ID value is 0, or else update a pre-existing entry (if it’s non-zero).
  7. Update the routes file so that the newContact method takes “id: Long ?= 0″ as a parameter.
  8. Update the newContact controller to create either an empty ContactFormData class (if the passed ID is 0) or one created from an existing Contact (if the passed ID is non-zero). Call Form.form(ContractFormData.class).fill(data) to populate the Play Form with the ContractFormData instance data.
  9. Update NewContract.scala.html with a hidden field containing the ID value.
  10. Update Index.scala.html with an “Edit” column that invokes the newContact controller method with the appropriate ID to edit each row’s data.
  11. When finished with these changes, test your system and correct any errors that occur.
  12. To end the WOD, commit the working code to your branch.

When finished, stop your timer, and record how many minutes it took you to complete the WOD.

Rx: <26 min Av: 26-40 min Sd: 40-50 min DNF: 50+ min

Demonstration

Once you’ve finished doing the WOD a single time, watch me do it:

Standard WOD Caveats

You’ll learn significantly less from watching me solve the WOD if you haven’t attempted the WOD yourself first.

While it’s an achievement to finish the WOD no matter how long it takes, you might experience “diminishing returns” if you work longer than the DNF time. Thus, it might be strategic to stop working at the DNF time and watch my solution.

After watching my solution, I recommend that you repeat the WOD if you have not achieved at least Av performance. If so, be sure to:

Feel free to keep trying until you make Rx if that’s of interest to you.

How to repeat the WOD

To repeat the WOD, simply switch your local workspace branch to master, then create a new branch off of master called “Attempt-2″ (or whatever attempt you are on). Then, in the play console, type ; clean ; compile ; run to clean the target directory, then recompile the system, then run the web application.