-
Notifications
You must be signed in to change notification settings - Fork 5
feature :: 게시판 첨부파일 기능 추가 #318
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
base: develop
Are you sure you want to change the base?
Changes from all commits
f8a20f2
e46702f
1f6cca7
dfa7ed6
b2a8162
e70591e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,11 @@ | |
| import com.kernel360.bbs.entity.BBS; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
|
|
||
| public interface BBSRepository extends BBSRepositoryJPA, BBSRepositoryDSL { | ||
| Page<BBSListDto> getBBSWithCondition(String type, String keyword, Pageable pageable); | ||
| Page<BBSListDto> getBBSWithConditionByPage(String type, String keyword, Pageable pageable); | ||
| Slice<BBSListDto> getBBSWithConditionBySlice(String type, String keyword, Pageable pageable); | ||
|
Comment on lines
+10
to
+11
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. 페이지를 반환하는 메서드와 슬라이스를 반환하는 메서드를 둘 다 만들어 두는 점 좋은 것 같습니다. |
||
|
|
||
| BBS findOneByBbsNo(Long bbsNo); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,9 @@ | |
| import com.querydsl.jpa.impl.JPAQuery; | ||
| import com.querydsl.jpa.impl.JPAQueryFactory; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageImpl; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.*; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static com.kernel360.bbs.entity.QBBS.bBS; | ||
| import static com.kernel360.member.entity.QMember.member; | ||
|
|
@@ -25,20 +22,20 @@ public class BBSRepositoryDSLImpl implements BBSRepositoryDSL { | |
| private final JPAQueryFactory queryFactory; | ||
|
|
||
| @Override | ||
| public Page<BBSListDto> getBBSWithCondition(String type, String keyword, Pageable pageable) { | ||
| public Page<BBSListDto> getBBSWithConditionByPage(String type, String keyword, Pageable pageable) { | ||
|
|
||
| Predicate finalPredicate = bBS.isVisible.eq(true) | ||
| .and(bBS.type.eq(BBSType.valueOf(type).name())); | ||
| .and(bBS.type.eq(BBSType.valueOf(type).name())); | ||
|
|
||
| List<BBSListDto> bbs = getBBSListWithMember(). | ||
| where( | ||
| finalPredicate, | ||
| keywordContains(keyword) | ||
| ) | ||
| .orderBy(bBS.createdAt.desc()) | ||
| .offset(pageable.getOffset()) | ||
| .limit(pageable.getPageSize()) | ||
| .fetch(); | ||
| where( | ||
| finalPredicate, | ||
| keywordContains(keyword) | ||
| ) | ||
| .orderBy(bBS.createdAt.desc()) | ||
| .offset(pageable.getOffset()) | ||
| .limit(pageable.getPageSize() + 1) | ||
| .fetch(); | ||
|
|
||
| Long totalCount = queryFactory | ||
| .select(bBS.count()) | ||
|
|
@@ -52,6 +49,32 @@ public Page<BBSListDto> getBBSWithCondition(String type, String keyword, Pageabl | |
| return new PageImpl<>(bbs, pageable, totalCount); | ||
| } | ||
|
|
||
| @Override | ||
| public Slice<BBSListDto> getBBSWithConditionBySlice(String type, String keyword, Pageable pageable) { | ||
|
|
||
| Predicate finalPredicate = bBS.isVisible.eq(true) | ||
| .and(bBS.type.eq(BBSType.valueOf(type).name())); | ||
|
|
||
| List<BBSListDto> bbs = getBBSListWithMember(). | ||
| where( | ||
| finalPredicate, | ||
| keywordContains(keyword) | ||
| ) | ||
| .orderBy(bBS.createdAt.desc()) | ||
| .offset(pageable.getOffset()) | ||
| .limit(pageable.getPageSize() + 1) | ||
| .fetch(); | ||
|
|
||
| boolean hasNext = false; | ||
| int pageSize = pageable.getPageSize(); | ||
| if(bbs.size() > pageSize){ | ||
| bbs.remove(pageSize); | ||
| hasNext = true; | ||
| } | ||
|
|
||
| return new SliceImpl<>(bbs, pageable, hasNext); | ||
| } | ||
|
|
||
|
Comment on lines
+52
to
+77
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. Slice 적용 시 참고하겠습니다~ |
||
| private JPAQuery<BBSListDto> getBBSListWithMember() { | ||
| return queryFactory | ||
| .select(fields(BBSListDto.class, | ||
|
|
@@ -61,8 +84,8 @@ private JPAQuery<BBSListDto> getBBSListWithMember() { | |
| bBS.createdAt, | ||
| bBS.createdBy, | ||
| bBS.viewCount, | ||
| bBS.member.memberNo, | ||
| bBS.member.id | ||
| member.memberNo, | ||
| member.id | ||
|
Comment on lines
-64
to
+88
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. 이 부분에서 성능 개선이 있었군요! |
||
| ) | ||
|
|
||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,96 @@ | ||
| package com.kernel360.bbs.service; | ||
|
|
||
| import com.kernel360.bbs.code.BBSErrorCode; | ||
| import com.kernel360.bbs.dto.BBSDto; | ||
| import com.kernel360.bbs.dto.BBSListDto; | ||
| import com.kernel360.bbs.entity.BBS; | ||
| import com.kernel360.bbs.repository.BBSRepository; | ||
| import com.kernel360.exception.BusinessException; | ||
| import com.kernel360.file.entity.File; | ||
| import com.kernel360.file.entity.FileReferType; | ||
| import com.kernel360.file.repository.FileRepository; | ||
| import com.kernel360.member.code.MemberErrorCode; | ||
| import com.kernel360.member.dto.MemberDto; | ||
| import com.kernel360.member.service.MemberService; | ||
| import com.kernel360.utils.file.FileUtils; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class BBSService { | ||
| private final FileUtils fileUtils; | ||
| private final BBSRepository bbsRepository; | ||
| private final MemberService memberService; | ||
| private final FileRepository fileRepository; | ||
|
|
||
| @Value("${aws.s3.bucket.url}") | ||
| private String bucketUrl; | ||
| private static final String REFERENCE_TYPE = "BBS"; | ||
|
|
||
| public Page<BBSListDto> getBBSWithConditionByPage(String type, String keyword, Pageable pageable) { | ||
|
|
||
| public Page<BBSListDto> getBBSWithCondition(String type, String keyword, Pageable pageable) { | ||
| return bbsRepository.getBBSWithConditionByPage(type, keyword, pageable); | ||
| } | ||
|
|
||
| public Slice<BBSListDto> getBBSWithConditionBySlice(String type, String keyword, Pageable pageable) { | ||
|
|
||
| return bbsRepository.getBBSWithCondition(type, keyword, pageable); | ||
| return bbsRepository.getBBSWithConditionBySlice(type, keyword, pageable); | ||
| } | ||
|
|
||
| public BBSDto getBBSView(Long bbsNo) { | ||
| BBS entity = Optional.of(bbsRepository.findOneByBbsNo(bbsNo)) | ||
| .orElseThrow(() -> new BusinessException(BBSErrorCode.FAILED_GET_BBS_VIEW)); | ||
|
|
||
| BBSDto result = BBSDto.from(entity, fileRepository.findByReferenceTypeAndReferenceNo(REFERENCE_TYPE, entity.getBbsNo())); | ||
|
|
||
| return BBSDto.from(bbsRepository.findOneByBbsNo(bbsNo)); | ||
| return result; | ||
|
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. 바로 리턴 하셔도 괜찮을거 같습니다~ |
||
| } | ||
|
|
||
| public Page<BBSDto> getBBSReply(Long upperNo, Pageable pageable) { | ||
|
|
||
| return bbsRepository.findAllByUpperNo(upperNo, pageable).map(BBSDto::from); | ||
| return bbsRepository.findAllByUpperNo(upperNo, pageable).map(entity -> BBSDto.from(entity, fileRepository.findByReferenceTypeAndReferenceNo(REFERENCE_TYPE, entity.getBbsNo()))); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void saveBBS(BBSDto bbsDto, String id) { | ||
| public void saveBBS(BBSDto bbsDto, String id, List<MultipartFile> files) { | ||
|
|
||
| MemberDto memberDto = memberService.findByMemberId(id); | ||
| MemberDto memberDto = Optional.of(memberService.findByMemberId(id)) | ||
| .orElseThrow(() -> new BusinessException(MemberErrorCode.FAILED_FIND_MEMBER_INFO)); | ||
|
|
||
| bbsRepository.save(BBS.save(bbsDto.bbsNo(), bbsDto.upperNo(), bbsDto.type(), bbsDto.title(), bbsDto.contents(), true, 0L, memberDto.toEntity())); | ||
| BBS bbs = bbsRepository.save(BBS.save(bbsDto.bbsNo(), bbsDto.upperNo(), bbsDto.type(), bbsDto.title(), bbsDto.contents(), true, 0L, memberDto.toEntity())); | ||
|
|
||
| if(Objects.nonNull(files)){ | ||
| uploadFiles(files, bbs.getBbsNo()); | ||
| } | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deleteBBS(Long bbsNo) { | ||
|
|
||
| bbsRepository.deleteByBbsNo(bbsNo); | ||
| } | ||
|
|
||
| private void uploadFiles(List<MultipartFile> files, Long bbsNo) { | ||
| files.stream().forEach(file -> { | ||
| String path = String.join("/", FileReferType.BBS.getDomain(), bbsNo.toString()); | ||
| String fileKey = fileUtils.upload(path, file); | ||
| String fileUrl = String.join("/", bucketUrl, fileKey); | ||
|
|
||
| File fileInfo = fileRepository.save(File.of(null, file.getOriginalFilename(), fileKey, fileUrl, FileReferType.BBS.getCode(), bbsNo)); | ||
| log.info("게시판 파일 등록 -> file_no {}", fileInfo.getFileNo()); | ||
| }); | ||
| } | ||
| } | ||
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.
byReferenceTypeAndReferenceNo 대신에 좀 더 '파일 리스트'인 게 드러나는 변수명이 좋지 않을까요?