Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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,11 @@
package com.somemore.community.domain;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class CommunityBoardWithNickname {
Copy link
Collaborator

Choose a reason for hiding this comment

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

레코드는 어떨까요?

Copy link
Collaborator

Choose a reason for hiding this comment

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

그리고 이것 자체가 DTO 같은데 추가로 클래스를 만들지 않았다는 PR 포인트가 이해가 잘 안되는데 추가적인 설명을 부탁드리고 싶어요!

Copy link
Collaborator Author

@ayoung-dev ayoung-dev Nov 27, 2024

Choose a reason for hiding this comment

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

추가한 클래스는 repository-service에서 사용하는 엔티티? 개념으로 생각했습니다.
그래서 다른 DTO들과는 차이점이 있다고 생각해서 구분지어 말하려했는데 명확하지 않았던 거 같네요
네이밍을 다르게 해야할 거 같은데 뭐가 좋을지 고민이 되네요..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

CommunityBoardView를 추천해주는데
다들 어떠신지 이거 보시면 남겨주시면 감사하겠습니다

Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 찾아봤는데 마땅히 끌리는게 없어서 아영 님 진행하는대로 따라가게씁니다

Copy link
Collaborator

Choose a reason for hiding this comment

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

repository-service에서 사용하는 엔티티라면 영속성 레이어의 엔티티라고 보면 되는걸까요?
그럼 서비스로 넘길때 뭔가 비즈니스 레이어에서 사용하는 형태로 변형시켜줘야 되는지도 궁금해요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

DB 테이블이랑 직접 매핑되지 않기도 하고 객체 같은 느낌이라 영속성 엔티티라고 보긴 어려울 거 같아요...
그럼.. repository-service단에서 사용하는 DTO라고 하는게 맞을까요?

지금은 기존 entity-dto 처럼 서비스에서 dto로 변환하고 있습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

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

저는 지금 괜찮아보입니다.!
서로다른 두가지 테이블에 있는 데이터를 묶어주기 위한 엔티티로 보이네요

private final CommunityBoard communityBoard;
private final 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.CommunityBoardWithNickname;

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(CommunityBoardWithNickname board) {
return new CommunityBoardGetResponseDto(
board.getId(),
board.getTitle(),
writerNickname,
board.getCreatedAt()
board.getCommunityBoard().getId(),
board.getCommunityBoard().getTitle(),
board.getWriterNickname(),
board.getCommunityBoard().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.CommunityBoardWithNickname;

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<CommunityBoardWithNickname> getCommunityBoards();
List<CommunityBoardWithNickname> findByWriterId(UUID writerId);
void deleteAllInBatch();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.somemore.community.repository;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardWithNickname;
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,22 +37,32 @@ public Optional<CommunityBoard> findById(Long id) {
}

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

return queryFactory
.selectFrom(communityBoard)
.select(Projections.constructor(CommunityBoardWithNickname.class,
communityBoard,
volunteer.nickname))
Copy link
Collaborator

Choose a reason for hiding this comment

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

감사합니다... 이 방법 전 좋아요

.from(communityBoard)
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
.where(communityBoard.deleted.eq(false))
.orderBy(communityBoard.createdAt.desc())
.fetch();
}

@Override
public List<CommunityBoard> findByWriterId(UUID writerId) {
public List<CommunityBoardWithNickname> findByWriterId(UUID writerId) {
QCommunityBoard communityBoard = QCommunityBoard.communityBoard;
QVolunteer volunteer = QVolunteer.volunteer;

return queryFactory
.selectFrom(communityBoard)
.select(Projections.constructor(CommunityBoardWithNickname.class,
communityBoard,
volunteer.nickname))
.from(communityBoard)
.join(volunteer).on(communityBoard.writerId.eq(volunteer.id))
.where(communityBoard.writerId.eq(writerId)
.and(communityBoard.deleted.eq(false))
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.somemore.community.service;

import com.somemore.center.usecase.query.CenterQueryUseCase;
import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.domain.CommunityBoardWithNickname;
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.query.FindVolunteerIdUseCase;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -26,54 +23,29 @@
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();
List<CommunityBoardWithNickname> boards = communityBoardRepository.getCommunityBoards();

return boards.stream()
.map(board -> {
String writerNickname = getWriterNickname(board.getWriterId());
return CommunityBoardGetResponseDto.fromEntity(board, writerNickname);
})
.map(CommunityBoardGetResponseDto::fromEntity)
.toList();
}

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

return boards.stream()
.map(board -> CommunityBoardGetResponseDto.fromEntity(board, writerNickname))
.map(CommunityBoardGetResponseDto::fromEntity)
.toList();
}

@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()));
}

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;
return CommunityBoardGetDetailResponseDto.fromEntity(board);
}
}

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import java.util.UUID;

public interface VolunteerJpaRepository extends JpaRepository<Volunteer, Long> {
Volunteer findById(UUID id);
Optional<Volunteer> findByOauthId(String oauthId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@Repository
public interface VolunteerRepository {
Volunteer save(Volunteer volunteer);
Volunteer findById(UUID id);
String findNicknameById(UUID id);
Optional<Volunteer> findByOauthId(String oauthId);
void deleteAllInBatch();
Expand Down
Loading