Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/161 : Facade 도입 & 클래스 의존관계 개선 #162

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 6 additions & 34 deletions src/main/java/briefing/briefing/api/BriefingApi.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package briefing.briefing.api;

import java.util.List;
import java.util.Optional;

import jakarta.validation.Valid;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import briefing.briefing.application.BriefingCommandService;
import briefing.briefing.application.BriefingQueryService;
import briefing.briefing.application.BriefingFacade;
import briefing.briefing.application.dto.*;
import briefing.briefing.domain.Briefing;
import briefing.common.aop.annotation.CacheEvictByBriefingId;
import briefing.common.enums.APIVersion;
import briefing.common.response.CommonResponse;
import briefing.member.domain.Member;
import briefing.scrap.application.ScrapQueryService;
import briefing.security.handler.annotation.AuthMember;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -30,49 +23,30 @@
@RequiredArgsConstructor
public class BriefingApi {

private final BriefingQueryService briefingQueryService;
private final BriefingCommandService briefingCommandService;
private final ScrapQueryService scrapQueryService;
private final BriefingFacade briefingFacade;

@GetMapping("/briefings")
@Parameter(name = "timeOfDay", hidden = true)
@Operation(summary = "03-01Briefing \uD83D\uDCF0 브리핑 목록 조회 V1", description = "")
public CommonResponse<BriefingResponseDTO.BriefingPreviewListDTO> findBriefings(
@ParameterObject @ModelAttribute BriefingRequestParam.BriefingPreviewListParam params) {

List<Briefing> briefingList = briefingQueryService.findBriefings(params, APIVersion.V1);
return CommonResponse.onSuccess(
BriefingConverter.toBriefingPreviewListDTO(params.getDate(), briefingList));
return CommonResponse.onSuccess(briefingFacade.findBriefings(params));
}

@GetMapping("/briefings/{id}")
@Parameter(name = "member", hidden = true)
@Operation(summary = "03-02Briefing \uD83D\uDCF0 브리핑 단건 조회 V1", description = "")
public CommonResponse<BriefingResponseDTO.BriefingDetailDTO> findBriefing(
@PathVariable final Long id, @AuthMember Member member) {

Boolean isScrap =
Optional.ofNullable(member)
.map(m -> scrapQueryService.existsByMemberIdAndBriefingId(m.getId(), id))
.orElseGet(() -> Boolean.FALSE);

Boolean isBriefingOpen = false;
Boolean isWarning = false;

return CommonResponse.onSuccess(
BriefingConverter.toBriefingDetailDTO(
briefingQueryService.findBriefing(id, APIVersion.V1),
isScrap,
isBriefingOpen,
isWarning));
return CommonResponse.onSuccess(briefingFacade.findBriefing(id, member));
}

@CacheEvict(value = "findBriefingsV2", key = "#request.getBriefingType()")
@PostMapping("/briefings")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "03-03Briefing \uD83D\uDCF0 브리핑 등록", description = "")
public void createBriefing(@RequestBody final BriefingRequestDTO.BriefingCreate request) {
briefingCommandService.createBriefing(request);
briefingFacade.createBriefing(request);
}

