-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
75 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
76 changes: 76 additions & 0 deletions
76
module-domain/src/main/java/com/devtoon/jtoon/webtoon/service/EpisodeDomainService.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,76 @@ | ||
package com.devtoon.jtoon.webtoon.service; | ||
|
||
import static com.devtoon.jtoon.error.model.ErrorStatus.*; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.devtoon.jtoon.error.exception.NotFoundException; | ||
import com.devtoon.jtoon.global.util.CustomPageRequest; | ||
import com.devtoon.jtoon.member.entity.Member; | ||
import com.devtoon.jtoon.member.entity.MemberCookie; | ||
import com.devtoon.jtoon.payment.repository.MemberCookieRepository; | ||
import com.devtoon.jtoon.webtoon.entity.Episode; | ||
import com.devtoon.jtoon.webtoon.entity.PurchasedEpisode; | ||
import com.devtoon.jtoon.webtoon.entity.Webtoon; | ||
import com.devtoon.jtoon.webtoon.repository.EpisodeRepository; | ||
import com.devtoon.jtoon.webtoon.repository.EpisodeSearchRepository; | ||
import com.devtoon.jtoon.webtoon.repository.PurchasedEpisodeRepository; | ||
import com.devtoon.jtoon.webtoon.request.CreateEpisodeReq; | ||
import com.devtoon.jtoon.webtoon.response.EpisodeRes; | ||
import com.devtoon.jtoon.webtoon.response.EpisodesRes; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class EpisodeDomainService { | ||
|
||
private final EpisodeRepository episodeRepository; | ||
private final EpisodeSearchRepository episodeSearchRepository; | ||
private final PurchasedEpisodeRepository purchasedEpisodeRepository; | ||
private final MemberCookieRepository memberCookieRepository; | ||
|
||
@Transactional | ||
public void createEpisode(Webtoon webtoon, String mainUrl, String thumbnailUrl, CreateEpisodeReq request) { | ||
Episode episode = request.toEntity(webtoon, mainUrl, thumbnailUrl); | ||
episodeRepository.save(episode); | ||
} | ||
|
||
public List<EpisodesRes> getEpisodes(Long webtoonId, CustomPageRequest request) { | ||
return episodeSearchRepository.getEpisodes(webtoonId, request) | ||
.stream() | ||
.map(EpisodesRes::from) | ||
.toList(); | ||
} | ||
|
||
public EpisodeRes getEpisode(Long episodeId) { | ||
Episode episode = getEpisodeById(episodeId); | ||
return EpisodeRes.from(episode); | ||
} | ||
|
||
@Transactional | ||
public void purchaseEpisode(Member member, Long episodeId) { | ||
Episode episode = getEpisodeById(episodeId); | ||
MemberCookie memberCookie = getMemberCookieById(member.getId()); | ||
memberCookie.decreaseCookieCount(episode.getCookieCount()); | ||
purchasedEpisodeRepository.save(PurchasedEpisode.builder() | ||
.member(member) | ||
.episode(episode) | ||
.build() | ||
); | ||
} | ||
|
||
private Episode getEpisodeById(Long episodeId) { | ||
return episodeRepository.findById(episodeId) | ||
.orElseThrow(() -> new NotFoundException(EPISODE_NOT_FOUND)); | ||
} | ||
|
||
private MemberCookie getMemberCookieById(Long memberId) { | ||
return memberCookieRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundException(MEMBER_COOKIE_NOT_FOUND)); | ||
} | ||
} |
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