-
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.
Merge pull request #168 from urinaner/feature/163
[BE] User 기본 CRUD 기능 구현
- Loading branch information
Showing
12 changed files
with
303 additions
and
32 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
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
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/org/example/backend/user/domain/dto/UserReqDto.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,36 @@ | ||
package org.example.backend.user.domain.dto; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class UserReqDto { | ||
|
||
private String studentId; | ||
|
||
private String name; | ||
|
||
private String major; | ||
|
||
private String phoneN; | ||
|
||
@Builder | ||
private UserReqDto(String studentId, String name, String major, String phoneN) { | ||
this.studentId = studentId; | ||
this.name = name; | ||
this.major = major; | ||
this.phoneN = phoneN; | ||
} | ||
|
||
public static UserReqDto of(String studentId, String name, String major, String phoneN) { | ||
return UserReqDto.builder() | ||
.studentId(studentId) | ||
.name(name) | ||
.major(major) | ||
.phoneN(phoneN) | ||
.build(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
backend/src/main/java/org/example/backend/user/domain/dto/UserResDto.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,41 @@ | ||
package org.example.backend.user.domain.dto; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.example.backend.user.domain.entity.User; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class UserResDto { | ||
|
||
private Long id; | ||
|
||
private String studentId; | ||
|
||
private String name; | ||
|
||
private String major; | ||
|
||
private String phoneN; | ||
|
||
@Builder | ||
private UserResDto(Long id, String studentId, String name, String major, String phoneN) { | ||
this.id = id; | ||
this.studentId = studentId; | ||
this.name = name; | ||
this.major = major; | ||
this.phoneN = phoneN; | ||
} | ||
|
||
public static UserResDto of(User user) { | ||
return UserResDto.builder() | ||
.id(user.getId()) | ||
.studentId(user.getStudentId()) | ||
.name(user.getName()) | ||
.major(user.getMajor()) | ||
.phoneN(user.getPhoneN()) | ||
.build(); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/org/example/backend/user/exception/UserException.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,16 @@ | ||
package org.example.backend.user.exception; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.common.exception.BaseException; | ||
import org.example.backend.common.exception.BaseExceptionType; | ||
|
||
@RequiredArgsConstructor | ||
public class UserException extends BaseException { | ||
|
||
private final UserExceptionType exceptionType; | ||
|
||
@Override | ||
public BaseExceptionType exceptionType() { | ||
return exceptionType; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/org/example/backend/user/exception/UserExceptionType.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,31 @@ | ||
package org.example.backend.user.exception; | ||
|
||
import static org.springframework.http.HttpStatus.BAD_REQUEST; | ||
import static org.springframework.http.HttpStatus.NOT_FOUND; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.common.exception.BaseExceptionType; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@RequiredArgsConstructor | ||
public enum UserExceptionType implements BaseExceptionType { | ||
|
||
NOT_FOUND_USER(NOT_FOUND, "사용자를 찾을 수 없습니다"), | ||
DUPLICATE_PHONE(BAD_REQUEST, "전화번호가 이미 존재합니다."), | ||
REQUIRED_NAME(BAD_REQUEST, "이름은 필수 입력값입니다."), | ||
REQUIRED_STUDENT_ID(BAD_REQUEST, "학번은 필수 입력값입니다.") | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String errorMessage; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return httpStatus; | ||
} | ||
|
||
@Override | ||
public String errorMessage() { | ||
return errorMessage; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/org/example/backend/user/repository/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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.example.backend.user.repository; | ||
|
||
import org.example.backend.user.domain.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
} |
71 changes: 66 additions & 5 deletions
71
backend/src/main/java/org/example/backend/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 |
---|---|---|
@@ -1,26 +1,87 @@ | ||
package org.example.backend.user.service; | ||
|
||
import static org.example.backend.user.exception.UserExceptionType.NOT_FOUND_USER; | ||
|
||
import org.example.backend.user.domain.entity.User; | ||
import org.springframework.data.domain.Pageable; | ||
import lombok.RequiredArgsConstructor; | ||
import org.example.backend.admin.domain.dto.SignInReqDto; | ||
import org.example.backend.admin.exception.AdminException; | ||
import org.example.backend.admin.exception.AdminExceptionType; | ||
import org.example.backend.jwt.JWTUtil; | ||
import org.example.backend.user.domain.dto.UserReqDto; | ||
import org.example.backend.user.domain.dto.UserResDto; | ||
import org.example.backend.user.exception.UserException; | ||
import org.example.backend.user.exception.UserExceptionType; | ||
import org.example.backend.user.repository.UserRepository; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.yj.sejongauth.controller.Sj; | ||
import org.yj.sejongauth.domain.SjProfile; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class UserService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
private final Sj sj; | ||
private final JWTUtil jwtUtil; | ||
public String joinProcess(SignInReqDto joinDTO) { | ||
|
||
@Transactional | ||
public String loginProcess(SignInReqDto joinDTO) { | ||
try { | ||
SjProfile sjProfile = sj.login(joinDTO.getLoginId(), joinDTO.getPassword()); | ||
String token = jwtUtil.createJwt(sjProfile.getName(), "USER", 60 * 60 * 10L); | ||
UserReqDto userReqDto = UserReqDto.builder() | ||
.name(sjProfile.getName()) | ||
.studentId(sjProfile.getStudentCode()) | ||
.major(sjProfile.getMajor()) | ||
.build(); | ||
saveUser(userReqDto); | ||
return token; | ||
} catch (RuntimeException e) { | ||
throw new AdminException(AdminExceptionType.NOT_FOUND_ADMIN); | ||
throw new UserException(NOT_FOUND_USER); | ||
} | ||
} | ||
|
||
@Transactional | ||
public Long saveUser(UserReqDto userReqDto) { | ||
validateRequiredFields(userReqDto); | ||
User user = User.of(userReqDto); | ||
User savedUser = userRepository.save(user); | ||
return savedUser.getId(); | ||
} | ||
|
||
private void validateRequiredFields(UserReqDto userReqDto) { | ||
if (userReqDto.getName() == null || userReqDto.getName().isEmpty()) { | ||
throw new UserException(UserExceptionType.REQUIRED_NAME); | ||
} | ||
if (userReqDto.getStudentId() == null || userReqDto.getStudentId().isEmpty()) { | ||
throw new UserException(UserExceptionType.REQUIRED_STUDENT_ID); | ||
} | ||
} | ||
|
||
public Page<UserResDto> getAllUsers(Pageable pageable) { | ||
return userRepository.findAll(pageable) | ||
.map(UserResDto::of); | ||
} | ||
|
||
@Transactional | ||
public UserResDto updateUser(Long userId, UserReqDto userReqDto) { | ||
User user = findUserById(userId); | ||
user.update(userReqDto); | ||
return UserResDto.of(user); | ||
} | ||
|
||
@Transactional | ||
public void deleteUser(Long userId) { | ||
User user = findUserById(userId); | ||
userRepository.delete(user); | ||
} | ||
|
||
private User findUserById(Long userId) { | ||
return userRepository.findById(userId) | ||
.orElseThrow(() -> new UserException(NOT_FOUND_USER)); | ||
} | ||
} |
Oops, something went wrong.