Skip to content

Commit

Permalink
✨ Feature: Scrap 생성 및 삭제 API 통일 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnsugyeong authored May 28, 2024
1 parent c3ac269 commit 1058ee8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class ScrapFacade {
private final ScrapCommandService scrapCommandService;
private final ScrapQueryService scrapQueryService;

public Long createScrap(User user, Long diaryId) {
return scrapCommandService.createScrap(user, diaryId).getId();
public void handleScrap(User user, Long diaryId) {
scrapCommandService.handleScrap(user, diaryId);
}

public void addCategoryToScrap(User user, Long scrapId, Long categoryId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package zzangdol.scrap.implement;

import zzangdol.scrap.domain.Scrap;
import zzangdol.user.domain.User;

public interface ScrapCommandService {

Scrap createScrap(User user, Long diaryId);
void handleScrap(User user, Long diaryId);

void addCategoryToScrap(User user, Long scrapId, Long categoryId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zzangdol.scrap.implement;

import groovy.util.logging.Slf4j;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,7 +10,6 @@
import zzangdol.diary.domain.Diary;
import zzangdol.exception.custom.CategoryNotFoundException;
import zzangdol.exception.custom.DiaryNotFoundException;
import zzangdol.exception.custom.ScrapDuplicateException;
import zzangdol.exception.custom.ScrapNotFoundException;
import zzangdol.scrap.dao.CategoryRepository;
import zzangdol.scrap.dao.ScrapRepository;
Expand All @@ -29,22 +29,19 @@ public class ScrapCommandServiceImpl implements ScrapCommandService {
private final DiaryRepository diaryRepository;

@Override
public Scrap createScrap(User user, Long diaryId) {
checkScrapDuplication(user, diaryId);
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> DiaryNotFoundException.EXCEPTION);
Scrap scrap = buildScrap(user, diary);
public void handleScrap(User user, Long diaryId) {
Optional<Scrap> optionalScrap = scrapRepository.findScrapByUserAndDiaryId(user, diaryId);
if (optionalScrap.isPresent()) {
scrapRepository.delete(optionalScrap.get());
} else {
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> DiaryNotFoundException.EXCEPTION);
Scrap scrap = buildScrap(user, diary);

Category defaultCategory = categoryRepository.findCategoryByUserAndName(user, Constants.DEFAULT_CATEGORY_NAME)
.orElseThrow(() -> CategoryNotFoundException.EXCEPTION);
scrap = scrapRepository.save(scrap);
addCategoryToScrap(user, scrap.getId(), defaultCategory.getId());
return scrap;
}

private void checkScrapDuplication(User user, Long diaryId) {
if (scrapRepository.findScrapByUserAndDiaryId(user, diaryId).isPresent()) {
throw ScrapDuplicateException.EXCEPTION;
Category defaultCategory = categoryRepository.findCategoryByUserAndName(user, Constants.DEFAULT_CATEGORY_NAME)
.orElseThrow(() -> CategoryNotFoundException.EXCEPTION);
scrap = scrapRepository.save(scrap);
addCategoryToScrap(user, scrap.getId(), defaultCategory.getId());
}
}

Expand All @@ -70,7 +67,7 @@ public void addCategoryToScrap(User user, Long scrapId, Long categoryId) {
public void deleteScrap(User user, Long scrapId) {
Scrap scrap = scrapRepository.findById(scrapId)
.orElseThrow(() -> ScrapNotFoundException.EXCEPTION);
scrapRepository.delete(scrap);

}

private ScrapCategory buildScrapCategory(Scrap scrap, Category category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public class ScrapController {
ErrorStatus.DIARY_NOT_FOUND
})
@Operation(
summary = "스크랩 생성 🔑",
summary = "스크랩 생성 및 취소 🔑",
description = "새로운 스크랩을 생성합니다."
)
@PostMapping
public ResponseDto<Long> createScrap(@AuthUser User user, @Valid @RequestParam("diaryId") Long diaryId) {
return ResponseDto.onSuccess(scrapFacade.createScrap(user, diaryId));
public ResponseDto<Void> handleScrap(@AuthUser User user, @Valid @RequestParam("diaryId") Long diaryId) {
scrapFacade.handleScrap(user, diaryId);
return ResponseDto.onSuccess();
}

@ApiErrorCodeExample({
Expand Down

0 comments on commit 1058ee8

Please sign in to comment.