forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiDemo.java
More file actions
134 lines (118 loc) · 3.3 KB
/
ApiDemo.java
File metadata and controls
134 lines (118 loc) · 3.3 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package apps;
import org.jooby.Err;
import org.jooby.Jooby;
import org.jooby.Status;
import org.jooby.apitool.ApiTool;
import java.util.ArrayList;
import java.util.List;
public class ApiDemo extends Jooby {
private static final int START = 0;
private static final int MAX = 200;
{
/**
* Everything about your Pets.
*/
path("/api/pet", () -> {
/**
*
* Add a new pet to the store.
*
* @param body Pet object that needs to be added to the store.
* @return Returns a saved pet or <code>405: Invalid input</code>.
*/
post(req -> {
Pet body = req.body(Pet.class);
DB db = req.require(DB.class);
body = db.create(body);
return body;
});
/**
*
* Update an existing pet.
*
* @param body Pet object that needs to be added to the store.
* @return Returns a saved pet or {400: Invalid ID input}, {404: Pet not found} <code>405: Validation exception</code>.
*/
put(req -> {
Pet body = req.body(Pet.class);
DB db = req.require(DB.class);
body = db.create(body);
return body;
});
/**
* Finds Pets by status.
* Multiple status values can be provided with comma separated strings
*
* @param status Status values that need to be considered for filter.
* @return Returns <code>200</code> with a single pet or <code>400</code>
*/
get("/findByStatus", req -> {
List<String> status = req.param("status").toList();
DB db = req.require(DB.class);
List<Pet> result = new ArrayList<>();
return result;
});
/**
*
* Find pet by ID.
*
* @param id Pet ID.
* @return Returns <code>200</code> with a single pet or <code>404</code>
*/
get("/:id", req -> {
int id = req.param("id").intValue();
DB db = req.require(DB.class);
Pet result = db.find(id);
if (result == null) {
throw new Err(Status.NOT_FOUND);
}
return result;
});
/**
*
* List pets ordered by id.
*
* @param start Start offset, useful for paging. Default is <code>0</code>.
* @param max Max page size, useful for paging. Default is <code>50</code>.
* @return Pets ordered by name.
*/
get(req -> {
int start = req.param("start").intValue(START);
int max = req.param("max").intValue(MAX);
DB db = req.require(DB.class);
List<Pet> results = db.findAll(start, max);
return results;
});
/**
*
* Deletes a pet by ID.
*
* @param id Pet ID.
* @return A <code>204</code>
*/
delete("/:id", req -> {
int id = req.param("id").intValue();
DB db = req.require(DB.class);
Pet result = db.delete(id);
return result;
});
});
/**
* Access to Petstore orders.
*/
path("/api/store", () -> {
/**
* Returns pet inventories by status
*/
get("/inventory", req -> {
return new Inventory();
});
});
use(new ApiTool()
.raml("/raml")
.swagger("/swagger"));
}
public static void main(String[] args) throws Exception {
run(ApiDemo::new, args);
}
}