Skip to content

Commit

Permalink
feat: 유저 생성 기능 구현 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed May 5, 2023
1 parent 589a72c commit ad63013
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 1 deletion.
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);
}
}
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));
}
}
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;
}
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();
}

}
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;
}
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();
}
}
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());
}
}
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;
}
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;
}
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> {

}
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());
}
}

This file was deleted.

22 changes: 22 additions & 0 deletions thirdSeminarAdvancedAssignment/src/main/resources/application.yml
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

0 comments on commit ad63013

Please sign in to comment.