-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserController.java
More file actions
92 lines (80 loc) · 3.72 KB
/
UserController.java
File metadata and controls
92 lines (80 loc) · 3.72 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
package com.gitranker.api.domain.user;
import com.gitranker.api.domain.user.dto.RegisterUserResponse;
import com.gitranker.api.domain.user.service.UserDeletionService;
import com.gitranker.api.domain.user.service.UserQueryService;
import com.gitranker.api.domain.user.service.UserRefreshService;
import com.gitranker.api.global.error.ErrorType;
import com.gitranker.api.global.error.exception.BusinessException;
import com.gitranker.api.global.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@Validated
@RequiredArgsConstructor
@RestController
@Tag(name = "Users")
@RequestMapping("/api/v1/users")
public class UserController {
private static final String USERNAME_PATTERN = "^(?=.{1,39}$)[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$";
private static final String USERNAME_MESSAGE = "{validation.user.username.pattern}";
private final UserQueryService userQueryService;
private final UserRefreshService userRefreshService;
private final UserDeletionService userDeletionService;
@GetMapping("/{username}")
@Operation(summary = "Get a user's profile", description = "Returns the public Git Ranker profile for a GitHub username.")
public ApiResponse<RegisterUserResponse> getUser(
@PathVariable @Pattern(regexp = USERNAME_PATTERN, message = USERNAME_MESSAGE) String username
) {
RegisterUserResponse response = userQueryService.findByUsername(username);
return ApiResponse.success(response);
}
@PostMapping("/{username}/refresh")
@Operation(
summary = "Refresh the authenticated user's score",
description = "Recalculates the caller's own profile. The authenticated user must match the path username.",
security = {
@SecurityRequirement(name = "bearerAuth"),
@SecurityRequirement(name = "accessTokenCookie")
}
)
public ApiResponse<RegisterUserResponse> refreshUser(
@PathVariable @Pattern(regexp = USERNAME_PATTERN, message = USERNAME_MESSAGE) String username,
@AuthenticationPrincipal User user
) {
if (user == null) {
throw new BusinessException(ErrorType.UNAUTHORIZED);
}
if (!user.getUsername().equals(username)) {
throw new BusinessException(ErrorType.FORBIDDEN);
}
RegisterUserResponse response = userRefreshService.refresh(username);
return ApiResponse.success(response);
}
@DeleteMapping("/me")
@Operation(
summary = "Delete the authenticated user's account",
description = "Deletes the current account and clears authentication cookies.",
security = {
@SecurityRequirement(name = "bearerAuth"),
@SecurityRequirement(name = "accessTokenCookie")
}
)
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "204", description = "No Content")
public ResponseEntity<Void> deleteMyAccount(
@AuthenticationPrincipal User user,
HttpServletResponse response
) {
if (user == null) {
throw new BusinessException(ErrorType.UNAUTHORIZED);
}
userDeletionService.deleteAccount(user, response);
return ResponseEntity.noContent().build();
}
}