Skip to content

Commit 52e9468

Browse files
authored
Merge pull request #9 from Dalguring/feature/user_and_exception
Feature/user and exception
2 parents b87ee0c + d949d5c commit 52e9468

File tree

11 files changed

+136
-11
lines changed

11 files changed

+136
-11
lines changed

src/main/java/com/rentify/rentify_api/common/config/SwaggerConfig.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ public class SwaggerConfig {
1313

1414
@Bean
1515
public OpenAPI openAPI() {
16-
Server server = new Server();
17-
server.setUrl("http://localhost:8080");
18-
server.setDescription("back-end-server");
16+
Server devServer = new Server();
17+
devServer.setUrl("http://43.201.87.180:8080");
18+
devServer.setDescription("back-end-server");
19+
20+
Server localServer = new Server();
21+
localServer.setUrl("http://localhost:8080");
22+
localServer.setDescription("local-server");
1923

2024
return new OpenAPI()
2125
.info(new Info()
2226
.title("Universal-device-rental")
2327
.version("v1")
2428
.description("다방면용 전자기기 대여 서비스 백엔드 서버")
25-
).servers(List.of(server));
29+
).servers(List.of(devServer, localServer));
2630
}
2731
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.rentify.rentify_api.common.exception;
2+
3+
public class DuplicateException extends RuntimeException {
4+
5+
public DuplicateException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.rentify.rentify_api.common.exception;
2+
3+
import com.rentify.rentify_api.common.response.ApiResponse;
4+
import com.rentify.rentify_api.user.exception.DuplicateEmailException;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.http.converter.HttpMessageNotReadableException;
9+
import org.springframework.web.bind.MethodArgumentNotValidException;
10+
import org.springframework.web.bind.annotation.ExceptionHandler;
11+
import org.springframework.web.bind.annotation.RestControllerAdvice;
12+
13+
@RestControllerAdvice
14+
@Slf4j
15+
public class GlobalExceptionHandler {
16+
17+
@ExceptionHandler(MethodArgumentNotValidException.class)
18+
public ResponseEntity<ApiResponse<Void>> handleValidationException(
19+
MethodArgumentNotValidException ex
20+
) {
21+
String message = ex.getBindingResult()
22+
.getFieldErrors()
23+
.stream()
24+
.findFirst()
25+
.map(error -> error.getField() + " : " + error.getDefaultMessage())
26+
.orElse("요청 값이 올바르지 않습니다.");
27+
28+
return ResponseEntity
29+
.status(HttpStatus.BAD_REQUEST)
30+
.body(ApiResponse.error("INVALID_REQUEST", message));
31+
}
32+
33+
@ExceptionHandler(HttpMessageNotReadableException.class)
34+
public ResponseEntity<ApiResponse<Void>> handleJsonParseException(
35+
HttpMessageNotReadableException ex
36+
) {
37+
38+
return ResponseEntity
39+
.status(HttpStatus.BAD_REQUEST)
40+
.body(ApiResponse.error("INVALID_JSON", "요청 본문(JSON)이 올바르지 않습니다."));
41+
}
42+
43+
@ExceptionHandler(DuplicateException.class)
44+
public ResponseEntity<ApiResponse<Void>> handleDuplicateException(
45+
DuplicateException ex
46+
) {
47+
48+
return ResponseEntity
49+
.status(HttpStatus.CONFLICT)
50+
.body(ApiResponse.error("DUPLICATE", ex.getMessage()));
51+
}
52+
53+
@ExceptionHandler(NotFoundException.class)
54+
public ResponseEntity<ApiResponse<Void>> handleNotFoundException(NotFoundException ex) {
55+
return ResponseEntity
56+
.status(HttpStatus.NOT_FOUND)
57+
.body(ApiResponse.error("NOT_FOUND", ex.getMessage()));
58+
}
59+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.rentify.rentify_api.common.exception;
2+
3+
public class NotFoundException extends RuntimeException {
4+
5+
public NotFoundException(String message) {
6+
super(message);
7+
}
8+
}

src/main/java/com/rentify/rentify_api/user/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public class UserController {
2727
private final UserService userService;
2828

2929
@PostMapping
30-
public ResponseEntity<ApiResponse<Void>> createUser(@Valid @RequestBody CreateUserRequest request) {
30+
public ResponseEntity<ApiResponse<Long>> createUser(@Valid @RequestBody CreateUserRequest request) {
3131
Long userId = userService.signup(request);
3232
URI location = URI.create("/api/users/" + userId);
3333

34-
return ResponseEntity.created(location).body(ApiResponse.success());
34+
return ResponseEntity.created(location).body(ApiResponse.success(userId));
3535
}
3636

3737
@GetMapping("/{id}")

src/main/java/com/rentify/rentify_api/user/dto/UserResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public class UserResponse {
1414
private final String bank;
1515
private final String account;
1616
private final String phone;
17+
private final Boolean isActive;
1718
}

src/main/java/com/rentify/rentify_api/user/entity/User.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class User {
7070
@Column(name = "updated_at", nullable = false)
7171
private LocalDateTime updateAt;
7272

73+
@Column(name = "is_active", nullable = false)
74+
private Boolean isActive;
75+
7376
public void addPoint(int amount) {
7477
if (amount < 0) {
7578
throw new IllegalArgumentException("amount must be >= 0");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.rentify.rentify_api.user.exception;
2+
3+
import com.rentify.rentify_api.common.exception.DuplicateException;
4+
5+
public class DuplicateEmailException extends DuplicateException {
6+
7+
public DuplicateEmailException() {
8+
super("이미 가입된 이메일 주소입니다.");
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.rentify.rentify_api.user.exception;
2+
3+
import com.rentify.rentify_api.common.exception.NotFoundException;
4+
5+
public class UserNotFoundException extends NotFoundException {
6+
7+
public UserNotFoundException() {
8+
super("존재하지 않는 사용자입니다.");
9+
}
10+
}

src/main/java/com/rentify/rentify_api/user/service/UserService.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.rentify.rentify_api.user.dto.CreateUserRequest;
44
import com.rentify.rentify_api.user.dto.UserResponse;
55
import com.rentify.rentify_api.user.entity.User;
6+
import com.rentify.rentify_api.user.entity.UserRole;
7+
import com.rentify.rentify_api.user.exception.DuplicateEmailException;
8+
import com.rentify.rentify_api.user.exception.UserNotFoundException;
69
import com.rentify.rentify_api.user.repository.UserRepository;
710
import lombok.RequiredArgsConstructor;
811
import lombok.extern.slf4j.Slf4j;
@@ -20,14 +23,34 @@ public class UserService {
2023

2124
@Transactional
2225
public Long signup(CreateUserRequest request) {
26+
// 멱등성 처리 (나중에 추가)
2327

24-
return 0L;
28+
if (userRepository.existsByEmail(request.getEmail())) {
29+
throw new DuplicateEmailException();
30+
}
31+
32+
User user = User.builder()
33+
.name(request.getName())
34+
.email(request.getEmail())
35+
.password(passwordEncoder.encode(request.getPassword()))
36+
.address(request.getAddress())
37+
.bank(request.getBank())
38+
.account(request.getAccount())
39+
.point(0)
40+
.pointVersion((short) 0)
41+
.role(UserRole.USER)
42+
.phone(request.getPhone())
43+
.isActive(true)
44+
.build();
45+
46+
User saved = userRepository.save(user);
47+
return saved.getId();
2548
}
2649

2750
@Transactional(readOnly = true)
2851
public UserResponse getUserInfo(Long id) {
2952
User user = userRepository.findById(id)
30-
.orElseThrow(() -> new IllegalArgumentException("사용자를 찾을 수 없습니다."));
53+
.orElseThrow(UserNotFoundException::new);
3154

3255
return UserResponse.builder()
3356
.id(user.getId())
@@ -37,6 +60,7 @@ public UserResponse getUserInfo(Long id) {
3760
.bank(user.getBank())
3861
.account(user.getAccount())
3962
.phone(user.getPhone())
63+
.isActive(user.getIsActive())
4064
.build();
4165
}
4266
}

0 commit comments

Comments
 (0)