Skip to content

Commit

Permalink
[JT- 63] 회차 도메인 서비스 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin authored Sep 12, 2023
2 parents a2f78ca + 40275cb commit 31841c7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.devtoon.jtoon.webtoon.response.EpisodesRes;
import com.devtoon.jtoon.webtoon.response.WebtoonInfoRes;
import com.devtoon.jtoon.webtoon.response.WebtoonItemRes;
import com.devtoon.jtoon.webtoon.service.EpisodeDomainService;
import com.devtoon.jtoon.webtoon.service.WebtoonDomainService;

import lombok.RequiredArgsConstructor;
Expand All @@ -33,18 +34,15 @@
public class WebtoonApplicationService {

private final WebtoonDomainService webtoonDomainService;
private final EpisodeDomainService episodeDomainService;
private final S3Service s3Service;

@Transactional
public void createWebtoon(MultipartFile thumbnailImage, CreateWebtoonReq request) {
Member member = MemberThreadLocal.getMember();
webtoonDomainService.validateDuplicateTitle(request.title());
String thumbnailUrl = s3Service.upload(UploadImageReq.builder()
.imageType(WEBTOON_THUMBNAIL)
.webtoonTitle(request.title())
.fileName(FileName.forWebtoon())
.image(thumbnailImage)
.build()
String thumbnailUrl = s3Service.upload(
UploadImageReq.of(WEBTOON_THUMBNAIL, request.title(), FileName.forWebtoon(), thumbnailImage)
);
webtoonDomainService.createWebtoon(member, thumbnailUrl, request);
}
Expand All @@ -59,21 +57,13 @@ public void createEpisode(
Member member = MemberThreadLocal.getMember();
Webtoon webtoon = webtoonDomainService.getWebtoonById(webtoonId);
webtoon.validateAuthor(member.getId());
String mainUrl = s3Service.upload(UploadImageReq.builder()
.imageType(EPISODE_MAIN)
.webtoonTitle(webtoon.getTitle())
.fileName(FileName.forEpisode(request.no()))
.image(mainImage)
.build()
String mainUrl = s3Service.upload(
UploadImageReq.of(EPISODE_MAIN, webtoon.getTitle(), FileName.forEpisode(request.no()), mainImage)
);
String thumbnailUrl = s3Service.upload(UploadImageReq.builder()
.imageType(EPISODE_THUMBNAIL)
.webtoonTitle(webtoon.getTitle())
.fileName(FileName.forEpisode(request.no()))
.image(thumbnailImage)
.build()
String thumbnailUrl = s3Service.upload(
UploadImageReq.of(EPISODE_THUMBNAIL, webtoon.getTitle(), FileName.forEpisode(request.no()), thumbnailImage)
);
webtoonDomainService.createEpisode(webtoon, mainUrl, thumbnailUrl, request);
episodeDomainService.createEpisode(webtoon, mainUrl, thumbnailUrl, request);
}

public Map<DayOfWeek, List<WebtoonItemRes>> getWebtoons(GetWebtoonsReq request) {
Expand All @@ -85,16 +75,16 @@ public WebtoonInfoRes getWebtoon(Long webtoonId) {
}

public List<EpisodesRes> getEpisodes(Long webtoonId, CustomPageRequest request) {
return webtoonDomainService.getEpisodes(webtoonId, request);
return episodeDomainService.getEpisodes(webtoonId, request);
}

public EpisodeRes getEpisode(Long episodeId) {
return webtoonDomainService.getEpisode(episodeId);
return episodeDomainService.getEpisode(episodeId);
}

@Transactional
public void purchaseEpisode(Long episodeId) {
Member member = MemberThreadLocal.getMember();
webtoonDomainService.purchaseEpisode(member, episodeId);
episodeDomainService.purchaseEpisode(member, episodeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public record UploadImageReq(
MultipartFile image
) {

public static UploadImageReq of(ImageType imageType, String webtoonTitle, FileName fileName, MultipartFile image) {
return UploadImageReq.builder()
.imageType(imageType)
.webtoonTitle(webtoonTitle)
.fileName(fileName)
.image(image)
.build();
}

public String toKey() {
return imageType.getPath(webtoonTitle, fileName.getValue());
}
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,19 @@

import com.devtoon.jtoon.error.exception.DuplicatedException;
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.DayOfWeekWebtoon;
import com.devtoon.jtoon.webtoon.entity.Episode;
import com.devtoon.jtoon.webtoon.entity.GenreWebtoon;
import com.devtoon.jtoon.webtoon.entity.PurchasedEpisode;
import com.devtoon.jtoon.webtoon.entity.Webtoon;
import com.devtoon.jtoon.webtoon.entity.enums.DayOfWeek;
import com.devtoon.jtoon.webtoon.repository.DayOfWeekWebtoonRepository;
import com.devtoon.jtoon.webtoon.repository.EpisodeRepository;
import com.devtoon.jtoon.webtoon.repository.EpisodeSearchRepository;
import com.devtoon.jtoon.webtoon.repository.GenreWebtoonRepository;
import com.devtoon.jtoon.webtoon.repository.PurchasedEpisodeRepository;
import com.devtoon.jtoon.webtoon.repository.WebtoonRepository;
import com.devtoon.jtoon.webtoon.repository.WebtoonSearchRepository;
import com.devtoon.jtoon.webtoon.request.CreateEpisodeReq;
import com.devtoon.jtoon.webtoon.request.CreateWebtoonReq;
import com.devtoon.jtoon.webtoon.request.GetWebtoonsReq;
import com.devtoon.jtoon.webtoon.response.EpisodeRes;
import com.devtoon.jtoon.webtoon.response.EpisodesRes;
import com.devtoon.jtoon.webtoon.response.GenreRes;
import com.devtoon.jtoon.webtoon.response.WebtoonInfoRes;
import com.devtoon.jtoon.webtoon.response.WebtoonItemRes;
Expand All @@ -48,10 +39,6 @@ public class WebtoonDomainService {
private final WebtoonSearchRepository webtoonSearchRepository;
private final DayOfWeekWebtoonRepository dayOfWeekWebtoonRepository;
private final GenreWebtoonRepository genreWebtoonRepository;
private final EpisodeRepository episodeRepository;
private final EpisodeSearchRepository episodeSearchRepository;
private final PurchasedEpisodeRepository purchasedEpisodeRepository;
private final MemberCookieRepository memberCookieRepository;

@Transactional
public void createWebtoon(Member member, String thumbnailUrl, CreateWebtoonReq request) {
Expand All @@ -63,12 +50,6 @@ public void createWebtoon(Member member, String thumbnailUrl, CreateWebtoonReq r
genreWebtoonRepository.saveAll(genreWebtoons);
}

@Transactional
public void createEpisode(Webtoon webtoon, String mainUrl, String thumbnailUrl, CreateEpisodeReq request) {
Episode episode = request.toEntity(webtoon, mainUrl, thumbnailUrl);
episodeRepository.save(episode);
}

public Map<DayOfWeek, List<WebtoonItemRes>> getWebtoons(GetWebtoonsReq request) {
return webtoonSearchRepository.findWebtoons(request.day(), request.keyword())
.stream()
Expand All @@ -86,30 +67,6 @@ public WebtoonInfoRes getWebtoon(Long webtoonId) {
return WebtoonInfoRes.of(webtoon, dayOfWeeks, genres);
}

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()
);
}

public void validateDuplicateTitle(String title) {
if (webtoonRepository.existsByTitle(title)) {
throw new DuplicatedException(WEBTOON_TITLE_DUPLICATED);
Expand All @@ -121,16 +78,6 @@ public Webtoon getWebtoonById(Long webtoonId) {
.orElseThrow(() -> new NotFoundException(WEBTOON_NOT_FOUND));
}

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));
}

private List<String> getDayOfWeeks(Long webtoonId) {
return dayOfWeekWebtoonRepository.findById(webtoonId)
.stream()
Expand Down

0 comments on commit 31841c7

Please sign in to comment.