Skip to content

Commit

Permalink
Fix :: return value in userService
Browse files Browse the repository at this point in the history
  • Loading branch information
priverg committed Oct 10, 2024
1 parent 220d7f4 commit bedb91f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public BaseResponse editStudent(StudentRequestDto studentDto, Long id) {

public StudentResponseDto studentFormat(Student student) {
String studentId = student.getStudentId();
User user = userRepository.findByEmail(student.getMail()).orElse(null);
User user = userService.getUserByEmail(student.getMail()).get();

return StudentResponseDto.builder()
.id(student.getId())
Expand All @@ -194,4 +194,8 @@ public StudentResponseDto studentFormat(Student student) {
.statusTime(user.getStatusTime() != null ? user.getStatusTime().format(formatter()) : null)
.build();
}

private DateTimeFormatter formatter(){
return DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public UserDto getProfileById(long id) {
book.bookName,
book.writer,
book.imageUrl,
book.nfcCode,
book.registrationDate,
book.rentDate,
book.state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;

import java.util.Optional;

public interface UserService {
ApiResponse<JwtResponse> registerUser(SignupRequest signupRequest) throws BusinessException;

Expand All @@ -26,4 +28,6 @@ public interface UserService {
User getTeacher(Authentication authentication);

ResponseEntity<String> updateUserStatus(StatusRequest statusRequest, Authentication authentication);

Optional<User> getUserByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
Expand Down Expand Up @@ -67,6 +69,7 @@ public UserNoticesDto getNoticeByUser(Authentication authentication) {
}

@Override
@Transactional(readOnly = true)
public User getTeacher(Authentication authentication) {
var ud = (UserDetailsImpl) authentication.getPrincipal();

Expand All @@ -83,6 +86,13 @@ public ResponseEntity<String> updateUserStatus(StatusRequest statusRequest, Auth
return ResponseEntity.ok().body("상태 수정 성공");
}

@Override
@Transactional(readOnly = true)
public Optional<User> getUserByEmail(String email){
return Optional.ofNullable(userRepository.findByEmail(email)
.orElseThrow(() -> new UserException(UserError.USER_NOT_FOUND)));
}

/* 인증 및 JWT 토큰 생성 */
public JwtResponse authenticateAndGenerateJWT(String email, String password) {
Authentication authentication = authenticationManager.authenticate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
@Getter
@RequiredArgsConstructor
public enum UserError implements ErrorProperty {
USER_NOT_TEACHER(HttpStatus.BAD_REQUEST, "선생님이 아닙니다.");
USER_NOT_TEACHER(HttpStatus.BAD_REQUEST, "선생님이 아닙니다."),
USER_NOT_FOUND(HttpStatus.BAD_REQUEST, "해당 학생을 찾을 수 없습니다");

private final HttpStatus status;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class UserException extends BusinessException {
private static final UserException USER_NOT_TEACHER = new UserException(UserError.USER_NOT_TEACHER);
private static final UserException USER_NOT_FOUND = new UserException(UserError.USER_NOT_FOUND);

public UserException(ErrorProperty error) {
super(error);
Expand All @@ -13,4 +14,5 @@ public UserException(ErrorProperty error) {
public static UserException userNotTeacher() {
return USER_NOT_TEACHER;
}
public static UserException userNotFound(){ return USER_NOT_FOUND; }
}

0 comments on commit bedb91f

Please sign in to comment.