Skip to content

Commit

Permalink
Merge pull request #9 from Hackathon0704/feat/5
Browse files Browse the repository at this point in the history
feat: 로그인 API 구현
  • Loading branch information
yslle authored Jul 4, 2024
2 parents 952cd71 + 0c2a76c commit 002dab3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 0 deletions.
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);
}
}

}

0 comments on commit 002dab3

Please sign in to comment.