-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from ttoklip/Feat/12-뉴스레터-스크랩-api
pr Feat/12 뉴스레터 좋아요, 스크랩 기능 구현
- Loading branch information
Showing
35 changed files
with
854 additions
and
109 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/api/ttoklip/domain/honeytip/Scrap/domain/HoneyTipScrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.api.ttoklip.domain.honeytip.Scrap.domain; | ||
|
||
import com.api.ttoklip.domain.common.base.BaseTimeEntity; | ||
import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip; | ||
import com.api.ttoklip.domain.member.domain.Member; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; | ||
|
||
@Getter | ||
@Builder | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class HoneyTipScrap extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", updatable = false) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "honey_tip_id") | ||
private HoneyTip honeyTip; | ||
|
||
public static HoneyTipScrap from(final HoneyTip honeyTip) { | ||
return HoneyTipScrap.builder() | ||
.member(getCurrentMember()) | ||
.honeyTip(honeyTip) | ||
.build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/api/ttoklip/domain/honeytip/Scrap/repository/HoneyTipScrapRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.api.ttoklip.domain.honeytip.Scrap.repository; | ||
|
||
import com.api.ttoklip.domain.honeytip.Scrap.domain.HoneyTipScrap; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface HoneyTipScrapRepository extends JpaRepository<HoneyTipScrap, Long>, HoneyTipScrapRepositoryCustom { | ||
|
||
Optional<HoneyTipScrap> findByHoneyTipIdAndMemberId(Long honeyTipId, Long memberId); | ||
boolean existsByHoneyTipIdAndMemberId(Long honeyTipId, Long memberId); | ||
|
||
} | ||
|
7 changes: 7 additions & 0 deletions
7
.../java/com/api/ttoklip/domain/honeytip/Scrap/repository/HoneyTipScrapRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.api.ttoklip.domain.honeytip.Scrap.repository; | ||
|
||
public interface HoneyTipScrapRepositoryCustom { | ||
|
||
Long countHoneyTipScrapsByHoneyTipId(final Long honeyTipId); | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/api/ttoklip/domain/honeytip/Scrap/service/HoneyTipScrapService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.api.ttoklip.domain.honeytip.Scrap.service; | ||
|
||
import com.api.ttoklip.domain.honeytip.Scrap.domain.HoneyTipScrap; | ||
import com.api.ttoklip.domain.honeytip.Scrap.repository.HoneyTipScrapRepository; | ||
import com.api.ttoklip.domain.honeytip.like.domain.HoneyTipLike; | ||
import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip; | ||
import com.api.ttoklip.domain.honeytip.post.service.HoneyTipCommonService; | ||
import com.api.ttoklip.global.exception.ApiException; | ||
import com.api.ttoklip.global.exception.ErrorType; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class HoneyTipScrapService { | ||
|
||
private final HoneyTipScrapRepository honeyTipScrapRepository; | ||
private final HoneyTipCommonService honeyTipCommonService; | ||
|
||
// 스크랩 생성 | ||
public void registerScrap(final Long honeyTipId) { | ||
Long currentMemberId = getCurrentMember().getId(); | ||
boolean exists = honeyTipScrapRepository.existsByHoneyTipIdAndMemberId(honeyTipId, currentMemberId); | ||
if (exists) { | ||
return; // 이미 스크랩이 존재하면 좋아요를 생성하지 않고 return | ||
} | ||
|
||
HoneyTip findHoneyTip = honeyTipCommonService.getHoneytip(honeyTipId); | ||
HoneyTipScrap honeyTipScrap = HoneyTipScrap.from(findHoneyTip); | ||
honeyTipScrapRepository.save(honeyTipScrap); | ||
} | ||
|
||
// 스크랩 취소 | ||
public void cancelScrap(final Long honeyTipId) { | ||
// HoneyTipId (게시글 ID) | ||
HoneyTip findHoneyTip = honeyTipCommonService.getHoneytip(honeyTipId); | ||
Long findHoneyTipId = findHoneyTip.getId(); | ||
Long currentMemberId = getCurrentMember().getId(); | ||
|
||
HoneyTipScrap honeyTipScrap = honeyTipScrapRepository.findByHoneyTipIdAndMemberId(findHoneyTipId, currentMemberId) | ||
.orElseThrow(() -> new ApiException(ErrorType.SCRAP_NOT_FOUND)); | ||
|
||
// 자격 검증: 이 단계에서는 findByHoneyTipIdAndMemberId 결과가 존재하므로, 현재 사용자가 스크랩을 누른 것입니다. | ||
// 별도의 자격 검증 로직이 필요 없으며, 바로 삭제를 진행할 수 있습니다. | ||
honeyTipScrapRepository.deleteById(honeyTipScrap.getId()); | ||
} | ||
|
||
public Long countHoneyTipScraps(final Long honeyTipId) { | ||
return honeyTipScrapRepository.countHoneyTipScrapsByHoneyTipId(honeyTipId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.