-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOtisSearchController.java
More file actions
106 lines (95 loc) · 3.56 KB
/
OtisSearchController.java
File metadata and controls
106 lines (95 loc) · 3.56 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
package net.onelitefeather.otis.controller;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
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 jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import net.onelitefeather.otis.database.entity.OtisPlayer;
import net.onelitefeather.otis.database.repository.OtisPlayerRepository;
import net.onelitefeather.otis.dto.OtisPlayerDTO;
import java.util.Optional;
import java.util.UUID;
@Controller("/search")
public class OtisSearchController {
private final OtisPlayerRepository otisPlayerRepository;
/**
* Constructs a new reference to the OtisSearchController.
*
* @param playerRepository the repository to access player data.
*/
@Inject
public OtisSearchController(OtisPlayerRepository playerRepository) {
this.otisPlayerRepository = playerRepository;
}
@Operation(
summary = "Search for a player by their ID",
description = "Returns the player information if found, otherwise returns 404 Not Found.",
tags = {"Player", "Search"}
)
@ApiResponse(
responseCode = "200",
description = "Player found and returned 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)
)
)
@Valid
@Get("/byId/{id}")
public HttpResponse<OtisPlayerDTO> searchById(@Valid UUID id) {
Optional<OtisPlayer> entity = otisPlayerRepository.findById(id);
if (entity.isEmpty()) {
return HttpResponse.notFound();
}
return HttpResponse.ok(entity.get().toDto());
}
@Operation(
summary = "Search for a player by their name",
description = "Returns the player information if found, otherwise returns 404 Not Found.",
tags = {"Player", "Search"}
)
@ApiResponse(
responseCode = "200",
description = "Player found and returned 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)
)
)
@Valid
@Get("/byName/{name}")
public HttpResponse<OtisPlayerDTO> searchByName(
@Valid
@Size(min = 3, max = 16)
@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "Username can only contain alphanumeric characters and underscores.")
String name
) {
Optional<OtisPlayer> entity = otisPlayerRepository.findByName(name);
if (entity.isEmpty()) {
return HttpResponse.notFound();
}
return HttpResponse.ok(entity.get().toDto());
}
}