@@ -51,7 +51,8 @@ public class TaskList {
51
51
* Adds a task entity to the Datastore.
52
52
*
53
53
* @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
55
56
*/
56
57
Key addTask (String description ) {
57
58
Key key = datastore .allocateId (keyFactory .newKey ());
@@ -70,14 +71,18 @@ Key addTask(String description) {
70
71
* Marks a task entity as done.
71
72
*
72
73
* @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
74
76
*/
75
- void markDone (long id ) {
77
+ boolean markDone (long id ) {
76
78
Transaction transaction = datastore .newTransaction ();
77
79
try {
78
80
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
+ }
80
84
transaction .commit ();
85
+ return task != null ;
81
86
} finally {
82
87
if (transaction .active ()) {
83
88
transaction .rollback ();
@@ -89,6 +94,8 @@ void markDone(long id) {
89
94
// [START retrieve_entities]
90
95
/**
91
96
* Returns a list of all task entities in ascending order of creation time.
97
+ *
98
+ * @throws DatastoreException if the query fails
92
99
*/
93
100
Iterator <Entity > listTasks () {
94
101
Query <Entity > query =
@@ -102,6 +109,7 @@ Iterator<Entity> listTasks() {
102
109
* Deletes a task entity.
103
110
*
104
111
* @param id The ID of the task entity as given by {@link Key#id()}
112
+ * @throws DatastoreException if the delete fails
105
113
*/
106
114
void deleteTask (long id ) {
107
115
datastore .delete (keyFactory .newKey (id ));
@@ -158,10 +166,9 @@ void handleCommandLine(String commandLine) {
158
166
case "done" :
159
167
assertArgsLength (args , 2 );
160
168
long id = Long .parseLong (args [1 ]);
161
- try {
162
- markDone (id );
169
+ if (markDone (id )) {
163
170
System .out .println ("task marked done" );
164
- } catch ( DatastoreException e ) {
171
+ } else {
165
172
System .out .printf ("did not find a Task entity with ID %d%n" , id );
166
173
}
167
174
break ;
@@ -178,7 +185,7 @@ void handleCommandLine(String commandLine) {
178
185
case "delete" :
179
186
assertArgsLength (args , 2 );
180
187
deleteTask (Long .parseLong (args [1 ]));
181
- System .out .println ("task deleted" );
188
+ System .out .println ("task deleted (if it existed) " );
182
189
break ;
183
190
default :
184
191
throw new IllegalArgumentException ("unrecognized command: " + command );
0 commit comments