-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Likelion-Inner-Join/feat/login
[Feat] applicant 로그인 api 추가
- Loading branch information
Showing
10 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
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
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/likelion/innerjoin/user/controller/LoginController.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,27 @@ | ||
package com.likelion.innerjoin.user.controller; | ||
|
||
|
||
import com.likelion.innerjoin.common.response.CommonResponse; | ||
import com.likelion.innerjoin.user.model.dto.request.LoginRequestDto; | ||
import com.likelion.innerjoin.user.model.dto.response.LoginResponseDto; | ||
import com.likelion.innerjoin.user.service.LoginService; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
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 | ||
@RequestMapping("/user") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class LoginController { | ||
private final LoginService loginService; | ||
|
||
@PostMapping("/login") | ||
public CommonResponse<LoginResponseDto> login(@RequestBody LoginRequestDto loginRequestDto, HttpSession session) { | ||
return new CommonResponse<>(loginService.login(loginRequestDto, session)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/likelion/innerjoin/user/exception/ApplicantNotFoundException.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,10 @@ | ||
package com.likelion.innerjoin.user.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ApplicantNotFoundException extends RuntimeException { | ||
private String message; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/likelion/innerjoin/user/exception/LoginExceptionHandler.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,22 @@ | ||
package com.likelion.innerjoin.user.exception; | ||
|
||
import com.likelion.innerjoin.common.exception.ErrorCode; | ||
import com.likelion.innerjoin.common.response.CommonResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class LoginExceptionHandler { | ||
@ExceptionHandler(ApplicantNotFoundException.class) | ||
@ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
public CommonResponse<?> applicantNotFound(ApplicantNotFoundException e, HttpServletRequest request) { | ||
log.warn("APPLICANT-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage()); | ||
return new CommonResponse<>(ErrorCode.APPLICANT_NOT_FOUNT); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/likelion/innerjoin/user/model/dto/request/LoginRequestDto.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,14 @@ | ||
package com.likelion.innerjoin.user.model.dto.request; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class LoginRequestDto { | ||
private String email; | ||
private String password; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/likelion/innerjoin/user/model/dto/response/LoginResponseDto.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,17 @@ | ||
package com.likelion.innerjoin.user.model.dto.response; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class LoginResponseDto { | ||
String studentNumber; | ||
String email; | ||
String name; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/likelion/innerjoin/user/repository/ApplicantRepository.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,8 @@ | ||
package com.likelion.innerjoin.user.repository; | ||
|
||
import com.likelion.innerjoin.user.model.entity.Applicant; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ApplicantRepository extends JpaRepository<Applicant, Long> { | ||
Applicant findByEmailAndPassword(String email, String password); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/likelion/innerjoin/user/service/LoginService.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.likelion.innerjoin.user.service; | ||
|
||
|
||
import com.likelion.innerjoin.user.exception.ApplicantNotFoundException; | ||
import com.likelion.innerjoin.user.model.dto.request.LoginRequestDto; | ||
import com.likelion.innerjoin.user.model.dto.response.LoginResponseDto; | ||
import com.likelion.innerjoin.user.model.entity.Applicant; | ||
import com.likelion.innerjoin.user.repository.ApplicantRepository; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LoginService { | ||
public final ApplicantRepository applicantRepository; | ||
|
||
public LoginResponseDto login(LoginRequestDto loginRequestDto, HttpSession session) { | ||
try{ | ||
Applicant applicant = | ||
applicantRepository.findByEmailAndPassword( | ||
loginRequestDto.getEmail(), | ||
loginRequestDto.getPassword() | ||
); | ||
|
||
if(applicant == null) {throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다.");} | ||
|
||
session.setAttribute("userId", applicant.getId()); | ||
session.setAttribute("role", "applicant"); | ||
return new LoginResponseDto(applicant.getStudentNumber(), applicant.getEmail(), applicant.getName()); | ||
}catch (NoSuchElementException e){ | ||
throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다."); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,3 +16,7 @@ spring: | |
hibernate: | ||
default_batch_fetch_size: 500 | ||
format_sql: true | ||
server: | ||
servlet: | ||
session: | ||
timeout: 30m |