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/5] 로그인 API 구현 #9

Merged
merged 1 commit into from
Jul 4, 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
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ public ApiResponse<UserResponseDTO.JoinResultDTO> join(@RequestBody UserRequestD
User user = userService.join(request);
return ApiResponse.onSuccess(UserConverter.toJoinResultDTO(user));
}

@Operation(summary = "로그인 API")
@PostMapping("/login")
public ApiResponse<UserResponseDTO.LoginResultDTO> login(@RequestBody UserRequestDTO.LoginDto request){
User user = userService.login(request);
return ApiResponse.onSuccess(UserConverter.toLoginResultDTO(user));
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/umc/dream/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public static UserResponseDTO.JoinResultDTO toJoinResultDTO(User user) {
.build();
}

public static UserResponseDTO.LoginResultDTO toLoginResultDTO(User user) {
return UserResponseDTO.LoginResultDTO.builder()
.userId(user.getId())
.build();
}

public static User toUser(UserRequestDTO.JoinDto joinDto) {
return User.builder()
Expand All @@ -24,4 +29,5 @@ public static User toUser(UserRequestDTO.JoinDto joinDto) {
.role(Role.NORMAL)
.build();
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/dto/UserRequestDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ public static class JoinDto {
String passwordConfirm;
}

@Getter
public static class LoginDto {
@NotNull
String account;
@NotNull
String password;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/dto/UserResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ public static class JoinResultDTO{
LocalDateTime createdAt;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class LoginResultDTO {
Long userId;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/umc/dream/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.umc.dream.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByAccount(String account);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

public interface UserService {
User join(UserRequestDTO.JoinDto request);

User login(UserRequestDTO.LoginDto request);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.umc.dream.service.UserService;

import com.umc.dream.apiPayload.code.status.ErrorStatus;
import com.umc.dream.apiPayload.exception.GeneralException;
import com.umc.dream.converter.UserConverter;
import com.umc.dream.domain.User;
import com.umc.dream.dto.UserRequestDTO;
Expand All @@ -19,4 +21,20 @@ public User join(UserRequestDTO.JoinDto request) {
return userRepository.save(user);
}

@Override
public User login(UserRequestDTO.LoginDto request) {
String userAccount = request.getAccount();
String userPassword = request.getPassword();

User user = userRepository.findByAccount(request.getAccount())
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));

if (user.getAccount().equals(userAccount) && user.getPassword().equals(userPassword)) {
return user;
}
else {
throw new GeneralException(ErrorStatus._BAD_REQUEST);
}
}

}
Loading