@@ -51,7 +51,8 @@ public class TaskList {
5151 * Adds a task entity to the Datastore.
5252 *
5353 * @param description The task description
54- * @return The {@link Key} of the entity.
54+ * @return The {@link Key} of the entity
55+ * @throws DatastoreException if the Datastore put fails
5556 */
5657 Key addTask (String description ) {
5758 Key key = datastore .allocateId (keyFactory .newKey ());
@@ -70,14 +71,18 @@ Key addTask(String description) {
7071 * Marks a task entity as done.
7172 *
7273 * @param id The ID of the task entity as given by {@link Key#id()}
73- * @throws DatastoreException if the task does not exist
74+ * @return true if the task was found, false if not
75+ * @throws DatastoreException if the transaction commit fails
7476 */
75- void markDone (long id ) {
77+ boolean markDone (long id ) {
7678 Transaction transaction = datastore .newTransaction ();
7779 try {
7880 Entity task = transaction .get (keyFactory .newKey (id ));
79- transaction .put (Entity .builder (task ).set ("done" , true ).build ());
81+ if (task != null ) {
82+ transaction .put (Entity .builder (task ).set ("done" , true ).build ());
83+ }
8084 transaction .commit ();
85+ return task != null ;
8186 } finally {
8287 if (transaction .active ()) {
8388 transaction .rollback ();
@@ -89,6 +94,8 @@ void markDone(long id) {
8994 // [START retrieve_entities]
9095 /**
9196 * Returns a list of all task entities in ascending order of creation time.
97+ *
98+ * @throws DatastoreException if the query fails
9299 */
93100 Iterator <Entity > listTasks () {
94101 Query <Entity > query =
@@ -102,6 +109,7 @@ Iterator<Entity> listTasks() {
102109 * Deletes a task entity.
103110 *
104111 * @param id The ID of the task entity as given by {@link Key#id()}
112+ * @throws DatastoreException if the delete fails
105113 */
106114 void deleteTask (long id ) {
107115 datastore .delete (keyFactory .newKey (id ));
@@ -158,10 +166,9 @@ void handleCommandLine(String commandLine) {
158166 case "done" :
159167 assertArgsLength (args , 2 );
160168 long id = Long .parseLong (args [1 ]);
161- try {
162- markDone (id );
169+ if (markDone (id )) {
163170 System .out .println ("task marked done" );
164- } catch ( DatastoreException e ) {
171+ } else {
165172 System .out .printf ("did not find a Task entity with ID %d%n" , id );
166173 }
167174 break ;
@@ -178,7 +185,7 @@ void handleCommandLine(String commandLine) {
178185 case "delete" :
179186 assertArgsLength (args , 2 );
180187 deleteTask (Long .parseLong (args [1 ]));
181- System .out .println ("task deleted" );
188+ System .out .println ("task deleted (if it existed) " );
182189 break ;
183190 default :
184191 throw new IllegalArgumentException ("unrecognized command: " + command );
0 commit comments