Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ default boolean doesNotExistById(UUID id) {
return !existsById(id);
}
Optional<Center> findCenterById(UUID id);
String findNameById(UUID id);
void deleteAllInBatch();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.somemore.center.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.somemore.center.domain.Center;
import com.somemore.center.domain.QCenter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand All @@ -14,7 +12,6 @@
public class CenterRepositoryImpl implements CenterRepository {

private final CenterJpaRepository centerJpaRepository;
private final JPAQueryFactory queryFactory;

@Override
public Center save(Center center) {
Expand All @@ -31,17 +28,6 @@ public Optional<Center> findCenterById(UUID id) {
return centerJpaRepository.findCenterById(id);
}

@Override
public String findNameById(UUID id) {
QCenter center = QCenter.center;

return queryFactory
.select(center.name)
.from(center)
.where(center.id.eq(id))
.fetchOne();
}

@Override
public void deleteAllInBatch() {
centerJpaRepository.deleteAllInBatch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.somemore.center.service.query;

import com.somemore.center.domain.Center;
import com.somemore.center.dto.response.CenterForCommunityResponseDto;
import com.somemore.center.dto.response.CenterProfileResponseDto;
import com.somemore.center.dto.response.PreferItemResponseDto;
import com.somemore.center.repository.CenterRepository;
Expand Down Expand Up @@ -45,15 +44,4 @@ private Center getCenterById(UUID centerId) {
return centerRepository.findCenterById(centerId)
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_CENTER.getMessage()));
}

@Override
public String getNameById(UUID id) {
return centerRepository.findNameById(id);
}

@Override
public CenterForCommunityResponseDto getCenterDetailForCommunity(UUID id) {
Center center = getCenterById(id);
return CenterForCommunityResponseDto.fromEntity(center);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.somemore.center.usecase.query;

import com.somemore.center.dto.response.CenterForCommunityResponseDto;
import com.somemore.center.dto.response.CenterProfileResponseDto;

import java.util.UUID;
Expand All @@ -9,6 +8,4 @@ public interface CenterQueryUseCase {

CenterProfileResponseDto getCenterProfileByCenterId(UUID centerId);
void validateCenterExists(UUID centerId);
String getNameById(UUID id);
CenterForCommunityResponseDto getCenterDetailForCommunity(UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.somemore.community.domain;

public record CommunityBoardView(
CommunityBoard communityBoard,
String writerNickname) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
import com.somemore.community.domain.CommunityBoard;

import java.time.LocalDateTime;
import java.util.UUID;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record CommunityBoardGetDetailResponseDto(
Long id,
UUID writerId,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

volunteerId보다 writerId가 좋을까요? 사실 저도 모르겠어요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시나 추후에 센터도 작성 가능하게 될 걸 고려해서 바꾸지 않았는데 volunteerId로 바꾸는게 나을까요?
저도 고민되어서 바꾸지 않고 그냥 뒀습니다..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 자유롭게 결정해주셔도 될 것 같아요

String title,
String content,
String imageUrl,
LocalDateTime createdAt,
LocalDateTime updatedAt,
WriterDetailDto writerDetailDto
LocalDateTime updatedAt
) {
public static CommunityBoardGetDetailResponseDto fromEntity(CommunityBoard board, WriterDetailDto writer) {
public static CommunityBoardGetDetailResponseDto fromEntity(CommunityBoard board) {
return new CommunityBoardGetDetailResponseDto(
board.getId(),
board.getWriterId(),
board.getTitle(),
board.getContent(),
board.getImgUrl(),
board.getCreatedAt(),
board.getUpdatedAt(),
writer
board.getUpdatedAt()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardView;

import java.time.LocalDateTime;

Expand All @@ -13,12 +13,12 @@ public record CommunityBoardGetResponseDto(
String writerNickname,
LocalDateTime createdAt
) {
public static CommunityBoardGetResponseDto fromEntity(CommunityBoard board, String writerNickname) {
public static CommunityBoardGetResponseDto fromEntity(CommunityBoardView board) {
return new CommunityBoardGetResponseDto(
board.getId(),
board.getTitle(),
writerNickname,
board.getCreatedAt()
board.communityBoard().getId(),
board.communityBoard().getTitle(),
board.writerNickname(),
board.communityBoard().getCreatedAt()
);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.somemore.community.repository;

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardView;

import java.util.List;
import java.util.Optional;
Expand All @@ -9,7 +10,7 @@
public interface CommunityBoardRepository {
CommunityBoard save(CommunityBoard communityBoard);
Optional<CommunityBoard> findById(Long id);
List<CommunityBoard> getCommunityBoards();
List<CommunityBoard> findByWriterId(UUID writerId);
List<CommunityBoardView> getCommunityBoards();
List<CommunityBoardView> findByWriterId(UUID writerId);
void deleteAllInBatch();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.somemore.community.repository;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardView;
import com.somemore.community.domain.QCommunityBoard;
import com.somemore.volunteer.domain.QVolunteer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -34,29 +38,34 @@ public Optional<CommunityBoard> findById(Long id) {
}

@Override
public List<CommunityBoard> getCommunityBoards() {
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;

return queryFactory
.selectFrom(communityBoard)
.where(communityBoard.deleted.eq(false))
.orderBy(communityBoard.createdAt.desc())
public List<CommunityBoardView> getCommunityBoards() {
return getCommunityBoardsQuery()
.where(QCommunityBoard.communityBoard.deleted.eq(false))
.fetch();
}

@Override
public List<CommunityBoard> findByWriterId(UUID writerId) {
public List<CommunityBoardView> findByWriterId(UUID writerId) {
return getCommunityBoardsQuery()
.where(QCommunityBoard.communityBoard.writerId.eq(writerId)
.and(QCommunityBoard.communityBoard.deleted.eq(false)))
.fetch();
}

private JPAQuery<CommunityBoardView> getCommunityBoardsQuery() {
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
QVolunteer volunteer = QVolunteer.volunteer;

return queryFactory
.selectFrom(communityBoard)
.where(communityBoard.writerId.eq(writerId)
.and(communityBoard.deleted.eq(false))
)
.orderBy(communityBoard.createdAt.desc())
.fetch();
.select(Projections.constructor(CommunityBoardView.class,
communityBoard,
volunteer.nickname))
.from(communityBoard)
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
.orderBy(communityBoard.createdAt.desc());
}


@Override
public void deleteAllInBatch() {
communityBoardJpaRepository.deleteAllInBatch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,51 @@
package com.somemore.community.service;

import com.somemore.center.usecase.query.CenterQueryUseCase;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardView;
import com.somemore.community.dto.response.CommunityBoardGetDetailResponseDto;
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
import com.somemore.community.dto.response.WriterDetailDto;
import com.somemore.community.repository.CommunityBoardRepository;
import com.somemore.community.usecase.CommunityBoardQueryUseCase;
import com.somemore.global.exception.BadRequestException;
import com.somemore.volunteer.dto.response.VolunteerForCommunityResponseDto;
import com.somemore.volunteer.usecase.FindVolunteerIdUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.function.Function;

import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;


@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class CommunityBoardQueryService implements CommunityBoardQueryUseCase {

private final CommunityBoardRepository communityBoardRepository;
private final CenterQueryUseCase centerQueryUseCase;
private final FindVolunteerIdUseCase findVolunteerIdUseCase;

@Override
public List<CommunityBoardGetResponseDto> getCommunityBoards() {
List<CommunityBoard> boards = communityBoardRepository.getCommunityBoards();

return boards.stream()
.map(board -> {
String writerNickname = getWriterNickname(board.getWriterId());
return CommunityBoardGetResponseDto.fromEntity(board, writerNickname);
})
.toList();
List<CommunityBoardView> boards = communityBoardRepository.getCommunityBoards();
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
}

@Override
public List<CommunityBoardGetResponseDto> getCommunityBoardsByWriterId(UUID writerId) {
List<CommunityBoard> boards = communityBoardRepository.findByWriterId(writerId);
String writerNickname = getWriterNickname(writerId);

return boards.stream()
.map(board -> CommunityBoardGetResponseDto.fromEntity(board, writerNickname))
.toList();
List<CommunityBoardView> boards = communityBoardRepository.findByWriterId(writerId);
return mapEntitiesToDtos(boards, CommunityBoardGetResponseDto::fromEntity);
}

@Override
public CommunityBoardGetDetailResponseDto getCommunityBoardDetail(Long id) {
CommunityBoard board = communityBoardRepository.findById(id)
.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));

return CommunityBoardGetDetailResponseDto.fromEntity(board, getWriterDetail(board.getWriterId()));
return CommunityBoardGetDetailResponseDto.fromEntity(board);
}

private String getWriterNickname(UUID writerId) {
String nickname = findVolunteerIdUseCase.getNicknameById(writerId);

if (nickname == null) {
nickname = centerQueryUseCase.getNameById(writerId);
}
return nickname;
}

private WriterDetailDto getWriterDetail(UUID writerId) {
VolunteerForCommunityResponseDto volunteer = findVolunteerIdUseCase.getVolunteerDetailForCommunity(writerId);

if (volunteer == null) {
return centerQueryUseCase.getCenterDetailForCommunity(writerId);
}
return volunteer;
private <T, R> List<R> mapEntitiesToDtos(List<T> entities, Function<T, R> mapper) {
return entities.stream()
.map(mapper)
.toList();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
import java.util.UUID;

public interface VolunteerDetailRepository extends JpaRepository<VolunteerDetail, Long> {

Optional<VolunteerDetail> findByVolunteerId(UUID volunteerId);
}
Loading