-
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.
fix: 뉴스레터 스크랩 로직 추가 & Member와 연관관계 매핑 (#63)
- Loading branch information
1 parent
4fe61e6
commit a687c5f
Showing
10 changed files
with
180 additions
and
48 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/api/ttoklip/domain/newsletter/post/service/NewsletterCommonService.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,44 @@ | ||
package com.api.ttoklip.domain.newsletter.post.service; | ||
|
||
import com.api.ttoklip.domain.newsletter.post.domain.Newsletter; | ||
import com.api.ttoklip.domain.newsletter.post.repository.NewsletterRepository; | ||
import com.api.ttoklip.global.exception.ApiException; | ||
import com.api.ttoklip.global.exception.ErrorType; | ||
import com.api.ttoklip.global.s3.S3FileUploader; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class NewsletterCommonService { | ||
|
||
private final S3FileUploader s3FileUploader; | ||
private final NewsletterRepository newsletterRepository; | ||
|
||
/* -------------------------------------------- COMMON -------------------------------------------- */ | ||
public Newsletter getNewsletter(final Long postId) { | ||
return newsletterRepository.findByIdActivated(postId); | ||
} | ||
|
||
public List<String> uploadImages(final List<MultipartFile> uploadImages) { | ||
return s3FileUploader.uploadMultipartFiles(uploadImages); | ||
} | ||
|
||
public void checkEditPermission(final Newsletter newsletter) { | ||
Long writerId = newsletter.getMember().getId(); | ||
Long currentMemberId = getCurrentMember().getId(); | ||
|
||
if (!writerId.equals(currentMemberId)) { | ||
throw new ApiException(ErrorType.UNAUTHORIZED_EDIT_POST); | ||
} | ||
} | ||
|
||
/* -------------------------------------------- COMMON 끝 -------------------------------------------- */ | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...in/java/com/api/ttoklip/domain/newsletter/scarp/repository/NewsletterScrapRepository.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,12 @@ | ||
package com.api.ttoklip.domain.newsletter.scarp.repository; | ||
|
||
import com.api.ttoklip.domain.newsletter.scarp.entity.NewsletterScrap; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface NewsletterScrapRepository extends JpaRepository<NewsletterScrap, Long>, NewsletterScrapRepositoryCustom { | ||
Optional<NewsletterScrap> findByNewsletterIdAndMemberId(Long newsletterId, Long memberId); | ||
|
||
boolean existsByNewsletterIdAndMemberId(Long postId, Long memberId); | ||
} |
6 changes: 6 additions & 0 deletions
6
...a/com/api/ttoklip/domain/newsletter/scarp/repository/NewsletterScrapRepositoryCustom.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,6 @@ | ||
package com.api.ttoklip.domain.newsletter.scarp.repository; | ||
|
||
public interface NewsletterScrapRepositoryCustom { | ||
|
||
Long countNewsletterScrapsByNewsletterId(final Long newsletterId); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/api/ttoklip/domain/newsletter/scarp/service/NewsletterScrapService.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,50 @@ | ||
package com.api.ttoklip.domain.newsletter.scarp.service; | ||
|
||
import com.api.ttoklip.domain.newsletter.post.domain.Newsletter; | ||
import com.api.ttoklip.domain.newsletter.post.service.NewsletterCommonService; | ||
import com.api.ttoklip.domain.newsletter.scarp.entity.NewsletterScrap; | ||
import com.api.ttoklip.domain.newsletter.scarp.repository.NewsletterScrapRepository; | ||
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 NewsletterScrapService { | ||
|
||
private final NewsletterScrapRepository newsletterScrapRepository; | ||
private final NewsletterCommonService newsletterCommonService; | ||
|
||
// 스크랩 생성 | ||
public void registerScrap(final Long newsletterId) { | ||
Long currentMemberId = getCurrentMember().getId(); | ||
boolean exists = newsletterScrapRepository.existsByNewsletterIdAndMemberId(newsletterId, currentMemberId); | ||
if (exists) { | ||
return; // 이미 스크랩이 존재하면 스크랩을 생성하지 않고 return | ||
} | ||
Newsletter findNewsletter = newsletterCommonService.getNewsletter(newsletterId); | ||
NewsletterScrap newsletterScrap = NewsletterScrap.from(findNewsletter); | ||
newsletterScrapRepository.save(newsletterScrap); | ||
} | ||
|
||
// 스크랩 취소 | ||
public void cancelScrap(final Long newsletterId) { | ||
// HoneyTipId (게시글 ID) | ||
Newsletter findNewsletter = newsletterCommonService.getNewsletter(newsletterId); | ||
Long findNewsletterId = findNewsletter.getId(); | ||
Long currentMemberId = getCurrentMember().getId(); | ||
NewsletterScrap newsletterScrap = newsletterScrapRepository.findByNewsletterIdAndMemberId(findNewsletterId, currentMemberId) | ||
.orElseThrow(() -> new ApiException(ErrorType.SCRAP_NOT_FOUND)); | ||
// 자격 검증: 이 단계에서는 findByHoneyTipIdAndMemberId 결과가 존재하므로, 현재 사용자가 좋아요를 누른 것입니다. | ||
// 별도의 자격 검증 로직이 필요 없으며, 바로 삭제를 진행할 수 있습니다. | ||
newsletterScrapRepository.deleteById(newsletterScrap.getId()); | ||
} | ||
public Long countNewsletterScraps(final Long newsletterId) { | ||
return newsletterScrapRepository.countNewsletterScrapsByNewsletterId(newsletterId); | ||
} | ||
} |
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