forked from GoogleCloudPlatform/endpoints-codelab-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyEndpoint.java
More file actions
77 lines (70 loc) · 3.11 KB
/
MyEndpoint.java
File metadata and controls
77 lines (70 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.google.todotxt.backend;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import java.util.ArrayList;
import java.util.List;
/** An endpoint class we are exposing */
@Api(name = "taskApi", version = "v1",
namespace = @ApiNamespace(ownerDomain = "backend.todotxt.google.com",
ownerName = "backend.todotxt.google.com",
packagePath=""))
public class MyEndpoint {
@ApiMethod(name = "storeTask")
public void storeTask(TaskBean taskBean) {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastoreService.beginTransaction();
try {
Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
Entity taskEntity = new Entity("TaskBean", taskBean.getId(), taskBeanParentKey);
taskEntity.setProperty("data", taskBean.getData());
datastoreService.put(taskEntity);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
}
@ApiMethod(name = "getTasks")
public List<TaskBean> getTasks() {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
Query query = new Query(taskBeanParentKey);
List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
ArrayList<TaskBean> taskBeans = new ArrayList<TaskBean>();
for (Entity result : results) {
TaskBean taskBean = new TaskBean();
taskBean.setId(result.getKey().getId());
taskBean.setData((String) result.getProperty("data"));
taskBeans.add(taskBean);
}
return taskBeans;
}
@ApiMethod(name = "clearTasks")
public void clearTasks() {
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastoreService.beginTransaction();
try {
Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt");
Query query = new Query(taskBeanParentKey);
List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults());
for (Entity result : results) {
datastoreService.delete(result.getKey());
}
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
}
}