From 920356813c0745fee36821f014137151fbb42972 Mon Sep 17 00:00:00 2001 From: David Aghalarpour Date: Wed, 16 Oct 2013 11:15:22 -1000 Subject: [PATCH] Digits-delete WOD --- app/controllers/Application.java | 11 +++++++++++ app/models/ContactDB.java | 15 +++++++++++++++ app/views/Index.scala.html | 3 ++- conf/routes | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/controllers/Application.java b/app/controllers/Application.java index 2955664..3d3b1ed 100644 --- a/app/controllers/Application.java +++ b/app/controllers/Application.java @@ -60,4 +60,15 @@ public static Result postContact() { } } + + /** + * Deletes corresponding ID from ContactDB. + * @param id The id to delete. + * @return Index page. + */ + public static Result deleteContact(long id) { + ContactDB.deleteContact(id); + + return ok(Index.render(ContactDB.getContacts())); + } } diff --git a/app/models/ContactDB.java b/app/models/ContactDB.java index fe97f95..c45d326 100644 --- a/app/models/ContactDB.java +++ b/app/models/ContactDB.java @@ -53,4 +53,19 @@ public static Contact getContact(long id) { } } + /** + * Deletes a specified id from the contacts Map. + * @param id The ID to delete. + */ + public static void deleteContact(long id) { + Contact contact = contacts.get(id); + + if (contact == null) { + throw new RuntimeException("Passed a bogus id: " + id); + } + else { + contacts.remove(id); + } + } + } diff --git a/app/views/Index.scala.html b/app/views/Index.scala.html index 8f62d3a..3ca1d22 100644 --- a/app/views/Index.scala.html +++ b/app/views/Index.scala.html @@ -16,7 +16,8 @@

Current Contacts

@contact.getFirstName() @contact.getLastName() @contact.getTelephone() - Edit + Edit + Delete } diff --git a/conf/routes b/conf/routes index 7cc5f5b..4c0c324 100644 --- a/conf/routes +++ b/conf/routes @@ -6,6 +6,7 @@ GET / controllers.Application.index() GET /newcontact controllers.Application.newContact(id: Long ?= 0) POST /newcontact controllers.Application.postContact() +GET /delete controllers.Application.deleteContact(id: Long) # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.at(path="/public", file)