1 package de.dlr.shepard.auth.users.endpoints;
2
3 import de.dlr.shepard.auth.users.entities.User;
4 import de.dlr.shepard.auth.users.io.UserIO;
5 import de.dlr.shepard.auth.users.services.UserService;
6 import de.dlr.shepard.common.util.Constants;
7 import jakarta.enterprise.context.RequestScoped;
8 import jakarta.inject.Inject;
9 import jakarta.validation.constraints.NotBlank;
10 import jakarta.ws.rs.Consumes;
11 import jakarta.ws.rs.GET;
12 import jakarta.ws.rs.Path;
13 import jakarta.ws.rs.PathParam;
14 import jakarta.ws.rs.Produces;
15 import jakarta.ws.rs.core.MediaType;
16 import jakarta.ws.rs.core.Response;
17 import org.eclipse.microprofile.openapi.annotations.Operation;
18 import org.eclipse.microprofile.openapi.annotations.media.Content;
19 import org.eclipse.microprofile.openapi.annotations.media.Schema;
20 import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
21 import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
22 import org.eclipse.microprofile.openapi.annotations.tags.Tag;
23
24 @Path(Constants.USERS)
25 @Produces(MediaType.APPLICATION_JSON)
26 @Consumes(MediaType.APPLICATION_JSON)
27 @RequestScoped
28 public class UserRest {
29
30 @Inject
31 UserService userService;
32
33 @GET
34 @Tag(name = Constants.USER)
35 @Operation(description = "Get current user")
36 @APIResponse(
37 description = "ok",
38 responseCode = "200",
39 content = @Content(schema = @Schema(implementation = UserIO.class))
40 )
41 @APIResponse(responseCode = "400", description = "bad request")
42 public Response getCurrentUser() {
43 User currentUser = userService.getCurrentUser();
44 return Response.ok(new UserIO(currentUser)).build();
45 }
46
47 @GET
48 @Path("/{" + Constants.USERNAME + "}")
49 @Tag(name = Constants.USER)
50 @Operation(description = "Get user")
51 @APIResponse(
52 description = "ok",
53 responseCode = "200",
54 content = @Content(schema = @Schema(implementation = UserIO.class))
55 )
56 @APIResponse(responseCode = "400", description = "bad request")
57 @APIResponse(responseCode = "401", description = "not authorized")
58 @APIResponse(responseCode = "403", description = "forbidden")
59 @APIResponse(responseCode = "404", description = "not found")
60 @Parameter(name = Constants.USERNAME)
61 public Response getUser(@PathParam(Constants.USERNAME) @NotBlank String username) {
62 User user = userService.getUser(username);
63 return Response.ok(new UserIO(user)).build();
64 }
65 }