-
Notifications
You must be signed in to change notification settings - Fork 0
✨ refactor: Notice 도메인 리팩터링(공지 추가/공지 조회) #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
101444e
refactor(#29): saveNotice 기능에 권한 확인 로직 추가
5nam 8c72589
refactor(#29): findNotices 페이징 방식[cursor 방식] 적용 및 나중에 생성된 순서대로 정렬 적용.…
5nam f91fc18
refactor(#29): saveNotice clubId 받아오는 것 PathVariable 방식으로 변경, 컨트롤러 테스…
5nam 0c3c9df
refactor(#29): 알림 부분 TODO 로 체크
5nam 6220185
Merge branch 'develop' into refactor/29-notice
5nam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
42 changes: 22 additions & 20 deletions
42
src/main/java/com/example/moim/club/controller/NoticeController.java
This file contains hidden or 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,43 +1,45 @@ | ||
| package com.example.moim.club.controller; | ||
|
|
||
| import com.example.moim.club.dto.request.NoticeInput; | ||
| import com.example.moim.club.dto.request.NoticeOutput; | ||
| import com.example.moim.club.dto.response.NoticeOutput; | ||
| import com.example.moim.club.service.NoticeCommandService; | ||
| import com.example.moim.club.service.NoticeCommandServiceImpl; | ||
| import com.example.moim.club.service.NoticeQueryService; | ||
| import com.example.moim.global.exception.BaseResponse; | ||
| import com.example.moim.global.exception.ResponseCode; | ||
| import com.example.moim.user.dto.UserDetailsImpl; | ||
| import com.example.moim.user.entity.User; | ||
| import com.example.moim.user.repository.UserRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class NoticeController implements NoticeControllerDocs { | ||
| private final NoticeCommandService noticeCommandService; | ||
| private final NoticeQueryService noticeQueryService; | ||
| private final UserRepository userRepository; | ||
|
|
||
| /** | ||
| * FIXME: 응답 값이 무조건 있어야 함. user 의 권한 체크는 안해도 되나? | ||
| */ | ||
| @PostMapping("/notice") | ||
| public BaseResponse noticeSave(NoticeInput noticeInput, @AuthenticationPrincipal UserDetailsImpl userDetailsImpl) { | ||
| noticeCommandService.saveNotice(noticeInput); | ||
| return BaseResponse.onSuccess(null, ResponseCode.OK); | ||
| @PostMapping("/notice/{clubId}") | ||
| public BaseResponse<NoticeOutput> saveNotice(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @ModelAttribute NoticeInput noticeInput, @PathVariable("clubId") Long clubId) { | ||
| // public BaseResponse<NoticeOutput> saveNotice(@ModelAttribute NoticeInput noticeInput, @PathVariable("clubId") Long clubId) { | ||
| /** | ||
| * FIXME: 컨트롤러 테스트용 코드 | ||
| */ | ||
| // User user = userRepository.findById(3L).get(); | ||
| NoticeOutput noticeOutput = noticeCommandService.saveNotice(userDetailsImpl.getUser(), noticeInput, clubId); | ||
| return BaseResponse.onSuccess(noticeOutput, ResponseCode.OK); | ||
| } | ||
|
|
||
| /** | ||
| * FIXME: 공지를 시간 순으로 정렬하지 않아도 되나? | ||
| * @return | ||
| */ | ||
| @GetMapping("/notice/{clubId}") | ||
| public BaseResponse<List<NoticeOutput>> noticeSave(@PathVariable Long clubId, @AuthenticationPrincipal UserDetailsImpl userDetailsImpl) { | ||
| return BaseResponse.onSuccess(noticeQueryService.findNotice(clubId), ResponseCode.OK); | ||
| public BaseResponse<Slice<NoticeOutput>> findNotices(@PathVariable Long clubId, @AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @RequestParam("cursorId") Long cursorId) { | ||
| // public BaseResponse<Slice<NoticeOutput>> findNotices(@PathVariable Long clubId, @RequestParam(value = "cursorId", required = false) Long cursorId) { | ||
| /** | ||
| * FIXME: 컨트롤러 테스트용 코드 | ||
| */ | ||
| // User user = userRepository.findById(1L).get(); | ||
| return BaseResponse.onSuccess(noticeQueryService.findNotice(userDetailsImpl.getUser(), clubId, cursorId), ResponseCode.OK); | ||
| } | ||
| } |
14 changes: 10 additions & 4 deletions
14
src/main/java/com/example/moim/club/controller/NoticeControllerDocs.java
This file contains hidden or 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,21 +1,27 @@ | ||
| package com.example.moim.club.controller; | ||
|
|
||
| import com.example.moim.club.dto.request.NoticeInput; | ||
| import com.example.moim.club.dto.request.NoticeOutput; | ||
| import com.example.moim.club.dto.response.NoticeOutput; | ||
| import com.example.moim.global.exception.BaseResponse; | ||
| import com.example.moim.user.dto.UserDetailsImpl; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Tag(name = "모임 공지 api", description = "모임(club) 공지 api 분리") | ||
| public interface NoticeControllerDocs { | ||
| @Operation(summary = "공지 생성") | ||
| BaseResponse noticeSave(NoticeInput noticeInput, @AuthenticationPrincipal UserDetailsImpl userDetailsImpl); | ||
| BaseResponse<NoticeOutput> saveNotice(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @ModelAttribute NoticeInput noticeInput, @PathVariable("clubId") Long clubId); | ||
| // BaseResponse<NoticeOutput> saveNotice(@ModelAttribute NoticeInput noticeInput, @PathVariable Long clubId); | ||
|
|
||
| @Operation(summary = "공지 조회") | ||
| BaseResponse<List<NoticeOutput>> noticeSave(@PathVariable Long clubId, @AuthenticationPrincipal UserDetailsImpl userDetailsImpl); | ||
| @Operation(summary = "공지 조회, 최초 요청 시 cursorId null 로 보내기") | ||
| BaseResponse<Slice<NoticeOutput>> findNotices(@PathVariable Long clubId, @AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @RequestParam Long cursorId); | ||
| // BaseResponse<Slice<NoticeOutput>> findNotices(@PathVariable Long clubId, @RequestParam Long cursorId); | ||
| } |
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
...e/moim/club/dto/request/NoticeOutput.java → .../moim/club/dto/response/NoticeOutput.java
This file contains hidden or 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 hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/example/moim/club/repository/NoticeRepositoryCustom.java
This file contains hidden or 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,11 @@ | ||
| package com.example.moim.club.repository; | ||
|
|
||
| import com.example.moim.club.entity.Club; | ||
| import com.example.moim.club.entity.Notice; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface NoticeRepositoryCustom { | ||
| List<Notice> findByCursor(Long cursor, int size, Club club); | ||
| } | ||
|
|
42 changes: 42 additions & 0 deletions
42
src/main/java/com/example/moim/club/repository/NoticeRepositoryImpl.java
This file contains hidden or 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,42 @@ | ||
| package com.example.moim.club.repository; | ||
|
|
||
| import com.example.moim.club.entity.Club; | ||
| import com.example.moim.club.entity.Notice; | ||
| import com.example.moim.club.entity.QNotice; | ||
| import com.querydsl.core.types.dsl.BooleanExpression; | ||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
| import jakarta.persistence.EntityManager; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class NoticeRepositoryImpl implements NoticeRepositoryCustom { | ||
|
|
||
| private final JPAQueryFactory queryFactory; | ||
|
|
||
| public NoticeRepositoryImpl(EntityManager em) { | ||
| this.queryFactory = new JPAQueryFactory(em); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Notice> findByCursor(Long cursor, int size, Club club) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저희 클라이언트가 모바일이기 때문에 스크롤 할 것을 고려하여 커서 방식의 페이지네이션을 선택하신 점 너무 좋아요! |
||
| return queryFactory | ||
| .selectFrom(QNotice.notice) | ||
| .where( | ||
| ltCursor(cursor), | ||
| clubEq(club) | ||
| ) | ||
| .orderBy(QNotice.notice.id.desc()) | ||
| .limit(size + 1) // hasNext 판단용으로 1개 더 | ||
| .fetch(); | ||
| } | ||
|
|
||
| private BooleanExpression ltCursor(Long cursor) { | ||
| return cursor != null ? QNotice.notice.id.lt(cursor) : null; | ||
| } | ||
|
|
||
| private BooleanExpression clubEq(Club club) { | ||
| return club != null ? QNotice.notice.club.eq(club) : null; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
4 changes: 3 additions & 1 deletion
4
src/main/java/com/example/moim/club/service/NoticeCommandService.java
This file contains hidden or 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,7 +1,9 @@ | ||
| package com.example.moim.club.service; | ||
|
|
||
| import com.example.moim.club.dto.request.NoticeInput; | ||
| import com.example.moim.club.dto.response.NoticeOutput; | ||
| import com.example.moim.user.entity.User; | ||
|
|
||
| public interface NoticeCommandService { | ||
| void saveNotice(NoticeInput noticeInput); | ||
| NoticeOutput saveNotice(User user, NoticeInput noticeInput, Long clubId); | ||
| } |
35 changes: 24 additions & 11 deletions
35
src/main/java/com/example/moim/club/service/NoticeCommandServiceImpl.java
This file contains hidden or 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,32 +1,45 @@ | ||
| package com.example.moim.club.service; | ||
|
|
||
| import com.example.moim.club.dto.request.NoticeInput; | ||
| import com.example.moim.club.dto.request.NoticeOutput; | ||
| import com.example.moim.club.dto.response.NoticeOutput; | ||
| import com.example.moim.club.entity.Club; | ||
| import com.example.moim.club.entity.Notice; | ||
| import com.example.moim.club.entity.UserClub; | ||
| import com.example.moim.club.exception.advice.ClubControllerAdvice; | ||
| import com.example.moim.club.repository.ClubRepository; | ||
| import com.example.moim.club.repository.NoticeRepository; | ||
| import com.example.moim.club.repository.UserClubRepository; | ||
| import com.example.moim.global.enums.ClubRole; | ||
| import com.example.moim.global.exception.ResponseCode; | ||
| import com.example.moim.user.entity.User; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class NoticeCommandServiceImpl implements NoticeCommandService { | ||
| private final NoticeRepository noticeRepository; | ||
| private final ClubRepository clubRepository; | ||
| private final UserClubRepository userClubRepository; | ||
|
|
||
| public NoticeOutput saveNotice(User user, NoticeInput noticeInput, Long clubId) { | ||
| log.debug("saveNotice 진입"); | ||
| Club club = clubRepository.findById(clubId).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.CLUB_NOT_FOUND)); | ||
| UserClub userClub = userClubRepository.findByClubAndUser(club, user).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.CLUB_USER_NOT_FOUND)); | ||
|
|
||
| if (!userClub.getClubRole().equals(ClubRole.STAFF)) { // 공지 등록 권한 확인 | ||
| log.debug("권한 오류"); | ||
| throw new ClubControllerAdvice(ResponseCode.CLUB_PERMISSION_DENIED); | ||
| } | ||
|
|
||
| Notice notice = noticeRepository.save(Notice.createNotice(club, noticeInput.getTitle(), noticeInput.getContent())); | ||
|
|
||
| /** | ||
| * TODO: 알림 보내는 부분 구현해야함 | ||
| */ | ||
|
|
||
| /** | ||
| * transaction 필요하지 않음. save 작업 하나만 이루어지고, 나머지는 다 조회 연산이므로 | ||
| * save 함수 내부에 이미 transaction 적용되어 있음 | ||
| * @param noticeInput | ||
| */ | ||
| public void saveNotice(NoticeInput noticeInput) { | ||
| noticeRepository.save(Notice.createNotice(clubRepository.findById(noticeInput.getClubId()).orElseThrow(() -> new ClubControllerAdvice(ResponseCode.CLUB_NOT_FOUND)), | ||
| noticeInput.getTitle(), noticeInput.getContent())); | ||
| return new NoticeOutput(notice); | ||
| } | ||
| } |
6 changes: 4 additions & 2 deletions
6
src/main/java/com/example/moim/club/service/NoticeQueryService.java
This file contains hidden or 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,9 +1,11 @@ | ||
| package com.example.moim.club.service; | ||
|
|
||
| import com.example.moim.club.dto.request.NoticeOutput; | ||
| import com.example.moim.club.dto.response.NoticeOutput; | ||
| import com.example.moim.user.entity.User; | ||
| import org.springframework.data.domain.Slice; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface NoticeQueryService { | ||
| List<NoticeOutput> findNotice(Long clubId); | ||
| Slice<NoticeOutput> findNotice(User user, Long clubId, Long cursorId); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NotificationRepositoryImpl가NotificationRepository의 구현체로 해석될 위험은 없을까요? 혹시 이름을NotificationJpaRepository로 변경하는건 불필요한 일일까요?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 네이밍에 대한 생각을 못했네요. Jpa로 바꾸는게 좋을 거 같습니다! 반영해서 머지하겠습니다! 좋은 피드백 감사합니다 ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금 오류가 나서 찾아보니까, JPA의 커스텀 리포지토리 규칙이 있다고 합니다! 그래서
XxxRepositoryCustom→ 구현체명은 반드시XxxRepositoryImpl이어야 한다고 합니다!https://imprint.tistory.com/142
링크 공유 드립니다!