-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
297 additions
and
1 deletion.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
.../src/main/java/sopt/org/thirdSeminarAdvancedAssignment/api/ControllerExceptionAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.api; | ||
|
||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import sopt.org.thirdSeminarAdvancedAssignment.exception.ApiResponseDto; | ||
import sopt.org.thirdSeminarAdvancedAssignment.exception.ErrorStatus; | ||
|
||
@RestControllerAdvice | ||
public class ControllerExceptionAdvice { | ||
|
||
/* | ||
* 400 BAD_REQUEST | ||
*/ | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
protected ApiResponseDto handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) { | ||
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...dAssignment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/api/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
import sopt.org.thirdSeminarAdvancedAssignment.dto.UserRequestDto; | ||
import sopt.org.thirdSeminarAdvancedAssignment.dto.UserResponseDto; | ||
import sopt.org.thirdSeminarAdvancedAssignment.exception.ApiResponseDto; | ||
import sopt.org.thirdSeminarAdvancedAssignment.exception.SuccessStatus; | ||
import sopt.org.thirdSeminarAdvancedAssignment.service.UserService; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/user") | ||
public class UserController { | ||
private final UserService userService; | ||
|
||
@PostMapping("/signup") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public ApiResponseDto<UserResponseDto> create(@RequestBody @Valid final UserRequestDto userRequestDto) { | ||
return ApiResponseDto.success(SuccessStatus.SIGNUP_SUCCESS, userService.insertUser(userRequestDto)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...AdvancedAssignment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/domain/Post.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public class Post { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long postId; | ||
|
||
@Column | ||
private String title; | ||
|
||
@Column | ||
private int content; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "userId") | ||
private User user; | ||
} |
33 changes: 33 additions & 0 deletions
33
...AdvancedAssignment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Builder | ||
public class User { | ||
|
||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long userId; | ||
|
||
@Column | ||
private String username; | ||
|
||
@Column | ||
private int age; | ||
|
||
public static User of(String username, int age) { | ||
return User.builder() | ||
.username(username) | ||
.age(age) | ||
.build(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...dAssignment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/dto/UserRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.Pattern; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
@Getter | ||
public class UserRequestDto { | ||
|
||
@NotBlank | ||
@Pattern(regexp = "^[가-힣a-zA-Z]{2,10}$", message = "닉네임 형식에 맞지 않습니다.") | ||
private String username; | ||
|
||
private int age; | ||
} |
25 changes: 25 additions & 0 deletions
25
...Assignment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/dto/UserResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class UserResponseDto { | ||
|
||
private Long userId; | ||
private String username; | ||
private int age; | ||
|
||
public static UserResponseDto of(Long userId, String username, int age) { | ||
return UserResponseDto.builder() | ||
.userId(userId) | ||
.username(username) | ||
.age(age) | ||
.build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...nment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/exception/ApiResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.exception; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ApiResponseDto<T> { | ||
|
||
private final int code; | ||
private final String message; | ||
private T data; | ||
|
||
public static ApiResponseDto success(SuccessStatus successStatus) { | ||
return new ApiResponseDto<>(successStatus.getHttpStatus().value(), successStatus.getMessage()); | ||
} | ||
|
||
public static <T> ApiResponseDto<T> success(SuccessStatus successStatus, T data) { | ||
return new ApiResponseDto<T>(successStatus.getHttpStatus().value(), successStatus.getMessage(), data); | ||
} | ||
|
||
public static ApiResponseDto error(ErrorStatus errorStatus) { | ||
return new ApiResponseDto<>(errorStatus.getHttpStatus().value(), errorStatus.getMessage()); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...signment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/exception/ErrorStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.exception; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum ErrorStatus { | ||
|
||
/* | ||
BAD_REQUEST | ||
*/ | ||
VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "요청값이 입력되지 않았습니다."), | ||
|
||
|
||
/* | ||
CONFLICT | ||
*/ | ||
CONFLICT_EMAIL_EXCEPTION(HttpStatus.CONFLICT, "이미 등록된 이메일입니다."), | ||
CONFLICT_NICKNAME_EXCEPTION(HttpStatus.CONFLICT, "이미 등록된 닉네임입니다."), | ||
|
||
/* | ||
SERVER_ERROR | ||
*/ | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "예상치 못한 서버 에러가 발생했습니다."), | ||
BAD_GATEWAY_EXCEPTION(HttpStatus.BAD_GATEWAY, "일시적인 에러가 발생하였습니다.\n잠시 후 다시 시도해주세요!"), | ||
SERVICE_UNAVAILABLE_EXCEPTION(HttpStatus.SERVICE_UNAVAILABLE, "현재 점검 중입니다.\n잠시 후 다시 시도해주세요!"), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
21 changes: 21 additions & 0 deletions
21
...gnment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/exception/SuccessStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.exception; | ||
|
||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum SuccessStatus { | ||
|
||
/* | ||
user | ||
*/ | ||
SIGNUP_SUCCESS(HttpStatus.CREATED, "회원가입이 완료되었습니다."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
8 changes: 8 additions & 0 deletions
8
...ment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import sopt.org.thirdSeminarAdvancedAssignment.domain.User; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...Assignment/src/main/java/sopt/org/thirdSeminarAdvancedAssignment/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sopt.org.thirdSeminarAdvancedAssignment.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import sopt.org.thirdSeminarAdvancedAssignment.domain.User; | ||
import sopt.org.thirdSeminarAdvancedAssignment.dto.UserRequestDto; | ||
import sopt.org.thirdSeminarAdvancedAssignment.dto.UserResponseDto; | ||
import sopt.org.thirdSeminarAdvancedAssignment.repository.UserRepository; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public UserResponseDto insertUser(UserRequestDto userRequestDto) { | ||
User user = User.of(userRequestDto.getUsername(), userRequestDto.getAge()); | ||
User savedUser = userRepository.save(user); | ||
|
||
return UserResponseDto.of(savedUser.getUserId(), user.getUsername(), user.getAge()); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
thirdSeminarAdvancedAssignment/src/main/resources/application.properties
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
thirdSeminarAdvancedAssignment/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
spring: | ||
datasource: | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
url: jdbc:mysql://localhost:3306/sopt_32?useSSL=true&useUnicode=true&serverTimezone=Asia/Seoul | ||
username: root | ||
password: tmdwns0907 | ||
|
||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: update | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
|
||
logging: | ||
level: | ||
org: | ||
hibernate: | ||
type: | ||
descriptor: | ||
sql: trace |