Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : 간단한 로그인 기능 추가 #24

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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", "아이디에 해당하는 답변을 찾을 수 업습니다.");
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions src/main/java/hsu/umc/server/converter/UserConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion src/main/java/hsu/umc/server/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions src/main/java/hsu/umc/server/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -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<User, Long> {
Optional<User> findByLoginId(String loginId);
}
8 changes: 8 additions & 0 deletions src/main/java/hsu/umc/server/service/UserService.java
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 29 additions & 0 deletions src/main/java/hsu/umc/server/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public class AnswerController {
private final AnswerCommandService answerCommandService;

//답변 생성
@PostMapping("/{questionId}")
@Operation(summary = "관리자 전용 답변 생성", description = "주어진 정보를 바탕으로 답변을 추가합니다.")
public ApiResponse<AnswerResponseDto.CreateResponseDto> addAnswer(
Expand All @@ -30,22 +29,20 @@ public ApiResponse<AnswerResponseDto.CreateResponseDto> addAnswer(
return ApiResponse.onSuccess(AnswerConverter.toCreateResponseDto(answer));
}

//답변 수정
@PatchMapping("/{questionId}/{answerId}")
@Operation(summary = "관리자 전용 답변 수정", description = "질문 id에 해당하는 답변을 수정합니다.")
public ApiResponse<Object> updateAnswer(
public ApiResponse<AnswerResponseDto.UpdateResponseDto> 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<Object> deleteAnswer(
public ApiResponse<AnswerResponseDto.DeleteResponseDto> deleteAnswer(
@PathVariable("questionId")Long questionId,
@PathVariable("answerId") Long answerId
){
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/hsu/umc/server/web/controller/LoginController.java
Original file line number Diff line number Diff line change
@@ -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<Object> login(
@RequestBody @Valid UserRequestDto.loginDto request
){
User user = userService.login(request);
return ApiResponse.onSuccess(UserConverter.toLoginResponseDto(user));
}
}
22 changes: 22 additions & 0 deletions src/main/java/hsu/umc/server/web/dto/UserRequestDto.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
20 changes: 20 additions & 0 deletions src/main/java/hsu/umc/server/web/dto/UserResponseDto.java
Original file line number Diff line number Diff line change
@@ -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;

}
}
Loading