Skip to content

Commit

Permalink
Merge pull request #59 from inu-appcenter/wonjeong#58-refactor-ocp
Browse files Browse the repository at this point in the history
  • Loading branch information
NARUBROWN authored Jul 14, 2024
2 parents 0217e87 + de120ed commit 6d19f8f
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 36 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import server.inuappcenter.kr.common.data.dto.CommonResponseDto;
import server.inuappcenter.kr.data.dto.request.FaqBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.AdditionalBoardStrategyProvider;
import server.inuappcenter.kr.service.boardService.BoardService;

import javax.validation.Valid;
Expand All @@ -19,15 +19,11 @@
@RestController
@RequestMapping("/faqs")
@Tag(name = "[FAQ] 질의응답 게시판")
@RequiredArgsConstructor
@Slf4j
public class FaqController {
private final BoardService boardService;
private final AdditionalBoardService additionalBoardService;

public FaqController(BoardService boardService, @Qualifier(value = "FaqBoardServiceImpl") AdditionalBoardService additionalBoardService) {
this.boardService = boardService;
this.additionalBoardService = additionalBoardService;
}
private final AdditionalBoardStrategyProvider additionalBoardStrategyProvider;

@Operation(summary = "FAQ 한 개 가져오기", description = "조회할 faq_id를 입력해주세요")
@GetMapping("public/{id}")
Expand All @@ -40,7 +36,7 @@ public ResponseEntity<BoardResponseDto> getFaqBoard(final @PathVariable("id") Lo
public ResponseEntity<List<BoardResponseDto>> getFaqBoardList(
@RequestParam(value = "topic", required = false) String topic
) {
return ResponseEntity.status(HttpStatus.OK).body(additionalBoardService.findBoardList(topic));
return ResponseEntity.status(HttpStatus.OK).body(additionalBoardStrategyProvider.findBoardList("FaqBoardServiceImpl", topic));
}

@Operation(summary = "FAQ 한 개 작성", description = "저장할 FAQ JSON을 보내주세요")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -14,7 +13,7 @@
import server.inuappcenter.kr.data.dto.request.IntroBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.exception.customExceptions.CustomModelAttributeException;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.AdditionalBoardStrategyProvider;
import server.inuappcenter.kr.service.boardService.BoardService;

import javax.validation.Valid;
Expand All @@ -23,16 +22,12 @@

@RestController
@RequestMapping("/introduction-board")
@RequiredArgsConstructor
@Tag(name = "[Intro] 애플리케이션 소개 게시판")
public class IntroBoardController {

public final BoardService boardService;
public final AdditionalBoardService additionalBoardService;

public IntroBoardController(BoardService boardService, @Qualifier(value = "IntroBoardServiceImpl") AdditionalBoardService additionalBoardService) {
this.boardService = boardService;
this.additionalBoardService = additionalBoardService;
}
public final AdditionalBoardStrategyProvider additionalBoardStrategyProvider;

@Operation(summary = "게시글 (1개) 가져오기", description = "가져올 게시글의 id를 입력해주세요")
@Parameter(name = "id", description = "게시판 id", required = true)
Expand Down Expand Up @@ -61,7 +56,7 @@ public ResponseEntity<CommonResponseDto> deleteBoard(final @PathVariable("id") L
@Operation(summary = "앱 소개 글 (전체) 조회", description = "앱 소개 글을 모두 반환합니다.")
@GetMapping("/public/all-boards-contents")
public ResponseEntity<List<BoardResponseDto>> findAllBoard() {
return ResponseEntity.status(HttpStatus.OK).body(additionalBoardService.findBoardList(null));
return ResponseEntity.status(HttpStatus.OK).body(additionalBoardStrategyProvider.findBoardList("IntroBoardServiceImpl"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Qualifier;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,7 +13,7 @@
import server.inuappcenter.kr.data.dto.request.PhotoBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.exception.customExceptions.CustomModelAttributeException;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.AdditionalBoardStrategyProvider;
import server.inuappcenter.kr.service.boardService.BoardService;

import javax.validation.Valid;
Expand All @@ -22,15 +22,11 @@

@RestController
@RequestMapping("/photo-board")
@RequiredArgsConstructor
@Tag(name = "[Photo] 사진 게시판")
public class PhotoBoardController {
private final BoardService boardService;
private final AdditionalBoardService additionalBoardService;

public PhotoBoardController(BoardService boardService, @Qualifier(value = "PhotoBoardServiceImpl") AdditionalBoardService additionalBoardService) {
this.boardService = boardService;
this.additionalBoardService = additionalBoardService;
}
private final AdditionalBoardStrategyProvider additionalBoardStrategyProvider;

@Operation(summary = "게시글 (1개) 가져오기", description = "가져올 게시글의 id를 입력해주세요")
@Parameter(name = "id", description = "게시판 id")
Expand Down Expand Up @@ -73,7 +69,7 @@ public ResponseEntity<CommonResponseDto> deleteBoard(final @PathVariable("id") L
@Operation(summary = "사진 글 (전체) 조회", description = "사진 글을 모두 반환합니다.")
@GetMapping("/public/all-boards-contents")
public ResponseEntity<List<BoardResponseDto>> findAllBoard() {
return ResponseEntity.status(HttpStatus.OK).body(additionalBoardService.findBoardList(null));
return ResponseEntity.status(HttpStatus.OK).body(additionalBoardStrategyProvider.findBoardList("PhotoBoardServiceImpl"));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

public interface BoardRepository<T extends Board> extends JpaRepository<T, Long> {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package server.inuappcenter.kr.service.boardService;

import server.inuappcenter.kr.data.dto.response.BoardResponseDto;

import java.util.List;

public interface AdditionalBoardStrategyProvider {
List<BoardResponseDto> findBoardList(String boardName, String topic);
List<BoardResponseDto> findBoardList(String boardName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package server.inuappcenter.kr.service.boardService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.service.boardService.impl.AdditionalBoardService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AdditionalBoardStrategyProviderImpl implements AdditionalBoardStrategyProvider {
private static final Logger log = LoggerFactory.getLogger(AdditionalBoardStrategyProviderImpl.class);
private final Map<String, AdditionalBoardService> boardStrategyMap;

@Autowired
public AdditionalBoardStrategyProviderImpl(List<AdditionalBoardService> additionalBoardServices) {
this.boardStrategyMap = new HashMap<>();
for (AdditionalBoardService additionalBoardService : additionalBoardServices) {
String key = additionalBoardService.getClass().getSimpleName().toLowerCase();
int index = key.indexOf("$");
this.boardStrategyMap.put(key.substring(0, index), additionalBoardService);
}
}


@Override
public List<BoardResponseDto> findBoardList(String boardName, String topic) {
AdditionalBoardService additionalBoardService = boardStrategyMap.get(boardName.toLowerCase());
return additionalBoardService.findBoardList(topic);
}

@Override
public List<BoardResponseDto> findBoardList(String boardName) {
log.info("getTest");
AdditionalBoardService additionalBoardService = boardStrategyMap.get(boardName.toLowerCase());
System.out.println(additionalBoardService.getClass().getSimpleName());
return additionalBoardService.findBoardList(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server.inuappcenter.kr.service.boardService;
package server.inuappcenter.kr.service.boardService.impl;

import server.inuappcenter.kr.data.dto.response.BoardResponseDto;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import server.inuappcenter.kr.data.domain.board.Board;
import server.inuappcenter.kr.data.domain.board.FaqBoard;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.repository.FaqRepository;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package server.inuappcenter.kr.service.boardService.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import server.inuappcenter.kr.data.domain.board.Board;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.repository.IntroBoardRepository;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service("IntroBoardServiceImpl")
@RequiredArgsConstructor
@Slf4j
public class IntroBoardServiceImpl implements AdditionalBoardService {
private final IntroBoardRepository introBoardRepository;
private final HttpServletRequest request;

@Override
@Transactional(readOnly = true)
public List<BoardResponseDto> findBoardList(String topic) {
log.info("findBoardList");
List<BoardResponseDto> responseDtoList = new ArrayList<>();
for (Board board : introBoardRepository.findAll()) {
responseDtoList.add(board.createResponse(request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import server.inuappcenter.kr.data.domain.board.Board;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.repository.PhotoBoardRepository;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ spring:
redis:
port: 5379
host: na2ru2.me
sentinel:
username: appcenter-homepage
password: appcenter-homepage1!
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: dev
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import server.inuappcenter.kr.data.dto.request.FaqBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.dto.response.FaqBoardResponseDto;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.impl.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.BoardService;
import server.inuappcenter.kr.service.boardService.impl.FaqBoardServiceImpl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import server.inuappcenter.kr.data.dto.request.IntroBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.dto.response.IntroBoardResponseDto;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.impl.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.BoardService;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import server.inuappcenter.kr.data.dto.request.PhotoBoardRequestDto;
import server.inuappcenter.kr.data.dto.response.BoardResponseDto;
import server.inuappcenter.kr.data.dto.response.PhotoBoardResponseDto;
import server.inuappcenter.kr.service.boardService.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.impl.AdditionalBoardService;
import server.inuappcenter.kr.service.boardService.BoardService;

import java.time.LocalDateTime;
Expand Down

0 comments on commit 6d19f8f

Please sign in to comment.