Skip to content

Commit

Permalink
fix : 게시글 좋아요 오류 수정 (#80)
Browse files Browse the repository at this point in the history
* fix : 게시글 좋아요 오류 수정

* update : Idea 좋아요 복합키 -> 기본키로 변경
  • Loading branch information
dnd2dnd authored Apr 17, 2024
1 parent 2c6cb58 commit cc18253
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 78 deletions.
20 changes: 11 additions & 9 deletions src/main/java/kr/co/conceptbe/idea/application/IdeaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import kr.co.conceptbe.idea.domain.Hit;
import kr.co.conceptbe.idea.domain.Idea;
import kr.co.conceptbe.idea.domain.IdeaLike;
import kr.co.conceptbe.idea.domain.IdeaLikeID;
import kr.co.conceptbe.idea.domain.persistence.HitRepository;
import kr.co.conceptbe.idea.domain.persistence.IdeaLikesRepository;
import kr.co.conceptbe.idea.domain.persistence.IdeaRepository;
import kr.co.conceptbe.idea.dto.IdeaDetailResponse;
import kr.co.conceptbe.idea.dto.IdeaHitResponse;
import kr.co.conceptbe.idea.exception.IdeaLikeException;
import kr.co.conceptbe.idea.exception.AlreadyIdeaLikeException;
import kr.co.conceptbe.idea.exception.NotFoundIdeaLikeException;
import kr.co.conceptbe.member.domain.Member;
import kr.co.conceptbe.member.exception.UnAuthorizedMemberException;
import kr.co.conceptbe.member.persistence.MemberRepository;
Expand Down Expand Up @@ -138,23 +138,25 @@ public Long likesIdea(Long tokenMemberId, Long ideaId) {
Member member = memberRepository.getById(tokenMemberId);
Idea idea = ideaRepository.getById(ideaId);

IdeaLikeID ideaLikeID = IdeaLikeID.of(member, idea);
ideaLikesRepository.findById(ideaLikeID)
ideaLikesRepository.findByMemberAndIdea(member, idea)
.ifPresent(ideaLike -> {
throw new IdeaLikeException();
throw new AlreadyIdeaLikeException();
});

IdeaLike.createIdeaLikeAssociatedWithIdeaAndMember(idea, member);
IdeaLike ideaLike = IdeaLike.createAssociatedWithIdeaAndMember(idea, member);
ideaLikesRepository.save(ideaLike);

return idea.getId();
}

public void likesCancelIdea(Long tokenMemberId, Long ideaId) {
Member member = memberRepository.getById(tokenMemberId);
Idea idea = ideaRepository.getById(ideaId);
IdeaLikeID ideaLikeID = IdeaLikeID.of(member, idea);
ideaLikesRepository.getById(ideaLikeID);
ideaLikesRepository.deleteById(ideaLikeID);

ideaLikesRepository.findByMemberAndIdea(member, idea)
.orElseThrow(NotFoundIdeaLikeException::new);

ideaLikesRepository.deleteByMemberAndIdea(member, idea);
}

public FindIdeaWriteResponse getFindIdeaWriteResponse() {
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/kr/co/conceptbe/idea/domain/IdeaLike.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import static lombok.AccessLevel.PROTECTED;

import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
import java.io.Serializable;
import kr.co.conceptbe.common.entity.base.BaseTimeEntity;
import kr.co.conceptbe.member.domain.Member;
import lombok.Getter;
Expand All @@ -16,34 +16,31 @@
@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
public class IdeaLike extends BaseTimeEntity implements Serializable {
public class IdeaLike extends BaseTimeEntity {

@EmbeddedId
private IdeaLikeID ideaLikeID;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@MapsId("memberId")
@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@MapsId("ideaId")
@ManyToOne
@JoinColumn(name = "idea_id")
private Idea idea;

private IdeaLike(IdeaLikeID ideaLikeID, Member member, Idea idea) {
this.ideaLikeID = ideaLikeID;
private IdeaLike(Member member, Idea idea) {
this.member = member;
this.idea = idea;
}

public static IdeaLike of(IdeaLikeID ideaLikeID, Member member, Idea idea) {
return new IdeaLike(ideaLikeID, member, idea);
public static IdeaLike of(Member member, Idea idea) {
return new IdeaLike(member, idea);
}

public static IdeaLike createIdeaLikeAssociatedWithIdeaAndMember(Idea idea, Member member) {
IdeaLikeID ideaLikeID = IdeaLikeID.of(member, idea);
IdeaLike ideaLike = IdeaLike.of(ideaLikeID, member, idea);
public static IdeaLike createAssociatedWithIdeaAndMember(Idea idea, Member member) {
IdeaLike ideaLike = IdeaLike.of(member, idea);
idea.addIdeaLikes(ideaLike);
return ideaLike;
}
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/kr/co/conceptbe/idea/domain/IdeaLikeID.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package kr.co.conceptbe.idea.domain.persistence;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import kr.co.conceptbe.idea.domain.IdeaLikeID;
import kr.co.conceptbe.idea.domain.Idea;
import kr.co.conceptbe.idea.domain.IdeaLike;
import kr.co.conceptbe.member.domain.Member;

@Repository
public interface IdeaLikesRepository extends JpaRepository<IdeaLike, IdeaLikeID> {
public interface IdeaLikesRepository extends JpaRepository<IdeaLike, Long> {

Optional<IdeaLike> findByMemberAndIdea(Member member, Idea idea);

void deleteByMemberAndIdea(Member member, Idea idea);

default IdeaLike getById(IdeaLikeID ideaLikeID) {
return findById(ideaLikeID).orElseThrow(
() -> new IllegalArgumentException("Not Found ID : " + ideaLikeID));
default IdeaLike getById(Long id) {
return findById(id).orElseThrow(
() -> new IllegalArgumentException("Not Found ID : " + id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.co.conceptbe.idea.exception;

import org.springframework.http.HttpStatus;

import kr.co.conceptbe.common.exception.ConceptBeException;
import kr.co.conceptbe.common.exception.ErrorCode;

public class AlreadyIdeaLikeException extends ConceptBeException {

public AlreadyIdeaLikeException() { super(new ErrorCode(HttpStatus.CONFLICT, "해당 게시글을 이미 '좋아요'했습니다.")); }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kr.co.conceptbe.idea.exception;

import org.springframework.http.HttpStatus;

import kr.co.conceptbe.common.exception.ConceptBeException;
import kr.co.conceptbe.common.exception.ErrorCode;

public class NotFoundIdeaLikeException extends ConceptBeException {

public NotFoundIdeaLikeException() {
super(new ErrorCode(HttpStatus.NOT_FOUND, "해당 게시글은 '좋아요'하지 않았습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class IdeaServiceTest {
member);
CommentLike.createAssociatedWithMemberAndCreator(notCreator, parentComment);
Comment.createCommentAssociatedWithIdeaAndCreator("대댓글", parentComment, idea, member);
IdeaLike.createIdeaLikeAssociatedWithIdeaAndMember(idea, member);
IdeaLike.createAssociatedWithIdeaAndMember(idea, member);
Hit.ofIdeaAndMember(idea, member);
Bookmark.createBookmarkAssociatedWithIdeaAndMember(idea, member);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import kr.co.conceptbe.idea.application.request.FilteringRequest;
import kr.co.conceptbe.idea.domain.Idea;
import kr.co.conceptbe.idea.domain.IdeaLike;
import kr.co.conceptbe.idea.domain.IdeaLikeID;
import kr.co.conceptbe.idea.domain.persistence.IdeaLikesRepository;
import kr.co.conceptbe.idea.domain.persistence.IdeaRepository;
import kr.co.conceptbe.idea.fixture.IdeaFixture;
Expand Down Expand Up @@ -171,9 +170,7 @@ class IdeaRepositoryTest {
region, List.of(branch), List.of(purpose),
List.of(skillCategory), member
));
ideaLikesRepository.save(IdeaLike.of(
IdeaLikeID.of(member, result), member, result
));
ideaLikesRepository.save(IdeaLike.of(member, result));

// when
List<Idea> queryResults = ideaRepository.findAllByOrderByLikesDesc(
Expand Down Expand Up @@ -209,9 +206,7 @@ class IdeaRepositoryTest {
region, List.of(branch1, branch3), List.of(purpose),
List.of(skillCategory1, skillCategory2), member
));
ideaLikesRepository.save(IdeaLike.of(
IdeaLikeID.of(member, result1), member, result1
));
ideaLikesRepository.save(IdeaLike.of(member, result1));
Idea notInquiry1 = ideaRepository.save(IdeaFixture.createIdea(
region, List.of(branch2), List.of(purpose),
List.of(skillCategory1, skillCategory2), member
Expand Down Expand Up @@ -257,7 +252,7 @@ class IdeaRepositoryTest {
region, List.of(branch1), List.of(purpose), List.of(), member
));
ideaLikesRepository.save(
IdeaLike.createIdeaLikeAssociatedWithIdeaAndMember(result1, member));
IdeaLike.createAssociatedWithIdeaAndMember(result1, member));

// when
List<Idea> queryResults = ideaRepository.findAllByOrderByLikesDesc(
Expand Down

0 comments on commit cc18253

Please sign in to comment.