/*
Expand All @@ -92,8 +66,6 @@ public void createBriefing(@RequestBody final BriefingRequestDTO.BriefingCreate
public CommonResponse<BriefingResponseDTO.BriefingUpdateDTO> patchBriefingContent(
@PathVariable(name = "id") Long id,
@RequestBody @Valid BriefingRequestDTO.BriefingUpdateDTO request) {

Briefing briefing = briefingCommandService.updateBriefing(id, request);
return CommonResponse.onSuccess(BriefingConverter.toBriefingUpdateDTO(briefing));
return CommonResponse.onSuccess(briefingFacade.updateBriefing(id, request));
}
}
33 changes: 4 additions & 29 deletions src/main/java/briefing/briefing/api/BriefingV2Api.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
package briefing.briefing.api;

import java.util.List;
import java.util.Optional;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import briefing.briefing.application.BriefingCommandService;
import briefing.briefing.application.BriefingQueryService;
import briefing.briefing.application.BriefingV2Facade;
import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.application.dto.BriefingResponseDTO;
import briefing.briefing.domain.Briefing;
import briefing.common.enums.APIVersion;
import briefing.common.response.CommonResponse;
import briefing.member.domain.Member;
import briefing.scrap.application.ScrapQueryService;
import briefing.security.handler.annotation.AuthMember;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -27,39 +20,21 @@
@RequiredArgsConstructor
@RequestMapping("/v2")
public class BriefingV2Api {
private final BriefingQueryService briefingQueryService;
private final BriefingCommandService briefingCommandService;
private final ScrapQueryService scrapQueryService;
private final BriefingV2Facade briefingFacade;

@GetMapping("/briefings")
@Operation(summary = "03-01Briefing \uD83D\uDCF0 브리핑 목록 조회 V2", description = "")
@Cacheable(value = "findBriefingsV2", key = "#params.getType()")
public CommonResponse<BriefingResponseDTO.BriefingPreviewListDTOV2> findBriefingsV2(
@ParameterObject @ModelAttribute BriefingRequestParam.BriefingPreviewListParam params) {
List<Briefing> briefingList = briefingQueryService.findBriefings(params, APIVersion.V2);
return CommonResponse.onSuccess(
BriefingConverter.toBriefingPreviewListDTOV2(params.getDate(), briefingList));
return CommonResponse.onSuccess(briefingFacade.findBriefings(params));
}

@GetMapping("/briefings/{id}")
@Operation(summary = "03-02Briefing \uD83D\uDCF0 브리핑 단건 조회 V2", description = "")
@Parameter(name = "member", hidden = true)
public CommonResponse<BriefingResponseDTO.BriefingDetailDTOV2> findBriefingV2(
@PathVariable final Long id, @AuthMember Member member) {

Boolean isScrap =
Optional.ofNullable(member)
.map(m -> scrapQueryService.existsByMemberIdAndBriefingId(m.getId(), id))
.orElseGet(() -> Boolean.FALSE);

Boolean isBriefingOpen = false;
Boolean isWarning = false;

return CommonResponse.onSuccess(
BriefingConverter.toBriefingDetailDTOV2(
briefingQueryService.findBriefing(id, APIVersion.V2),
isScrap,
isBriefingOpen,
isWarning));
return CommonResponse.onSuccess(briefingFacade.findBriefing(id, member));
}
}

This file was deleted.

82 changes: 82 additions & 0 deletions src/main/java/briefing/briefing/application/BriefingFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package briefing.briefing.application;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import briefing.briefing.api.BriefingConverter;
import briefing.briefing.application.dto.BriefingRequestDTO;
import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.application.dto.BriefingResponseDTO;
import briefing.briefing.application.service.ArticleCommandService;
import briefing.briefing.application.service.BriefingArticleCommandService;
import briefing.briefing.application.service.BriefingCommandService;
import briefing.briefing.application.service.BriefingQueryService;
import briefing.briefing.domain.Article;
import briefing.briefing.domain.Briefing;
import briefing.briefing.domain.BriefingArticle;
import briefing.common.enums.APIVersion;
import briefing.member.domain.Member;
import briefing.scrap.application.ScrapQueryService;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class BriefingFacade {
private final ScrapQueryService scrapQueryService;
private final BriefingQueryService briefingQueryService;
private final BriefingCommandService briefingCommandService;
private final ArticleCommandService articleCommandService;
private final BriefingArticleCommandService briefingArticleCommandService;
private static final APIVersion version = APIVersion.V1;

@Transactional(readOnly = true)
public BriefingResponseDTO.BriefingPreviewListDTO findBriefings(
BriefingRequestParam.BriefingPreviewListParam params) {
List<Briefing> briefingList = briefingQueryService.findBriefings(params, version);
return BriefingConverter.toBriefingPreviewListDTO(params.getDate(), briefingList);
}

@Transactional(readOnly = true)
public BriefingResponseDTO.BriefingDetailDTO findBriefing(final Long id, Member member) {
Boolean isScrap =
Optional.ofNullable(member)
.map(m -> scrapQueryService.existsByMemberIdAndBriefingId(m.getId(), id))
.orElseGet(() -> Boolean.FALSE);

Boolean isBriefingOpen = false;
Boolean isWarning = false;

return BriefingConverter.toBriefingDetailDTO(
briefingQueryService.findBriefing(id, version), isScrap, isBriefingOpen, isWarning);
}

@Transactional
public void createBriefing(final BriefingRequestDTO.BriefingCreate request) {
final List<Article> articles =
request.getArticles().stream().map(BriefingConverter::toArticle).toList();

Briefing createdBriefing =
briefingCommandService.create(BriefingConverter.toBriefing(request));
List<Article> createdArticles = articleCommandService.createAll(articles);

final List<BriefingArticle> briefingArticles =
createdArticles.stream()
.map(article -> new BriefingArticle(createdBriefing, article))
.toList();

briefingArticleCommandService.createAll(briefingArticles);
}

@Transactional
public BriefingResponseDTO.BriefingUpdateDTO updateBriefing(
Long id, final BriefingRequestDTO.BriefingUpdateDTO request) {
Briefing briefing = briefingQueryService.findBriefing(id, APIVersion.V1);
Briefing updatedBriefing =
briefingCommandService.update(
briefing, request.getTitle(), request.getSubTitle(), request.getContent());
return BriefingConverter.toBriefingUpdateDTO(updatedBriefing);
}
}
47 changes: 47 additions & 0 deletions src/main/java/briefing/briefing/application/BriefingV2Facade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package briefing.briefing.application;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import briefing.briefing.api.BriefingConverter;
import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.application.dto.BriefingResponseDTO;
import briefing.briefing.application.service.BriefingQueryService;
import briefing.briefing.domain.Briefing;
import briefing.common.enums.APIVersion;
import briefing.member.domain.Member;
import briefing.scrap.application.ScrapQueryService;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class BriefingV2Facade {

private final BriefingQueryService briefingQueryService;
private final ScrapQueryService scrapQueryService;
private static final APIVersion version = APIVersion.V2;

@Transactional(readOnly = true)
public BriefingResponseDTO.BriefingPreviewListDTOV2 findBriefings(
BriefingRequestParam.BriefingPreviewListParam params) {
List<Briefing> briefingList = briefingQueryService.findBriefings(params, version);
return BriefingConverter.toBriefingPreviewListDTOV2(params.getDate(), briefingList);
}

@Transactional(readOnly = true)
public BriefingResponseDTO.BriefingDetailDTOV2 findBriefing(final Long id, Member member) {
Boolean isScrap =
Optional.ofNullable(member)
.map(m -> scrapQueryService.existsByMemberIdAndBriefingId(m.getId(), id))
.orElseGet(() -> Boolean.FALSE);

Boolean isBriefingOpen = false;
Boolean isWarning = false;

return BriefingConverter.toBriefingDetailDTOV2(
briefingQueryService.findBriefing(id, version), isScrap, isBriefingOpen, isWarning);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package briefing.briefing.application.service;

import java.util.List;

import org.springframework.stereotype.Service;

import briefing.briefing.domain.Article;
import briefing.briefing.domain.repository.ArticleRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ArticleCommandService {
private final ArticleRepository articleRepository;

public List<Article> createAll(final List<Article> articles) {
return articleRepository.saveAll(articles);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package briefing.briefing.application.service;

import java.util.List;

import org.springframework.stereotype.Service;

import briefing.briefing.domain.BriefingArticle;
import briefing.briefing.domain.repository.BriefingArticleRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class BriefingArticleCommandService {
private final BriefingArticleRepository briefingArticleRepository;

public List<BriefingArticle> createAll(final List<BriefingArticle> briefingArticles) {
return briefingArticleRepository.saveAll(briefingArticles);
}
}
Loading
Loading