Skip to content

Commit

Permalink
Merge pull request #4 from Seasoning-Today/feat/#3
Browse files Browse the repository at this point in the history
절기 계산 로직 안정성 개선
  • Loading branch information
csct3434 authored Jan 13, 2024
2 parents 2550b36 + b9833d0 commit f20c1d0
Show file tree
Hide file tree
Showing 19 changed files with 636 additions and 528 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dependencies {

runtimeOnly 'com.mysql:mysql-connector-j'

implementation platform('software.amazon.awssdk:bom:2.23.0')
implementation 'software.amazon.awssdk:sns'

// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package today.seasoning.seasoning.article.service;

import com.github.f4b6a3.tsid.TsidCreator;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -16,8 +17,8 @@
import today.seasoning.seasoning.common.aws.S3Service;
import today.seasoning.seasoning.common.aws.UploadFileInfo;
import today.seasoning.seasoning.common.exception.CustomException;
import today.seasoning.seasoning.common.util.SolarTermUtil;
import today.seasoning.seasoning.common.util.TsidUtil;
import today.seasoning.seasoning.solarterm.service.SolarTermService;
import today.seasoning.seasoning.user.domain.User;
import today.seasoning.seasoning.user.domain.UserRepository;

Expand All @@ -26,78 +27,69 @@
@RequiredArgsConstructor
public class RegisterArticleService {

private final S3Service s3Service;
private final UserRepository userRepository;
private final ArticleRepository articleRepository;
private final ArticleImageRepository articleImageRepository;

@Value("${ARTICLE_IMAGES_LIMIT}")
private int ARTICLE_IMAGES_LIMIT;

public Long doRegister(RegisterArticleCommand command) {
validateRequest(command);

Article article = createArticle(command);
articleRepository.save(article);

uploadAndRegisterArticleImages(article, command.getImages());

return article.getId();
}

private void validateRequest(RegisterArticleCommand command) {
checkSolarTerm();
checkArticleImagesLimit(command.getImages());
}

private void checkSolarTerm() {
if (SolarTermUtil.getCurrentTerm() == -1) {
throw new CustomException(HttpStatus.FORBIDDEN, "등록기간이 아닙니다.");
}
}

private void checkArticleImagesLimit(List<MultipartFile> images) {
if(images.size() > ARTICLE_IMAGES_LIMIT) {
throw new CustomException(HttpStatus.FORBIDDEN, "최대 이미지 개수: " + ARTICLE_IMAGES_LIMIT);
}
}

private Article createArticle(RegisterArticleCommand command) {
User user = userRepository.findById(command.getUserId()).get();

return new Article(user,
command.isPublished(),
SolarTermUtil.getCurrentYear(),
SolarTermUtil.getCurrentTerm(),
command.getContents());
}

private void uploadAndRegisterArticleImages(Article article, List<MultipartFile> images) {
for (int sequence = 0; sequence < images.size(); sequence++) {
MultipartFile image = images.get(sequence);
UploadFileInfo fileInfo = uploadImage(image);
registerArticleImage(article, fileInfo, sequence + 1);
}
}

private UploadFileInfo uploadImage(MultipartFile image) {
String uid = TsidCreator.getTsid().encode(62);
String originalFilename = image.getOriginalFilename();
String uploadFileName = "images/article/" + uid + "/" + originalFilename;

String url = s3Service.uploadFile(image, uploadFileName);

return new UploadFileInfo(uploadFileName, url);
}

private void registerArticleImage(Article article, UploadFileInfo fileInfo, int sequence) {
ArticleImage articleImage = new ArticleImage(
TsidUtil.createLong(),
article,
fileInfo.getFilename(),
fileInfo.getUrl(),
sequence);

articleImageRepository.save(articleImage);
}
private final S3Service s3Service;
private final UserRepository userRepository;
private final SolarTermService solarTermService;
private final ArticleRepository articleRepository;
private final ArticleImageRepository articleImageRepository;

@Value("${ARTICLE_IMAGES_LIMIT}")
private int ARTICLE_IMAGES_LIMIT;

public Long doRegister(RegisterArticleCommand command) {
validateRequest(command);

Article article = createArticle(command);
articleRepository.save(article);

uploadAndRegisterArticleImages(article, command.getImages());

return article.getId();
}

private void validateRequest(RegisterArticleCommand command) {
if (!solarTermService.checkRecordOpen()) {
throw new CustomException(HttpStatus.FORBIDDEN, "등록 기간이 아닙니다.");
}

if (command.getImages().size() > ARTICLE_IMAGES_LIMIT) {
throw new CustomException(HttpStatus.FORBIDDEN, "이미지 개수 초과");
}
}

private Article createArticle(RegisterArticleCommand command) {
User user = userRepository.findById(command.getUserId()).get();

return new Article(user, command.isPublished(), LocalDate.now().getYear(),
solarTermService.findCurrentTerm(), command.getContents());
}

private void uploadAndRegisterArticleImages(Article article, List<MultipartFile> images) {
for (int sequence = 0; sequence < images.size(); sequence++) {
MultipartFile image = images.get(sequence);
UploadFileInfo fileInfo = uploadImage(image);
registerArticleImage(article, fileInfo, sequence + 1);
}
}

private UploadFileInfo uploadImage(MultipartFile image) {
String uid = TsidCreator.getTsid().encode(62);
String originalFilename = image.getOriginalFilename();
String uploadFileName = "images/article/" + uid + "/" + originalFilename;

String url = s3Service.uploadFile(image, uploadFileName);

return new UploadFileInfo(uploadFileName, url);
}

private void registerArticleImage(Article article, UploadFileInfo fileInfo, int sequence) {
ArticleImage articleImage = new ArticleImage(
TsidUtil.createLong(),
article,
fileInfo.getFilename(),
fileInfo.getUrl(),
sequence);

articleImageRepository.save(articleImage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,97 +16,87 @@
import today.seasoning.seasoning.common.aws.S3Service;
import today.seasoning.seasoning.common.aws.UploadFileInfo;
import today.seasoning.seasoning.common.exception.CustomException;
import today.seasoning.seasoning.common.util.SolarTermUtil;
import today.seasoning.seasoning.common.util.TsidUtil;
import today.seasoning.seasoning.solarterm.service.SolarTermService;

@Service
@Transactional
@RequiredArgsConstructor
public class UpdateArticleService {

private final S3Service s3Service;
private final ArticleRepository articleRepository;
private final ArticleImageRepository articleImageRepository;

@Value("${ARTICLE_IMAGES_LIMIT}")
private int ARTICLE_IMAGES_LIMIT;

public void doUpdate(UpdateArticleCommand command) {
Article article = findArticle(command.getArticleId());

validateRequest(article, command);

deleteOldImages(article.getArticleImages());

uploadAndRegisterArticleImages(article, command.getImages());

updateArticle(article, command);
}

private void validateRequest(Article article, UpdateArticleCommand command) {
checkSolarTerm();
checkArticleImagesLimit(command.getImages());
validatePermission(command.getUserId(), article);
}

private Article findArticle(Long articleId) {
return articleRepository.findById(articleId)
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "기록장 조회 실패"));
}

private void checkSolarTerm() {
if (SolarTermUtil.getCurrentTerm() == -1) {
throw new CustomException(HttpStatus.FORBIDDEN, "등록기간이 아닙니다.");
}
}

private void checkArticleImagesLimit(List<MultipartFile> images) {
if(images.size() > ARTICLE_IMAGES_LIMIT) {
throw new CustomException(HttpStatus.FORBIDDEN, "최대 이미지 개수: " + ARTICLE_IMAGES_LIMIT);
}
}

private void validatePermission(Long userId, Article article) {
Long authorId = article.getUser().getId();

if (!authorId.equals(userId)) {
throw new CustomException(HttpStatus.FORBIDDEN, "권한 없음");
}
}

private void deleteOldImages(List<ArticleImage> articleImages) {
articleImages.stream()
.map(ArticleImage::getFilename)
.forEach(s3Service::deleteFile);
articleImages.clear();
}

private void uploadAndRegisterArticleImages(Article article, List<MultipartFile> images) {
for (int sequence = 0; sequence < images.size(); sequence++) {
MultipartFile image = images.get(sequence);
UploadFileInfo uploadFileInfo = uploadImage(image);
registerArticleImage(article, uploadFileInfo, sequence + 1);
}
}

private UploadFileInfo uploadImage(MultipartFile image) {
String uid = TsidCreator.getTsid().encode(62);
String uploadFileName = "images/article/" + uid + "/" + image.getOriginalFilename();
String url = s3Service.uploadFile(image, uploadFileName);
return new UploadFileInfo(uploadFileName, url);
}

private void registerArticleImage(Article article, UploadFileInfo fileInfo, int sequence) {
ArticleImage articleImage = new ArticleImage(TsidUtil.createLong(),
article,
fileInfo.getFilename(),
fileInfo.getUrl(),
sequence);
articleImageRepository.save(articleImage);
}

private void updateArticle(Article article, UpdateArticleCommand command) {
article.update(command.isPublished(), command.getContents());
articleRepository.save(article);
}
}
private final S3Service s3Service;
private final SolarTermService solarTermService;
private final ArticleRepository articleRepository;
private final ArticleImageRepository articleImageRepository;

@Value("${ARTICLE_IMAGES_LIMIT}")
private int ARTICLE_IMAGES_LIMIT;

public void doUpdate(UpdateArticleCommand command) {
Article article = findArticle(command.getArticleId());

validateRequest(article, command);

deleteOldImages(article.getArticleImages());

uploadAndRegisterArticleImages(article, command.getImages());

updateArticle(article, command);
}

private void validateRequest(Article article, UpdateArticleCommand command) {
if (!solarTermService.checkRecordOpen()) {
throw new CustomException(HttpStatus.FORBIDDEN, "등록 기간이 아닙니다.");
}

if (command.getImages().size() > ARTICLE_IMAGES_LIMIT) {
throw new CustomException(HttpStatus.FORBIDDEN, "이미지 개수 초과");
}

Long authorId = article.getUser().getId();
if (!authorId.equals(command.getUserId())) {
throw new CustomException(HttpStatus.FORBIDDEN, "권한 없음");
}
}

private Article findArticle(Long articleId) {
return articleRepository.findById(articleId)
.orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, "기록장 조회 실패"));
}

private void deleteOldImages(List<ArticleImage> articleImages) {
articleImages.stream()
.map(ArticleImage::getFilename)
.forEach(s3Service::deleteFile);
articleImages.clear();
}

private void uploadAndRegisterArticleImages(Article article, List<MultipartFile> images) {
for (int sequence = 0; sequence < images.size(); sequence++) {
MultipartFile image = images.get(sequence);
UploadFileInfo uploadFileInfo = uploadImage(image);
registerArticleImage(article, uploadFileInfo, sequence + 1);
}
}

private UploadFileInfo uploadImage(MultipartFile image) {
String uid = TsidCreator.getTsid().encode(62);
String uploadFileName = "images/article/" + uid + "/" + image.getOriginalFilename();
String url = s3Service.uploadFile(image, uploadFileName);
return new UploadFileInfo(uploadFileName, url);
}

private void registerArticleImage(Article article, UploadFileInfo fileInfo, int sequence) {
ArticleImage articleImage = new ArticleImage(TsidUtil.createLong(),
article,
fileInfo.getFilename(),
fileInfo.getUrl(),
sequence);
articleImageRepository.save(articleImage);
}

private void updateArticle(Article article, UpdateArticleCommand command) {
article.update(command.isPublished(), command.getContents());
articleRepository.save(article);
}
}
Loading

0 comments on commit f20c1d0

Please sign in to comment.