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

✨ Feat: 브리핑 목록 조회 V2 방어적 작성 #129

Merged
merged 4 commits into from
Dec 27, 2023
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
<br/>

## 📚 개발 과정
- [[Briefing] API 버전 관리 & 전략 패턴](https://velog.io/@cekim/briefing-api-versioning)


<br/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
@RequiredArgsConstructor
public class BriefingQueryService {

private final BriefingQueryContextFactory briefingQueryContextFactory;

public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListParam params, APIVersion version) {
BriefingQueryContext briefingQueryContext = BriefingQueryContextFactory.getContextByVersion(version);
BriefingQueryContext briefingQueryContext = briefingQueryContextFactory.getContextByVersion(version);
return briefingQueryContext.findBriefings(params);
}

public Briefing findBriefing(final Long id, final APIVersion version) {
BriefingQueryContext briefingQueryContext = BriefingQueryContextFactory.getContextByVersion(version);
BriefingQueryContext briefingQueryContext = briefingQueryContextFactory.getContextByVersion(version);
return briefingQueryContext.findById(id)
.orElseThrow(() -> new BriefingException(ErrorCode.NOT_FOUND_BRIEFING));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@
import briefing.common.enums.APIVersion;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.EnumMap;
import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class BriefingQueryContextFactory {

private final BriefingRepository briefingRepository;
private static BriefingQueryContext staticBriefingQueryContextV1;
private static BriefingQueryContext staticBriefingQueryContextV2;

@PostConstruct
private void init() {
staticBriefingQueryContextV1 = createContext(new BriefingV1QueryStrategy(briefingRepository));
staticBriefingQueryContextV2 = createContext(new BriefingV2QueryStrategy(briefingRepository));
}
private final Map<APIVersion, BriefingQueryContext> contextMap;

private static BriefingQueryContext createContext(BriefingQueryStrategy strategy) {
return new BriefingQueryContext(strategy);
@Autowired
public BriefingQueryContextFactory(List<BriefingQueryStrategy> strategies) {
contextMap = new EnumMap<>(APIVersion.class);
for (BriefingQueryStrategy strategy : strategies) {
APIVersion version = strategy.getVersion();
contextMap.put(version, new BriefingQueryContext(strategy));
}
}

public static BriefingQueryContext getContextByVersion(APIVersion version) {
return switch (version) {
case V1 -> staticBriefingQueryContextV1;
case V2 -> staticBriefingQueryContextV2;
};
public BriefingQueryContext getContextByVersion(APIVersion version) {
BriefingQueryContext context = contextMap.get(version);
if (context == null) {
throw new IllegalArgumentException("Invalid API version: " + version);
}
return context;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.domain.Briefing;
import briefing.common.enums.APIVersion;

import java.util.List;
import java.util.Optional;
Expand All @@ -10,4 +11,6 @@ public interface BriefingQueryStrategy {
List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListParam params);

Optional<Briefing> findById(Long id);

APIVersion getVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import briefing.briefing.domain.Briefing;
import briefing.briefing.domain.BriefingType;
import briefing.briefing.domain.repository.BriefingRepository;
import briefing.common.enums.APIVersion;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class BriefingV1QueryStrategy implements BriefingQueryStrategy {

Expand All @@ -34,4 +37,9 @@ public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListPara
public Optional<Briefing> findById(Long id) {
return briefingRepository.findById(id);
}

@Override
public APIVersion getVersion() {
return APIVersion.V1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@
import briefing.briefing.application.dto.BriefingRequestParam;
import briefing.briefing.domain.Briefing;
import briefing.briefing.domain.repository.BriefingRepository;
import briefing.common.enums.APIVersion;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class BriefingV2QueryStrategy implements BriefingQueryStrategy{

private final BriefingRepository briefingRepository;

@Override
public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListParam params) {
List<Briefing> briefingList;
if(params.isPresentDate()) {
final LocalDateTime startDateTime = params.getDate().atStartOfDay();
final LocalDateTime endDateTime = params.getDate().atTime(LocalTime.MAX);

return briefingRepository.findBriefingsWithScrapCount(
briefingList = briefingRepository.findBriefingsWithScrapCount(
params.getType(), startDateTime, endDateTime, params.getTimeOfDay());
if(!briefingList.isEmpty()) return briefingList;
}

List<Briefing> briefingList = briefingRepository.findTop10ByTypeOrderByCreatedAtDesc(params.getType());
briefingList = briefingRepository.findTop10ByTypeOrderByCreatedAtDesc(params.getType());
Collections.reverse(briefingList);
return briefingList;
}
Expand All @@ -35,4 +41,9 @@ public List<Briefing> findBriefings(BriefingRequestParam.BriefingPreviewListPara
public Optional<Briefing> findById(Long id) {
return briefingRepository.findByIdWithScrapCount(id);
}

@Override
public APIVersion getVersion() {
return APIVersion.V2;
}
}
Loading