From 9b9d59757b4f038dfabb174df0bbb04c7f010a1b Mon Sep 17 00:00:00 2001 From: Lee Haewon Date: Wed, 10 Jan 2024 17:31:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85,?= =?UTF-8?q?=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/controller/UserController.java | 62 +++++++++++++++++++ .../blooming/domain/user/dto/UserForm.java | 27 ++++++++ .../blooming/domain/user/entity/User.java | 36 +++++++++++ .../user/repository/UserRepository.java | 10 +++ .../domain/user/service/UserService.java | 39 ++++++++++++ .../blooming/global/common/BasicResponse.java | 17 +++++ .../global/error/GlobalExceptionHandler.java | 23 +++++++ 7 files changed, 214 insertions(+) create mode 100644 src/main/java/tenten/blooming/domain/user/controller/UserController.java create mode 100644 src/main/java/tenten/blooming/domain/user/dto/UserForm.java create mode 100644 src/main/java/tenten/blooming/domain/user/entity/User.java create mode 100644 src/main/java/tenten/blooming/domain/user/repository/UserRepository.java create mode 100644 src/main/java/tenten/blooming/domain/user/service/UserService.java create mode 100644 src/main/java/tenten/blooming/global/common/BasicResponse.java create mode 100644 src/main/java/tenten/blooming/global/error/GlobalExceptionHandler.java diff --git a/src/main/java/tenten/blooming/domain/user/controller/UserController.java b/src/main/java/tenten/blooming/domain/user/controller/UserController.java new file mode 100644 index 0000000..415a086 --- /dev/null +++ b/src/main/java/tenten/blooming/domain/user/controller/UserController.java @@ -0,0 +1,62 @@ +package tenten.blooming.domain.user.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import tenten.blooming.domain.user.entity.User; +import tenten.blooming.domain.user.dto.UserForm; +import tenten.blooming.domain.user.service.UserService; +import tenten.blooming.global.common.BasicResponse; + +@RestController +@RequestMapping("/user") +public class UserController { + + @Autowired + private UserService userService; + + @PostMapping("/signup") + public ResponseEntity signup(@RequestBody UserForm dto) { + User user = userService.join(dto); + BasicResponse basicResponse = new BasicResponse(); + + + if (user != null) { + basicResponse = BasicResponse.builder() + .code(HttpStatus.OK.value()) + .message("회원가입에 성공하였습니다.") + .result(user) + .build(); + } + + return new ResponseEntity<>(basicResponse, HttpStatus.OK); + } + + @PostMapping("/login") + public ResponseEntity login(@RequestBody UserForm dto) { + try { + User user = userService.login(dto.getLoginId(), dto.getPassword()); + + BasicResponse basicResponse = BasicResponse.builder() + .code(HttpStatus.OK.value()) + .message("로그인에 성공하였습니다.") + .result(user) + .build(); + + return new ResponseEntity<>(basicResponse, HttpStatus.OK); + } catch (DataIntegrityViolationException e) { + BasicResponse basicResponse = BasicResponse.builder() + .code(HttpStatus.BAD_REQUEST.value()) + .message("로그인에 실패했습니다.") + .result(null) + .build(); + + return new ResponseEntity<>(basicResponse, HttpStatus.BAD_REQUEST); + } + } +} diff --git a/src/main/java/tenten/blooming/domain/user/dto/UserForm.java b/src/main/java/tenten/blooming/domain/user/dto/UserForm.java new file mode 100644 index 0000000..599b9a6 --- /dev/null +++ b/src/main/java/tenten/blooming/domain/user/dto/UserForm.java @@ -0,0 +1,27 @@ +package tenten.blooming.domain.user.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import tenten.blooming.domain.user.entity.User; + +@Data +@AllArgsConstructor +@Builder +public class UserForm { + private Long userId; + + private String loginId; + private String nickname; + private String password; + + public User toEntity() { + User user = new User(); + + user.setLoginId(loginId); + user.setNickname(nickname); + user.setPassword(password); + + return user; + } +} diff --git a/src/main/java/tenten/blooming/domain/user/entity/User.java b/src/main/java/tenten/blooming/domain/user/entity/User.java new file mode 100644 index 0000000..8b3241b --- /dev/null +++ b/src/main/java/tenten/blooming/domain/user/entity/User.java @@ -0,0 +1,36 @@ +package tenten.blooming.domain.user.entity; + +import jakarta.persistence.*; +import lombok.*; +import org.hibernate.annotations.CreationTimestamp; + +import java.sql.Timestamp; + +@AllArgsConstructor +@NoArgsConstructor +@Entity +@Getter @Setter +@Table(name = "users") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "user_id", nullable = false) + private Long userId; // 기본키 + + @Column(name = "login_id", nullable = false) + private String loginId; + + @Column(name = "nickname", nullable = false) + private String nickname; + + @Column(name = "password", nullable = false) + private String password; + + @Column(name = "has_goal", nullable = false) + private Boolean hasGoal = false; + + @CreationTimestamp + @Column(name = "created_at", nullable = false, updatable = false) + private Timestamp createdAt; + +} diff --git a/src/main/java/tenten/blooming/domain/user/repository/UserRepository.java b/src/main/java/tenten/blooming/domain/user/repository/UserRepository.java new file mode 100644 index 0000000..ccaa48b --- /dev/null +++ b/src/main/java/tenten/blooming/domain/user/repository/UserRepository.java @@ -0,0 +1,10 @@ +package tenten.blooming.domain.user.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import tenten.blooming.domain.user.entity.User; + + +public interface UserRepository extends JpaRepository { + boolean existsByLoginId(String loginId); + User findByLoginId(String loginId); +} diff --git a/src/main/java/tenten/blooming/domain/user/service/UserService.java b/src/main/java/tenten/blooming/domain/user/service/UserService.java new file mode 100644 index 0000000..4474a98 --- /dev/null +++ b/src/main/java/tenten/blooming/domain/user/service/UserService.java @@ -0,0 +1,39 @@ +package tenten.blooming.domain.user.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.stereotype.Service; +import tenten.blooming.domain.user.repository.UserRepository; +import tenten.blooming.domain.user.dto.UserForm; +import tenten.blooming.domain.user.entity.User; + +@Service +public class UserService { + @Autowired + private UserRepository userRepository; + + // 로그인 id 중복 체크 + public boolean existsMember(String loginId) { + return userRepository.existsByLoginId(loginId); + } + + public User join(UserForm dto) { + + // 이미 존재하는 아이디로 회원가입한 경우 + if (existsMember(dto.getLoginId())) { + throw new DataIntegrityViolationException("이미 존재하는 아이디입니다."); + } + + return userRepository.save(dto.toEntity()); + } + + public User login(String loginId, String password) { + User findUser = userRepository.findByLoginId(loginId); + + if (findUser != null && findUser.getPassword().equals(password)) { + return findUser; + } else { + throw new DataIntegrityViolationException("로그인에 실패하였습니다."); + } + } +} diff --git a/src/main/java/tenten/blooming/global/common/BasicResponse.java b/src/main/java/tenten/blooming/global/common/BasicResponse.java new file mode 100644 index 0000000..627afbd --- /dev/null +++ b/src/main/java/tenten/blooming/global/common/BasicResponse.java @@ -0,0 +1,17 @@ +package tenten.blooming.global.common; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class BasicResponse { + private Integer code; + private String message; + + private Object result; +} diff --git a/src/main/java/tenten/blooming/global/error/GlobalExceptionHandler.java b/src/main/java/tenten/blooming/global/error/GlobalExceptionHandler.java new file mode 100644 index 0000000..1d9e3d5 --- /dev/null +++ b/src/main/java/tenten/blooming/global/error/GlobalExceptionHandler.java @@ -0,0 +1,23 @@ +package tenten.blooming.global.error; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import tenten.blooming.global.common.BasicResponse; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(DataIntegrityViolationException.class) + public ResponseEntity handleDataIntegrityViolationException(DataIntegrityViolationException ex) { + BasicResponse response = BasicResponse.builder() + .code(HttpStatus.BAD_REQUEST.value()) + .message(ex.getMessage()) + .result(null) + .build(); + + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } +}