-
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
9 changed files
with
178 additions
and
12 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
19 changes: 19 additions & 0 deletions
19
...e-application/src/main/java/com/devtoon/jtoon/webtoon/presentation/WebtoonController.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 |
---|---|---|
@@ -1,9 +1,28 @@ | ||
package com.devtoon.jtoon.webtoon.presentation; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.devtoon.jtoon.webtoon.application.WebtoonService; | ||
import com.devtoon.jtoon.webtoon.request.CreateEpisodeReq; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/webtoons") | ||
public class WebtoonController { | ||
|
||
private final WebtoonService webtoonService; | ||
|
||
@PostMapping("/{webtoonId}") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public void createEpisode(@PathVariable Long webtoonId, @Valid CreateEpisodeReq req) { | ||
webtoonService.createEpisode(webtoonId, req); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
module-domain-s3/src/main/java/com/devtoon/jtoon/application/S3Service.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,29 @@ | ||
package com.devtoon.jtoon.application; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.devtoon.jtoon.common.FileName; | ||
import com.devtoon.jtoon.common.ImageType; | ||
import com.devtoon.jtoon.util.S3Uploader; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
private final S3Uploader s3Uploader; | ||
|
||
@Value("${spring.cloud.aws.cloud-front.url}") | ||
private String IMAGE_URL; | ||
|
||
//TODO 확장자 포함 | ||
public String upload(ImageType imageType, String webtoonTitle, FileName fileName, MultipartFile image) { | ||
String key = imageType.getPath(webtoonTitle, fileName.getValue()); | ||
s3Uploader.upload(key, image); | ||
|
||
return IMAGE_URL + key; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
module-domain-s3/src/main/java/com/devtoon/jtoon/common/FileName.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,22 @@ | ||
package com.devtoon.jtoon.common; | ||
|
||
import java.util.UUID; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FileName { | ||
|
||
private final String value; | ||
|
||
public static FileName forWebtoon() { | ||
return new FileName(UUID.randomUUID().toString()); | ||
} | ||
|
||
public static FileName forEpisode(int no) { | ||
return new FileName(String.format("%04d_", no) + UUID.randomUUID()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
module-domain-s3/src/main/java/com/devtoon/jtoon/common/ImageType.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,19 @@ | ||
package com.devtoon.jtoon.common; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum ImageType { | ||
|
||
WEBTOON_THUMBNAIL("webtoons/%s/thumbnail/%s"), | ||
EPISODE_MAIN("webtoons/%s/episodes/main/%s"), | ||
EPISODE_THUMBNAIL("webtoons/%s/episodes/thumbnail/%s"), | ||
; | ||
|
||
private final String pathFormat; | ||
|
||
public String getPath(String webtoonTitle, String filename) { | ||
return String.format(pathFormat, webtoonTitle, filename); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
module-domain/src/main/java/com/devtoon/jtoon/webtoon/application/WebtoonService.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 |
---|---|---|
@@ -1,7 +1,50 @@ | ||
package com.devtoon.jtoon.webtoon.application; | ||
|
||
import static com.devtoon.jtoon.common.ImageType.*; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.devtoon.jtoon.application.S3Service; | ||
import com.devtoon.jtoon.common.FileName; | ||
import com.devtoon.jtoon.webtoon.entity.Episode; | ||
import com.devtoon.jtoon.webtoon.entity.Webtoon; | ||
import com.devtoon.jtoon.webtoon.repository.EpisodeRepository; | ||
import com.devtoon.jtoon.webtoon.repository.WebtoonRepository; | ||
import com.devtoon.jtoon.webtoon.request.CreateEpisodeReq; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class WebtoonService { | ||
|
||
private final WebtoonRepository webtoonRepository; | ||
private final EpisodeRepository episodeRepository; | ||
private final S3Service s3Service; | ||
|
||
@Transactional | ||
public void createEpisode(Long webtoonId, CreateEpisodeReq req) { | ||
Webtoon webtoon = getWebtoonById(webtoonId); | ||
String mainUrl = s3Service.upload( | ||
EPISODE_MAIN, | ||
webtoon.getTitle(), | ||
FileName.forEpisode(req.no()), | ||
req.mainImage() | ||
); | ||
String thumbnailUrl = s3Service.upload( | ||
EPISODE_THUMBNAIL, | ||
webtoon.getTitle(), | ||
FileName.forEpisode(req.no()), | ||
req.thumbnailImage() | ||
); | ||
Episode episode = req.toEntity(webtoon, mainUrl, thumbnailUrl); | ||
episodeRepository.save(episode); | ||
} | ||
|
||
private Webtoon getWebtoonById(Long webtoonId) { | ||
return webtoonRepository.findById(webtoonId) | ||
.orElseThrow(() -> new RuntimeException("존재하는 웹툰이 아닙니다.")); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
module-domain/src/main/java/com/devtoon/jtoon/webtoon/request/CreateEpisodeReq.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,35 @@ | ||
package com.devtoon.jtoon.webtoon.request; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.devtoon.jtoon.webtoon.entity.Episode; | ||
import com.devtoon.jtoon.webtoon.entity.Webtoon; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record CreateEpisodeReq( | ||
@Min(1) int no, | ||
@NotBlank @Size(max = 30) String title, | ||
boolean hasComment, | ||
@NotNull LocalDateTime openedAt, | ||
@NotNull MultipartFile mainImage, | ||
MultipartFile thumbnailImage | ||
) { | ||
|
||
public Episode toEntity(Webtoon webtoon, String mainUrl, String thumbnailUrl) { | ||
return Episode.builder() | ||
.no(no) | ||
.title(title) | ||
.hasComment(hasComment) | ||
.openedAt(openedAt) | ||
.mainUrl(mainUrl) | ||
.thumbnailUrl(thumbnailUrl) | ||
.webtoon(webtoon) | ||
.build(); | ||
} | ||
} |