Skip to content

Commit

Permalink
feature: 로그인 기능 구현(#32)
Browse files Browse the repository at this point in the history
feature: 로그인 기능 구현
  • Loading branch information
Lee-Dahyeon authored Nov 4, 2024
2 parents c2e417d + 7906a8e commit 0b9134c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 2 deletions.
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);
}
}
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);
}
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;
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@AllArgsConstructor
public enum UserError implements BaseErrorCode {
USER_NOT_FOUND("USER_400_1", HttpStatus.BAD_REQUEST, "사용자를 찾을 수 없습니다."),
;
USER_WRONG_PASSWORD("USER_400_2", HttpStatus.BAD_REQUEST, "비밀번호가 틀렸습니다.");

private final String code;
private final HttpStatus httpStatus;
Expand Down
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 src/main/java/com/hyunsolution/dangu/user/service/UserService.java
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());
}
}

0 comments on commit 0b9134c

Please sign in to comment.