Skip to content

Commit

Permalink
#164 Refactor : 사용자 id 얻기 Service 단으로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
ls-rain committed Feb 28, 2024
1 parent d42e600 commit 1afc8be
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 161 deletions.
2 changes: 1 addition & 1 deletion src/main/java/friend/spring/service/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface AlarmService {

void checkAlarm(boolean flag);

Page<Alarm> getAlarmList(Long userId, Integer page);
Page<Alarm> getAlarmList(HttpServletRequest request, Integer page);

AlarmResponseDTO.AlarmLeftResDTO getRemainingAlarm(HttpServletRequest request);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/friend/spring/service/AlarmServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void checkAlarm(boolean flag) {
}

@Override
public Page<Alarm> getAlarmList(Long userId, Integer page) {
public Page<Alarm> getAlarmList(HttpServletRequest request, Integer page) {
Long userId = jwtTokenProvider.getCurrentUser(request);
Optional<User> optionalUser = userRepository.findById(userId);
if (optionalUser.isEmpty()) {
userService.checkUser(false);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/friend/spring/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface CommentService {

void editComment(Long postId, Long commentId, CommentRequestDTO.commentEditReq requestBody, HttpServletRequest request);

Page<Comment> getMyCommentList(Long userId, Integer page);
Page<Comment> getMyCommentList(HttpServletRequest request, Integer page);

void deleteComment(Long postId, Long commentId, HttpServletRequest request);
}
3 changes: 2 additions & 1 deletion src/main/java/friend/spring/service/CommentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ public void editComment(Long postId, Long commentId, CommentRequestDTO.commentEd

//한 유저의 모든 댓글
@Override
public Page<Comment> getMyCommentList(Long userId, Integer page) {
public Page<Comment> getMyCommentList(HttpServletRequest request, Integer page) {
Long userId = jwtTokenProvider.getCurrentUser(request);
Optional<User> optionalUser = userRepository.findById(userId);
if (optionalUser.isEmpty()){
userService.checkUser(false);
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/friend/spring/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@

public interface MyPageService {
void checkPost(Boolean flag);
List<Category> getCategoryList(Long userId);
List<Category> getCategoryList(HttpServletRequest request);

Page<Post> getAllPostList(Long userId, Integer page, Integer sort);
Page<Post> getAllPostList(HttpServletRequest request, Integer page, Integer sort);

void editUserImage(MultipartFile file, HttpServletRequest request);

User getEditUserPage(Long userId);
User getEditUserPage(HttpServletRequest request);

User editUserName(Long userId, MyPageRequestDTO.ProfileEditNameReq profileEditNameReq);
User editUserName(HttpServletRequest request, MyPageRequestDTO.ProfileEditNameReq profileEditNameReq);

User editUserEmail(Long userId, MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq);
User editUserPhone(Long userId, MyPageRequestDTO.ProfileEditPhoneReq profileEditPhoneReq);
User editUserEmail(HttpServletRequest request, MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq);
User editUserPhone(HttpServletRequest request, MyPageRequestDTO.ProfileEditPhoneReq profileEditPhoneReq);

User editUserPassword(Long userId, MyPageRequestDTO.ProfileEditPasswordReq profileEditPasswordReq);
User editUserSecurity(Long userId, MyPageRequestDTO.ProfileEditSecurityReq profileEditSecurityReq);
User editUserPassword(HttpServletRequest request, MyPageRequestDTO.ProfileEditPasswordReq profileEditPasswordReq);
User editUserSecurity(HttpServletRequest request, MyPageRequestDTO.ProfileEditSecurityReq profileEditSecurityReq);

Inquiry createInquiry(Long userId, MyPageRequestDTO.MyInquiryReq myInquiryReq);
Page<Post> getCategoryDetailList(Long userId, Long categoryId, Integer page);
Inquiry createInquiry(HttpServletRequest request, MyPageRequestDTO.MyInquiryReq myInquiryReq);
Page<Post> getCategoryDetailList(HttpServletRequest request, Long categoryId, Integer page);

Category getCategory(Long categoryId);

Expand Down
30 changes: 20 additions & 10 deletions src/main/java/friend/spring/service/MyPageServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void checkPost(Boolean flag) {
}
//저장한 게시물
@Override
public List<Category> getCategoryList(Long userId) {
public List<Category> getCategoryList(HttpServletRequest request) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
List<Post_scrap> scrapList = user.getPostScrapList();
if (scrapList.isEmpty()) {
Expand All @@ -71,7 +72,8 @@ public List<Category> getCategoryList(Long userId) {
}

@Override
public Page<Post> getAllPostList(Long userId, Integer page, Integer sort) {
public Page<Post> getAllPostList(HttpServletRequest request, Integer page, Integer sort) {
Long userId = jwtTokenProvider.getCurrentUser(request);
if (sort == 0){
Page<Post> scrapListByView = postScrapRepository.findPostsByUserIdOrderByPostViewDesc(userId, PageRequest.of(page, 10));
return scrapListByView;
Expand Down Expand Up @@ -106,20 +108,23 @@ public void editUserImage(MultipartFile file, HttpServletRequest request) {
}

@Override
public User getEditUserPage(Long userId) {
public User getEditUserPage(HttpServletRequest request) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
return user;
}

@Override
public User editUserName(Long userId, MyPageRequestDTO.ProfileEditNameReq profileEditNameReq) {
public User editUserName(HttpServletRequest request, MyPageRequestDTO.ProfileEditNameReq profileEditNameReq) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
user.setNickname(profileEditNameReq.getNickName());
return user;
}

@Override
public User editUserEmail(Long userId, MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq) {
public User editUserEmail(HttpServletRequest request, MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
List<User> all = userRepository.findAll();
all.forEach(eachUser -> {
Expand All @@ -132,14 +137,16 @@ public User editUserEmail(Long userId, MyPageRequestDTO.ProfileEditEmailReq prof
}

@Override
public User editUserPhone(Long userId, MyPageRequestDTO.ProfileEditPhoneReq profileEditPhoneReq) {
public User editUserPhone(HttpServletRequest request, MyPageRequestDTO.ProfileEditPhoneReq profileEditPhoneReq) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
user.setPhone(profileEditPhoneReq.getPhone());
return user;
}

@Override
public User editUserPassword(Long userId, MyPageRequestDTO.ProfileEditPasswordReq profileEditPasswordReq) {
public User editUserPassword(HttpServletRequest request, MyPageRequestDTO.ProfileEditPasswordReq profileEditPasswordReq) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
if (!encoder.matches(profileEditPasswordReq.getCurPassword(), user.getPassword())){
throw new GeneralException(ErrorStatus.PASSWORD_INCORRECT);
Expand All @@ -153,7 +160,8 @@ public User editUserPassword(Long userId, MyPageRequestDTO.ProfileEditPasswordRe
}

@Override
public User editUserSecurity(Long userId, MyPageRequestDTO.ProfileEditSecurityReq profileEditSecurityReq) {
public User editUserSecurity(HttpServletRequest request ,MyPageRequestDTO.ProfileEditSecurityReq profileEditSecurityReq) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
List<User> all = userRepository.findAll();
all.forEach(eachUser -> {
Expand All @@ -166,14 +174,16 @@ public User editUserSecurity(Long userId, MyPageRequestDTO.ProfileEditSecurityRe
}

@Override
public Inquiry createInquiry(Long userId, MyPageRequestDTO.MyInquiryReq myInquiryReq) {
public Inquiry createInquiry(HttpServletRequest request, MyPageRequestDTO.MyInquiryReq myInquiryReq) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
Inquiry inquiry = MyPageConverter.toInquiry(myInquiryReq, user);
return inquiryRepository.save(inquiry);
}

@Override
public Page<Post> getCategoryDetailList(Long userId, Long categoryId, Integer page) {
public Page<Post> getCategoryDetailList(HttpServletRequest request, Long categoryId, Integer page) {
Long userId = jwtTokenProvider.getCurrentUser(request);
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
Category category = categoryRepository.findById(categoryId).orElseThrow(() -> new GeneralException(ErrorStatus.POST_CATGORY_NOT_FOUND));
Page<Post> detailList = postRepository.findCategoryDetail(userId, categoryId, PageRequest.of(page, 10));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/friend/spring/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface PostService {

void deletePost(Long postId, Long userId);

Page<Post> getMyPostList(Long userId, Integer page);
Page<Post> getMyPostList(HttpServletRequest request, Integer page);

Post_like likePost(Long postId, HttpServletRequest request);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/friend/spring/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public interface UserService {

User joinUser(UserRequestDTO.UserJoinRequest userJoinRequest);
List<TokenDTO> login(UserRequestDTO.UserLoginRequest userLoginRequest);
User findMyPage(Long id);
User findMyPage(HttpServletRequest httpServletRequest);
void checkUser(Boolean flag);
Integer pointCheck(Long id);
Level nextLevel(Long id);
Level nextLevel(HttpServletRequest request);
String logout(HttpServletRequest request);
List<TokenDTO> reissue(HttpServletRequest request);
User updatePassword(String email, UserRequestDTO.PasswordUpdateReq passwordUpdateReq);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/friend/spring/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import friend.spring.security.JwtTokenProvider;
import friend.spring.web.dto.TokenDTO;
import friend.spring.web.dto.UserRequestDTO;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -45,7 +46,8 @@ public class UserServiceImpl implements UserService {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

@Override
public User findMyPage(Long id) {
public User findMyPage(HttpServletRequest request) {
Long id = jwtTokenProvider.getCurrentUser(request);
Optional<User> user = userRepository.findById(id);
if (user.isEmpty()) {
throw new UserHandler(USER_NOT_FOUND);
Expand Down Expand Up @@ -77,7 +79,8 @@ public User joinUser(UserRequestDTO.UserJoinRequest userJoinRequest) {//회원
}

@Override
public Level nextLevel(Long id) {
public Level nextLevel(HttpServletRequest request) {
Long id = jwtTokenProvider.getCurrentUser(request);
Optional<User> user = userRepository.findById(id);
if (user.isEmpty()) {
throw new UserHandler(USER_NOT_FOUND);
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/friend/spring/web/controller/AlarmRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public class AlarmRestController {

private final AlarmService alarmService;
private final JwtTokenService jwtTokenService;

//알림 조회
@GetMapping("/alarm")
@Operation(summary = "사용자 알림 조회 API",description = "사용자의 알림 목록을 조회하는 API이며, 페이징을 포함합니다. query String 으로 page 번호를 주세요")
@Operation(summary = "사용자 알림 조회 API", description = "사용자의 알림 목록을 조회하는 API이며, 페이징을 포함합니다. query String 으로 page 번호를 주세요")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "ALARM4001",description = "NOT_FOUND, 알림을 찾을 수 없습니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "ALARM4001", description = "NOT_FOUND, 알림을 찾을 수 없습니다."),

})
@Parameters({
Expand All @@ -39,32 +40,31 @@ public class AlarmRestController {
private ApiResponse<AlarmResponseDTO.AlarmListResDTO> getAlarm(
@RequestHeader(name = "atk") String atk,
HttpServletRequest request,
@RequestParam(name = "page") Integer page){
Long userId=jwtTokenService.JwtToId(request);
Page<Alarm> alarmList = alarmService.getAlarmList(userId, page);
@RequestParam(name = "page") Integer page) {
Page<Alarm> alarmList = alarmService.getAlarmList(request, page);
return ApiResponse.onSuccess(AlarmConverter.toAlarmListResDTO(alarmList));
}

// 홈 - 안 읽은 알림 존재 여부 조회
@GetMapping("/alarm/notReadAlarm")
@Operation(summary = "홈 - 안 읽은 알림 존재 여부 조회 API",description = "사용자가 안 읽은 알림이 존재하는지 여부를 조회하는 API입니다.")
@Operation(summary = "홈 - 안 읽은 알림 존재 여부 조회 API", description = "사용자가 안 읽은 알림이 존재하는지 여부를 조회하는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공")
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")
})
@Parameters({})
private ApiResponse<AlarmResponseDTO.AlarmLeftResDTO> getRemainingAlarm(
@RequestHeader(name = "atk") String atk,
HttpServletRequest request
){
) {
return ApiResponse.onSuccess(alarmService.getRemainingAlarm(request));
}

// 알림 읽음 처리
@PatchMapping("/alarm/{alarm-id}")
@Operation(summary = "사용자 알림 읽음 처리 API",description = "사용자의 알림을 읽음 처리하는 API입니다.")
@Operation(summary = "사용자 알림 읽음 처리 API", description = "사용자의 알림을 읽음 처리하는 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "ALARM4001",description = "NOT_FOUND, 알림을 찾을 수 없습니다."),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "ALARM4001", description = "NOT_FOUND, 알림을 찾을 수 없습니다."),
})
@Parameters({
@Parameter(name = "alarm-id", description = "path variable - 알람 아이디"),
Expand All @@ -73,7 +73,7 @@ private ApiResponse<Void> editAlarmRead(
@PathVariable("alarm-id") Long alarmId,
@RequestHeader(name = "atk") String atk,
HttpServletRequest request
){
) {
alarmService.editAlarmRead(alarmId, request);
return ApiResponse.onSuccess(null);
}
Expand Down
Loading

0 comments on commit 1afc8be

Please sign in to comment.