-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: 로그인 기능 구현
- Loading branch information
Showing
7 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/com/hyunsolution/dangu/user/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.hyunsolution.dangu.user.controller; | ||
|
||
import com.hyunsolution.dangu.common.apiResponse.ApiResponse; | ||
import com.hyunsolution.dangu.user.dto.request.LoginRequest; | ||
import com.hyunsolution.dangu.user.dto.response.LoginResponse; | ||
import com.hyunsolution.dangu.user.service.UserService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class UserController { | ||
private final UserService userService; | ||
|
||
@PostMapping("/users/login") | ||
@Operation(summary = "로그인", description = "기존아이디, 비밀번호의 경우 로그인을 진행하며 신규 id의 경우 회원가입을 진행한다.") | ||
public ApiResponse<LoginResponse> login(@RequestBody LoginRequest loginRequest) { | ||
String uid = loginRequest.getUid(); | ||
String password = loginRequest.getPassword(); | ||
LoginResponse id = userService.login(uid, password); | ||
return ApiResponse.success(id); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
src/main/java/com/hyunsolution/dangu/user/domain/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package com.hyunsolution.dangu.user.domain; | ||
|
||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> {} | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
Optional<User> findByUid(String uid); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/hyunsolution/dangu/user/dto/request/LoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.hyunsolution.dangu.user.dto.request; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class LoginRequest { | ||
private String uid; | ||
private String password; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hyunsolution/dangu/user/dto/response/LoginResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hyunsolution.dangu.user.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class LoginResponse { | ||
private Long userPk; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/hyunsolution/dangu/user/exception/UserWrongPasswordException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.hyunsolution.dangu.user.exception; | ||
|
||
import com.hyunsolution.dangu.common.exception.CustomException; | ||
|
||
public class UserWrongPasswordException extends CustomException { | ||
public static final UserWrongPasswordException USER_WRONG_PASSWORD_EXCEPTION = | ||
new UserWrongPasswordException(); | ||
|
||
private UserWrongPasswordException() { | ||
super(UserError.USER_WRONG_PASSWORD); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/hyunsolution/dangu/user/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.hyunsolution.dangu.user.service; | ||
|
||
import com.hyunsolution.dangu.user.domain.User; | ||
import com.hyunsolution.dangu.user.domain.UserRepository; | ||
import com.hyunsolution.dangu.user.dto.response.LoginResponse; | ||
import com.hyunsolution.dangu.user.exception.UserWrongPasswordException; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
// TODO: 왜 트랜잭션 어노테이션을 붙이는지 공부하기(with flush가 언제 일어나나) - 다현 | ||
public LoginResponse login(String uid, String password) { | ||
Optional<User> loginUser = userRepository.findByUid(uid); | ||
// 사용자 존재 여부 판단 | ||
if (loginUser.isEmpty()) { | ||
User newUser = registerUser(uid, password); | ||
return new LoginResponse(newUser.getId()); | ||
} | ||
// 비밀번호 일치 확인 | ||
if (loginUser.get().getPassword().equals(password)) { | ||
return new LoginResponse(loginUser.get().getId()); | ||
} else { | ||
throw UserWrongPasswordException.USER_WRONG_PASSWORD_EXCEPTION; | ||
} | ||
} | ||
|
||
public User registerUser(String uid, String password) { | ||
return userRepository.save(User.builder().uid(uid).password(password).build()); | ||
} | ||
} |