-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOtisRequestsController.java
More file actions
223 lines (212 loc) · 7.69 KB
/
OtisRequestsController.java
File metadata and controls
223 lines (212 loc) · 7.69 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package net.onelitefeather.otis.controller;
import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.validation.Validated;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import net.onelitefeather.otis.database.entity.OtisPlayer;
import net.onelitefeather.otis.database.repository.OtisPlayerRepository;
import net.onelitefeather.otis.dto.OtisPlayerDTO;
import java.util.List;
import java.util.UUID;
@Controller("/otis")
public class OtisRequestsController {
private final OtisPlayerRepository repository;
@Inject
public OtisRequestsController(OtisPlayerRepository repository) {
this.repository = repository;
}
@Operation(
summary = "Add a new Otis player",
description = "This endpoint allows you to add a new Otis player to the database.",
tags = {"Player"}
)
@ApiResponse(
responseCode = "200",
description = "Player added successfully",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = OtisPlayerDTO.class)
)
)
@ApiResponse(
responseCode = "500",
description = "The player could not be added to the database",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = String.class)
)
)
@Validated
@Post
public HttpResponse<OtisPlayerDTO> add(@Valid OtisPlayerDTO playerDTO) {
OtisPlayer otisPlayer = OtisPlayer.toEntity(playerDTO);
OtisPlayer saved = repository.save(otisPlayer);
return HttpResponse.ok(saved.toDto());
}
@Operation(
summary = "Get Otis player by ID",
description = "This endpoint retrieves an Otis player by their UUID.",
tags = {"Player"}
)
@ApiResponse(
responseCode = "200",
description = "Player was successfully found.",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = OtisPlayerDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "Player not found",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = String.class)
)
)
@Validated
@Get("/byId/{owner}")
public HttpResponse<OtisPlayerDTO> getById(@Valid UUID owner) {
OtisPlayer entity = this.repository.findById(owner).orElse(null);
if (entity == null) {
return HttpResponse.notFound();
}
return HttpResponse.ok(entity.toDto());
}
@Operation(
summary = "Get Otis player by name",
description = "This endpoint retrieves an Otis player by their name.",
tags = {"Player"}
)
@ApiResponse(
responseCode = "200",
description = "Player was successfully found.",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = OtisPlayerDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "Player not found",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = String.class)
)
)
@Validated
@Get("/byName/{owner}")
public HttpResponse<OtisPlayerDTO> getByString(@Valid String owner) {
OtisPlayer entity = this.repository.findById(UUID.fromString(owner)).orElse(null);
if (entity == null) {
return HttpResponse.notFound();
}
return HttpResponse.ok(entity.toDto());
}
@Operation(
summary = "Update an existing Otis player",
description = "This endpoint allows you to update an existing Otis player in the database.",
tags = {"Player"}
)
@ApiResponse(
responseCode = "200",
description = "Player updated successfully",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = OtisPlayerDTO.class)
)
)
@ApiResponse(
responseCode = "400",
description = "Bad request, player UUID does not match the owner",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = String.class)
)
)
@Validated
@Post("/update/{owner}")
public HttpResponse<OtisPlayerDTO> update(@Valid UUID owner, @Valid OtisPlayerDTO playerDTO) {
if (!playerDTO.uuid().equals(owner)) {
return HttpResponse.badRequest();
}
OtisPlayer otisPlayer = OtisPlayer.toEntity(playerDTO);
OtisPlayer saved = repository.save(otisPlayer);
return HttpResponse.ok(saved.toDto());
}
@Operation(
summary = "Delete an Otis player",
description = "This endpoint allows you to delete an Otis player from the database.",
tags = {"Player"}
)
@ApiResponse(
responseCode = "200",
description = "Player deleted successfully",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = OtisPlayerDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "Player not found",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = String.class)
)
)
@Validated
@Post("/delete/{owner}")
public HttpResponse<OtisPlayerDTO> delete(@Valid UUID owner) {
OtisPlayer entity = this.repository.findById(owner).orElse(null);
if (entity == null) {
return HttpResponse.notFound();
}
this.repository.delete(entity);
return HttpResponse.ok(entity.toDto());
}
/**
* Get all players from the database.
*
* @param pageable the pageable instance
* @return a list of all players
*/
@Operation(
summary = "Get all Otis players",
description = "This endpoint retrieves all Otis players from the database.",
tags = {"Player"}
)
@ApiResponse(
responseCode = "200",
description = "Players retrieved successfully",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = OtisPlayerDTO.class)
)
)
@ApiResponse(
responseCode = "404",
description = "No players found",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = String.class)
)
)
@Get(uris = {"/getAll", "/all"})
public HttpResponse<Iterable<OtisPlayerDTO>> getAll(Pageable pageable) {
Page<OtisPlayer> entities = this.repository.findAll(pageable);
if (entities.isEmpty()) {
return HttpResponse.ok(List.of());
}
return HttpResponse.ok(entities.map(OtisPlayer::toDto));
}
}