Skip to content
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

[fix] 게시글 조회 데이터 추가 및 scrap 삭제시 오류 해결 #72

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,34 @@
@Builder
public record PostDetailResponseDto(
Long memberId,
String title,
String content,
String name,
String major,
String profile,
String gender,
String mbti,
String roomType,
String dormitoryType,
String importantKeyType,
String similarityKeyType,
boolean isScrap,
CheckListResponseDto checkList
) {
public static PostDetailResponseDto of(PostDetailDto postDetailDto, CheckListResponseDto checkList, boolean isScrap) {
return PostDetailResponseDto.builder()
.memberId(postDetailDto.memberId())
.title(postDetailDto.title())
.content(postDetailDto.content())
.name(postDetailDto.name())
.major(postDetailDto.major())
.profile(postDetailDto.profile())
.gender(postDetailDto.gender().getDesc())
.roomType(postDetailDto.roomType().getDesc())
.dormitoryType(postDetailDto.dormitoryType().getDesc())
.mbti(postDetailDto.mbti().getDesc())
.importantKeyType(postDetailDto.importantKeyType().getDesc())
.similarityKeyType(postDetailDto.similarityKeyType().getDesc())
.isScrap(isScrap)
.checkList(checkList)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@
import org.gachon.checkmate.domain.member.entity.MbtiType;
import org.gachon.checkmate.domain.member.entity.ProfileImageType;
import org.gachon.checkmate.domain.post.entity.DormitoryType;
import org.gachon.checkmate.domain.post.entity.ImportantKeyType;
import org.gachon.checkmate.domain.post.entity.RoomType;
import org.gachon.checkmate.domain.post.entity.SimilarityKeyType;

public record PostDetailDto(
Long memberId,
String title,
String content,
String major,
MbtiType mbti,
GenderType gender,
RoomType roomType,
DormitoryType dormitoryType,
String name,
String profile,
ImportantKeyType importantKeyType,
SimilarityKeyType similarityKeyType,
PostCheckList postCheckList
) {
@QueryProjection
public PostDetailDto(Long memberId, String major, MbtiType mbti, GenderType gender, RoomType roomType, DormitoryType dormitoryType, String name, String profile, PostCheckList postCheckList) {
public PostDetailDto(Long memberId, String title, String content, String major, MbtiType mbti,
GenderType gender, RoomType roomType, DormitoryType dormitoryType, String name,
String profile, ImportantKeyType importantKeyType, SimilarityKeyType similarityKeyType, PostCheckList postCheckList) {
this.memberId = memberId;
this.title = title;
this.content = content;
this.major = major;
this.mbti = mbti;
this.gender = gender;
this.roomType = roomType;
this.dormitoryType = dormitoryType;
this.name = name;
this.profile = profile;
this.importantKeyType = importantKeyType;
this.similarityKeyType = similarityKeyType;
this.postCheckList = postCheckList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ public Optional<PostDetailDto> findPostDetail(Long postId) {
return Optional.ofNullable(queryFactory
.select(new QPostDetailDto(
user.id,
post.title,
post.content,
user.major,
user.mbtiType,
user.gender,
post.roomType,
post.dormitoryType,
user.name,
user.profile,
post.importantKeyType,
post.similarityKeyType,
post.postCheckList
))
.from(post)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public ResponseEntity<SuccessResponse<?>> createScrapPost(@UserId final Long use

@DeleteMapping("/{id}")
public ResponseEntity<SuccessResponse<?>> deleteScrapPost(@UserId final Long userId,
@PathVariable("id") final Long scrapId) {
scrapService.deleteScrapPost(userId, scrapId);
@PathVariable("id") final Long postId) {
scrapService.deleteScrapPost(userId, postId);
return SuccessResponse.ok(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface ScrapRepository extends JpaRepository<Scrap, Long>, ScrapCustomRepository {
void deleteByIdAndUserId(Long scrapId, Long userId);
void deleteByPostIdAndUserId(Long postId, Long userId);
boolean existsByPostIdAndUserId(Long postId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.gachon.checkmate.domain.scrap.dto.support.ScrapSearchCondition;
import org.gachon.checkmate.domain.scrap.entity.Scrap;
import org.gachon.checkmate.domain.scrap.repository.ScrapRepository;
import org.gachon.checkmate.global.error.exception.ConflictException;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.error.exception.InvalidValueException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -48,11 +50,13 @@ public PostSearchResponseDto getScrapPosts(Long userId, Pageable pageable) {
public void creatScrapPost(Long userId, ScrapRequestDto scrapRequestDto) {
User user = findUserOrThrow(userId);
Post post = findPostOrThrow(scrapRequestDto.postId());
validateDuplicateScrap(post.getId(), user.getId());
createScrapAndSave(user, post);
}

public void deleteScrapPost(Long userId, Long scrapId) {
scrapRepository.deleteByIdAndUserId(scrapId, userId);
public void deleteScrapPost(Long userId, Long postId) {
validateExistScrap(postId, userId);
scrapRepository.deleteByPostIdAndUserId(postId, userId);
}

private List<PostSearchElementResponseDto> createPostSearchResponseDto(Page<PostSearchDto> postSearchDtoList, CheckList checkList) {
Expand All @@ -74,6 +78,20 @@ private void createScrapAndSave(User user, Post post) {
scrapRepository.save(scrap);
}

private void validateDuplicateScrap(Long postId, Long userId) {
if(existPostInScrap(postId, userId))
throw new ConflictException(DUPLICATE_SCRAP);
}

private void validateExistScrap(Long postId, Long userId) {
if(!existPostInScrap(postId, userId))
throw new InvalidValueException(INVALID_DELETE_SCRAP);
}

private boolean existPostInScrap(Long postId, Long userId) {
return scrapRepository.existsByPostIdAndUserId(postId, userId);
}

private CheckList getCheckList(Long userId) {
return checkListRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException(CHECK_LIST_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum ErrorCode {
INVALID_PASSWORD(HttpStatus.BAD_REQUEST, "비밀번호는 8~20자 대소문자 영문, 숫자, 특수문자의 조합이어야 합니다."),
INVALID_POST_TITLE(HttpStatus.BAD_REQUEST, "이미 존재하는 게시물입니다."),
INVALID_POST_DATE(HttpStatus.BAD_REQUEST, "마감시간이 지난 게시물입니다."),
INVALID_DELETE_SCRAP(HttpStatus.BAD_REQUEST, "등록되지 않은 스크랩입니다."),

/**
* 401 Unauthorized
Expand Down Expand Up @@ -59,6 +60,7 @@ public enum ErrorCode {
*/
CONFLICT(HttpStatus.CONFLICT, "이미 존재하는 리소스입니다."),
DUPLICATE_EMAIL(HttpStatus.CONFLICT, "이미 가입된 이메일입니다."),
DUPLICATE_SCRAP(HttpStatus.CONFLICT, "이미 등록된 스크랩입니다."),
DUPLICATE_CHECK_LIST(HttpStatus.CONFLICT, "체크리스트가 이미 존재합니다."),
DUPLICATE_POST_REPORT(HttpStatus.CONFLICT, "이전에 신고했던 게시물입니다."),
DUPLICATE_CHATROOM_REPORT(HttpStatus.CONFLICT, "이전에 신고했던 채팅방입니다."),
Expand Down
Loading