From 51c65f3d50342cfac7f6b03fe4b1deb77f7a22cc Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Mon, 19 Aug 2024 20:38:35 +0900 Subject: [PATCH] =?UTF-8?q?Feat=20:=20=EA=B0=84=EB=8B=A8=ED=95=9C=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apipayload/code/status/ErrorStatus.java | 4 ++- .../exception/handler/UserHandler.java | 10 ++++++ .../umc/server/converter/UserConverter.java | 12 +++++++ src/main/java/hsu/umc/server/entity/User.java | 2 +- .../umc/server/repository/UserRepository.java | 10 ++++++ .../hsu/umc/server/service/UserService.java | 8 +++++ .../umc/server/service/UserServiceImpl.java | 29 +++++++++++++++++ .../web/controller/AnswerController.java | 9 ++---- .../web/controller/LoginController.java | 32 +++++++++++++++++++ .../umc/server/web/dto/UserRequestDto.java | 22 +++++++++++++ .../umc/server/web/dto/UserResponseDto.java | 20 ++++++++++++ 11 files changed, 150 insertions(+), 8 deletions(-) create mode 100644 src/main/java/hsu/umc/server/apipayload/exception/handler/UserHandler.java create mode 100644 src/main/java/hsu/umc/server/converter/UserConverter.java create mode 100644 src/main/java/hsu/umc/server/repository/UserRepository.java create mode 100644 src/main/java/hsu/umc/server/service/UserService.java create mode 100644 src/main/java/hsu/umc/server/service/UserServiceImpl.java create mode 100644 src/main/java/hsu/umc/server/web/controller/LoginController.java create mode 100644 src/main/java/hsu/umc/server/web/dto/UserRequestDto.java create mode 100644 src/main/java/hsu/umc/server/web/dto/UserResponseDto.java diff --git a/src/main/java/hsu/umc/server/apipayload/code/status/ErrorStatus.java b/src/main/java/hsu/umc/server/apipayload/code/status/ErrorStatus.java index 16aaeec..de2f5b6 100644 --- a/src/main/java/hsu/umc/server/apipayload/code/status/ErrorStatus.java +++ b/src/main/java/hsu/umc/server/apipayload/code/status/ErrorStatus.java @@ -15,7 +15,9 @@ public enum ErrorStatus implements ErrorCode { _UNAUTHORIZED(HttpStatus.UNAUTHORIZED,"COMMON401","인증이 필요합니다."), _FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "금지된 요청입니다."), _NOT_FOUND(HttpStatus.NOT_FOUND, "COMMON404", "요청을 찾을 수 없습니다."), - + //유저 관련 응답 + USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER4001", "아이디에 해당하는 유저를 찾을 수 없습니다."), + INVALID_CREDENTIALS(HttpStatus.NOT_FOUND, "USER4002", "유저의 비밀번호가 맞지 않습니다."), //질문 관련 응답 QUESTION_NOT_FOUND(HttpStatus.NOT_FOUND, "QUESTION4001", "아이디에 해당하는 질문을 찾을 수 업습니다."), ANSWER_NOT_FOUND(HttpStatus.NOT_FOUND, "ANSWER4001", "아이디에 해당하는 답변을 찾을 수 업습니다."); diff --git a/src/main/java/hsu/umc/server/apipayload/exception/handler/UserHandler.java b/src/main/java/hsu/umc/server/apipayload/exception/handler/UserHandler.java new file mode 100644 index 0000000..6e350dd --- /dev/null +++ b/src/main/java/hsu/umc/server/apipayload/exception/handler/UserHandler.java @@ -0,0 +1,10 @@ +package hsu.umc.server.apipayload.exception.handler; + +import hsu.umc.server.apipayload.code.ErrorCode; +import hsu.umc.server.apipayload.exception.GeneralException; + +public class UserHandler extends GeneralException { + public UserHandler(ErrorCode errorCode) { + super(errorCode); + } +} diff --git a/src/main/java/hsu/umc/server/converter/UserConverter.java b/src/main/java/hsu/umc/server/converter/UserConverter.java new file mode 100644 index 0000000..fe14384 --- /dev/null +++ b/src/main/java/hsu/umc/server/converter/UserConverter.java @@ -0,0 +1,12 @@ +package hsu.umc.server.converter; + +import hsu.umc.server.entity.User; +import hsu.umc.server.web.dto.UserResponseDto; + +public class UserConverter { + public static UserResponseDto.CreateResponseDto toLoginResponseDto(User user) { + return UserResponseDto.CreateResponseDto.builder() + .loginId(user.getLoginId()) + .message("로그인성공").build(); + } +} diff --git a/src/main/java/hsu/umc/server/entity/User.java b/src/main/java/hsu/umc/server/entity/User.java index 258a7f5..553e05b 100644 --- a/src/main/java/hsu/umc/server/entity/User.java +++ b/src/main/java/hsu/umc/server/entity/User.java @@ -16,6 +16,6 @@ public class User extends BaseEntity{ @Column(name = "user_id") private Long userId; - private String LoginId; + private String loginId; private String password; } diff --git a/src/main/java/hsu/umc/server/repository/UserRepository.java b/src/main/java/hsu/umc/server/repository/UserRepository.java new file mode 100644 index 0000000..43730ed --- /dev/null +++ b/src/main/java/hsu/umc/server/repository/UserRepository.java @@ -0,0 +1,10 @@ +package hsu.umc.server.repository; + +import hsu.umc.server.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface UserRepository extends JpaRepository { + Optional findByLoginId(String loginId); +} diff --git a/src/main/java/hsu/umc/server/service/UserService.java b/src/main/java/hsu/umc/server/service/UserService.java new file mode 100644 index 0000000..c077f36 --- /dev/null +++ b/src/main/java/hsu/umc/server/service/UserService.java @@ -0,0 +1,8 @@ +package hsu.umc.server.service; + +import hsu.umc.server.entity.User; +import hsu.umc.server.web.dto.UserRequestDto; + +public interface UserService { + User login(UserRequestDto.loginDto request); +} diff --git a/src/main/java/hsu/umc/server/service/UserServiceImpl.java b/src/main/java/hsu/umc/server/service/UserServiceImpl.java new file mode 100644 index 0000000..9b2a611 --- /dev/null +++ b/src/main/java/hsu/umc/server/service/UserServiceImpl.java @@ -0,0 +1,29 @@ +package hsu.umc.server.service; + +import hsu.umc.server.apipayload.code.status.ErrorStatus; +import hsu.umc.server.apipayload.exception.handler.AnswerHandler; +import hsu.umc.server.apipayload.exception.handler.UserHandler; +import hsu.umc.server.entity.Answer; +import hsu.umc.server.entity.User; +import hsu.umc.server.repository.UserRepository; +import hsu.umc.server.web.dto.UserRequestDto; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class UserServiceImpl implements UserService{ + private final UserRepository userRepository; + @Override + public User login(UserRequestDto.loginDto request) { + User findUser = userRepository.findByLoginId(request.getLoginId()) + .orElseThrow(() -> new UserHandler(ErrorStatus.USER_NOT_FOUND)); + + if (!findUser.getPassword().equals(request.getPassword())) { + throw new UserHandler(ErrorStatus.INVALID_CREDENTIALS); + } + return findUser; + } +} diff --git a/src/main/java/hsu/umc/server/web/controller/AnswerController.java b/src/main/java/hsu/umc/server/web/controller/AnswerController.java index 7278ac0..c5ba06e 100644 --- a/src/main/java/hsu/umc/server/web/controller/AnswerController.java +++ b/src/main/java/hsu/umc/server/web/controller/AnswerController.java @@ -19,7 +19,6 @@ public class AnswerController { private final AnswerCommandService answerCommandService; - //답변 생성 @PostMapping("/{questionId}") @Operation(summary = "관리자 전용 답변 생성", description = "주어진 정보를 바탕으로 답변을 추가합니다.") public ApiResponse addAnswer( @@ -30,22 +29,20 @@ public ApiResponse addAnswer( return ApiResponse.onSuccess(AnswerConverter.toCreateResponseDto(answer)); } - //답변 수정 @PatchMapping("/{questionId}/{answerId}") @Operation(summary = "관리자 전용 답변 수정", description = "질문 id에 해당하는 답변을 수정합니다.") - public ApiResponse updateAnswer( + public ApiResponse updateAnswer( @PathVariable("questionId")Long questionId, @PathVariable("answerId")Long answerId, - @RequestBody AnswerRequestDto.UpdateRequestDto requestDto + @RequestBody @Valid AnswerRequestDto.UpdateRequestDto requestDto ){ Answer updatedAnswer = answerCommandService.updateAnswer(questionId, answerId, requestDto); return ApiResponse.onSuccess(AnswerConverter.toUpdateResponseDto(updatedAnswer)); } - //답변 삭제 @DeleteMapping("/{questionId}/{answerId}") @Operation(summary = "관리자 전용 답변 삭제, description", description = "답변 id에 해당하는 답변을 삭제합니다. ") - public ApiResponse deleteAnswer( + public ApiResponse deleteAnswer( @PathVariable("questionId")Long questionId, @PathVariable("answerId") Long answerId ){ diff --git a/src/main/java/hsu/umc/server/web/controller/LoginController.java b/src/main/java/hsu/umc/server/web/controller/LoginController.java new file mode 100644 index 0000000..265486e --- /dev/null +++ b/src/main/java/hsu/umc/server/web/controller/LoginController.java @@ -0,0 +1,32 @@ +package hsu.umc.server.web.controller; + + +import hsu.umc.server.apipayload.ApiResponse; +import hsu.umc.server.converter.UserConverter; +import hsu.umc.server.entity.User; +import hsu.umc.server.service.UserService; +import hsu.umc.server.web.dto.UserRequestDto; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +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; + +@RestController +@RequiredArgsConstructor +@Tag(name="user",description = "회원 관련 API") +@RequestMapping("/user") +public class LoginController { + private final UserService userService; + @PostMapping("/login") + @Operation(summary = "관리자 로그인", description = "주어진 정보를 바탕으로 로그인을 수행합니다.") + public ApiResponse login( + @RequestBody @Valid UserRequestDto.loginDto request + ){ + User user = userService.login(request); + return ApiResponse.onSuccess(UserConverter.toLoginResponseDto(user)); + } +} diff --git a/src/main/java/hsu/umc/server/web/dto/UserRequestDto.java b/src/main/java/hsu/umc/server/web/dto/UserRequestDto.java new file mode 100644 index 0000000..5d7545d --- /dev/null +++ b/src/main/java/hsu/umc/server/web/dto/UserRequestDto.java @@ -0,0 +1,22 @@ +package hsu.umc.server.web.dto; + +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class UserRequestDto { + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class loginDto{ + @NotNull(message = "로그인 Id는 필수 값입니다.") + private String loginId; + @NotNull(message = "비밀번호는 필수 값입니다.") + private String password; + } + + +} diff --git a/src/main/java/hsu/umc/server/web/dto/UserResponseDto.java b/src/main/java/hsu/umc/server/web/dto/UserResponseDto.java new file mode 100644 index 0000000..b9401f0 --- /dev/null +++ b/src/main/java/hsu/umc/server/web/dto/UserResponseDto.java @@ -0,0 +1,20 @@ +package hsu.umc.server.web.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +public class UserResponseDto { + @Builder + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class CreateResponseDto { + private String loginId; + private String message; + + } +}