This repository was archived by the owner on May 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMinApp.java
More file actions
79 lines (61 loc) · 1.97 KB
/
Copy pathMinApp.java
File metadata and controls
79 lines (61 loc) · 1.97 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
package examples;
import io.jooby.Context;
import io.jooby.Jooby;
import io.jooby.StatusCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
public class MinApp extends Jooby {
{
setContextPath("/myapp");
path("/api/pets", () -> {
get("/", ctx -> {
PetRepo repo = require(PetRepo.class);
PetQuery query = ctx.query(PetQuery.class);
return repo.pets(query);
});
get("/{id}", this::findPetById);
post("/", this::createPet);
put("/", this::updatePet);
patch("/", this::updatePet);
delete("/{id}", this::deletePet);
post("/form", this::formPet);
});
}
public Pet formPet(Context context) {
PetRepo repo = require(PetRepo.class);
Pet pet = context.form(Pet.class);
return repo.save(pet);
}
@Operation(requestBody = @RequestBody(description = "Pet to create"))
public Pet createPet(Context ctx) {
PetRepo repo = require(PetRepo.class);
Pet pet = ctx.body(Pet.class);
return repo.save(pet);
}
public Pet updatePet(Context ctx) {
PetRepo repo = require(PetRepo.class);
Pet pet = ctx.body(Pet.class);
return repo.update(pet);
}
@ApiResponse(responseCode = "204")
public Context deletePet(Context ctx) {
PetRepo repo = require(PetRepo.class);
long id = ctx.path("id").longValue();
repo.deleteById(id);
return ctx.send(StatusCode.NO_CONTENT);
}
@Operation(
summary = "Find a pet by ID",
description = "Find a pet by ID or throws a 404",
tags = {"find", "query"},
parameters = @Parameter(description = "Pet ID", in = ParameterIn.PATH)
)
public Pet findPetById(Context ctx) {
PetRepo repo = require(PetRepo.class);
long id = ctx.path("id").longValue();
return repo.findById(id);
}
}