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/#67 - ipo domain 로직 개선 #77

Merged
merged 18 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b73a3ce
feat(IpoPaging): #67 - IPO 페이징 처리를 위한 정보를 담는 객체 생성
chhs2131 Dec 20, 2022
8ce8776
refactor(IpoPaging,IpoController,IpoService): #67 - IpoPaging 객체를 통해 …
chhs2131 Dec 20, 2022
2af017e
refactor(IpoPaging,Sort): #67 - Sort가 여러항목에 대치될 수 있도록 수정
chhs2131 Dec 20, 2022
3ee8668
refactor(IpoPaging,IpoSequence): #67 - IpoSequence 이름을 toString() 함수를…
chhs2131 Dec 22, 2022
adbbdab
feat(DatePeriod): #67 - 날짜 범위를 다르는 객체 추가
chhs2131 Dec 22, 2022
aaa8691
feat(IpoController,IpoPaging,ErrorCode): #67 - IpoPaging 사용시 발생할 수 있는…
chhs2131 Dec 22, 2022
d32af7e
feat(IpoPaging,DatePeriod): #67 - IpoPaging시 DatePeriod를 통해 기간을 설정하도록 지정
chhs2131 Dec 22, 2022
7e1e95e
feat(IpoController): comment 리스트 기본 조회 기간 최근 7일에서 14일로 수정
chhs2131 Dec 23, 2022
d256a85
feat(IpoController): comment 를 통해 특정 ipo를 조회하는 기능 제거
chhs2131 Dec 23, 2022
d34f44f
refactor(Ipo): ipo api 중 기간 관련 파라미터를 받는 요청들에 대해서 내부적으로 DatePeriod를 사용
chhs2131 Dec 23, 2022
6b06ce6
fix(IpoComment): comment 내용이 json으로 제대로 반환되지 못하던 문제 수정
chhs2131 Dec 24, 2022
ee67f6f
feat(CommentService): IpoService에서 comment 부분만 분리하여 별도의 Service Layer…
chhs2131 Dec 24, 2022
5f55845
feat(CommentMapper): IpoMapper에서 comment 부분만을 분리하여 CommentMapper 기능으로 분리
chhs2131 Dec 24, 2022
c447896
fix(IpoCommentDto,CommentServiceImpl): 신규등록된 종목의 comment가 의도하지 않게 반환되…
chhs2131 Dec 25, 2022
e484d98
refactor(CommentServiceImpl): 변수명 수정 commentList -> comments
chhs2131 Dec 25, 2022
5ed3b9b
style(CommentServiceImpl): 주석 설명 추가
chhs2131 Dec 25, 2022
d01277e
feat(sql-ipo.xml): ipo list TODAY 조회시 기간을 기준으로 조회되도록 로직 수정
chhs2131 Dec 28, 2022
66f7322
feat(sql-ipo.xml): ipo list 조회시 기간을 기준으로 조회되도록 로직 수정
chhs2131 Dec 28, 2022
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.dbsgapi.dbsgapi.api.ipo.controller;

import com.dbsgapi.dbsgapi.api.ipo.domain.DatePeriod;
import com.dbsgapi.dbsgapi.api.ipo.domain.IpoPaging;
import com.dbsgapi.dbsgapi.api.ipo.domain.IpoSequence;
import com.dbsgapi.dbsgapi.api.ipo.domain.Sort;
import com.dbsgapi.dbsgapi.api.ipo.dto.*;
import com.dbsgapi.dbsgapi.api.ipo.service.CommentService;
import com.dbsgapi.dbsgapi.api.ipo.service.IpoService;
import com.dbsgapi.dbsgapi.global.response.CustomException;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -26,34 +30,35 @@
@RequestMapping("/api/v1/ipo")
public class IpoApiController {
private final IpoService ipoService;
private final CommentService commentService;

@GetMapping(value = "")
@Operation(summary = "IPO 목록을 반환", description = "IPO 목록을 최근 등록된 순으로 반환합니다.")
public ResponseEntity<List<IpoSummaryDto>> getIpoList(
@Parameter(description = "페이지 번호") @RequestParam(required = false, defaultValue = "1") int page,
@Parameter(description = "페이지당 반환갯수") @RequestParam(required = false, defaultValue = "100") int num,
@Parameter(description = "기준 일자") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now()}")
@Parameter(description = "기준 일자 (deprecated)") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now()}")
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate targetDate,
@Parameter(description = "기준일 진행 단계") @RequestParam(required = false, defaultValue = "ALL") IpoSequence state,
@Parameter(description = "정렬 (현재 사용되지 않음)") @RequestParam(required = false, defaultValue = "asc") String sort,
@Parameter(description = "청약철회된 종목 반환여부") @RequestParam(required = false, defaultValue = "false") Boolean withCancelItem,
@Parameter(description = "기준 시작 일자 (현재 사용되지 않음)") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now()}")
@Parameter(description = "기준 시작 일자") @RequestParam(required = false)
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@Parameter(description = "기준 종료 일자 (현재 사용되지 않음)") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now()}")
@Parameter(description = "기준 종료 일자") @RequestParam(required = false)
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
) {

//TODO 추후 페이징 관련 dto 를 만들어서 서비스에 넘기기

// 아직 처리할 수 없는 state 예외처리
IpoSequence.validate(state);

// IPO 목록 조회
try {
List<IpoSummaryDto> listIpo = ipoService.selectIpos(targetDate, startDate, endDate, state, withCancelItem, page, num, sort);
IpoPaging ipoPaging = new IpoPaging(page, num, state, targetDate, startDate, endDate, withCancelItem,
Sort.from(sort));
IpoSequence.validate(state); // 아직 처리할 수 없는 sequence 예외처리

// IPO 목록 조회
List<IpoSummaryDto> listIpo = ipoService.selectIpos(ipoPaging);
return new ResponseEntity<>(listIpo, HttpStatus.OK);
} catch (IllegalStateException e) {
throw new CustomException(IPO_LIST_NOT_FOUND_EXCEPTION);
} catch (IllegalArgumentException e) {
throw new CustomException(IPO_ILLEGAL_ARGUMENT_EXCEPTION);
}
}

Expand All @@ -63,7 +68,7 @@ public ResponseEntity<IpoDetailDto> getIpo(@PathVariable("ipoIndex") long ipoInd
IpoDetailDto ipoData = new IpoDetailDto();
try {
ipoData.setIpo(ipoService.selectIpo(ipoIndex));
ipoData.setComment(ipoService.selectIpoComment(ipoIndex));
ipoData.setComment(commentService.selectIpoComment(ipoIndex));
ipoData.setUnderwriter(ipoService.selectIpoUnderwriter(ipoIndex));
return new ResponseEntity<>(ipoData, HttpStatus.OK);
} catch (IllegalStateException e) {
Expand Down Expand Up @@ -97,7 +102,7 @@ public ResponseEntity<List<IpoUnderwriterDto>> getIpoUnderwriter(@PathVariable("
@Operation(summary = "특정 종목의 Comment 조회", description = "특정 종목의 코멘트(히스토리)를 조회합니다.")
public ResponseEntity<List<IpoCommentDto>> getIpoCommentList(@PathVariable("ipoIndex") long ipoIndex) {
try {
List<IpoCommentDto> ipoData = ipoService.selectIpoComment(ipoIndex);
List<IpoCommentDto> ipoData = commentService.selectIpoComment(ipoIndex);
return new ResponseEntity<>(ipoData, HttpStatus.OK);
} catch (IllegalStateException e) {
throw new CustomException(IPO_COMMENT_LIST_NOT_FOUND_EXCEPTION);
Expand All @@ -107,32 +112,27 @@ public ResponseEntity<List<IpoCommentDto>> getIpoCommentList(@PathVariable("ipoI
@GetMapping(value = "/comment")
@Operation(summary = "IPO Comment 조회", description = "코멘트(히스토리)를 조회합니다. 이 때, 최근 코멘트가 앞쪽 페이지에 위치합니다.")
public ResponseEntity<List<IpoCommentDto>> getIpoCommentList(
@Parameter(description = "특정 ipoIndex만 조회") @RequestParam(required = false, defaultValue = "0") int ipoIndex,
@Parameter(description = "조회 시작일자") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now().minusDays(7)}")
@Parameter(description = "조회 시작일자") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now().minusDays(14)}")
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@Parameter(description = "조회 종료일자") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now()}")
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate
) {
List<IpoCommentDto> ipoData;
try {
if (ipoIndex == 0) { // 전체 조회
ipoData = ipoService.selectIpoCommentList(startDate, endDate);
} else if (ipoIndex > 0) { // 특정 종목만 조회
ipoData = ipoService.selectIpoComment(ipoIndex);
} else {
throw new CustomException(IPO_COMMENT_WRONG_PARAMETER_EXCEPTION);
}
DatePeriod datePeriod = DatePeriod.from(startDate, endDate);
List<IpoCommentDto> ipoData = commentService.selectIpoCommentList(datePeriod);
return new ResponseEntity<>(ipoData, HttpStatus.OK);
} catch (IllegalStateException e) {
throw new CustomException(IPO_COMMENT_LIST_NOT_FOUND_EXCEPTION);
} catch (IllegalArgumentException e) {
throw new CustomException(IPO_ILLEGAL_ARGUMENT_EXCEPTION);
}
}

@GetMapping(value = "/comment/{commentIndex}")
@Operation(summary = "특정 Comment 확인", description = "단일 comment를 조회합니다.")
public ResponseEntity<IpoCommentDto> getIpoComment(@PathVariable("commentIndex") long commentIndex) {
try {
IpoCommentDto ipoData = ipoService.selectIpoCommentIndex(commentIndex);
IpoCommentDto ipoData = commentService.selectIpoCommentIndex(commentIndex);
return new ResponseEntity<>(ipoData, HttpStatus.OK);
} catch (IllegalStateException e) {
throw new CustomException(IPO_COMMENT_NOT_FOUND_EXCEPTION);
Expand All @@ -142,15 +142,18 @@ public ResponseEntity<IpoCommentDto> getIpoComment(@PathVariable("commentIndex")
@GetMapping(value = "/schedule")
@Operation(summary = "지정 기간내에 일정을 확인", description = "지정한 기간내에 일정을 모두 확인합니다.")
public ResponseEntity<List<IpoSummaryDto>> getScheduleList(
@Parameter(description = "조회 시작일자") String startDate,
@Parameter(description = "조회 종료일자") String endDate) {
//TODO 파라미터 반드시 입력해야되는지 확인
//TODO Date Parameter를 String이 아닌 LocalDate Type으로 받도록 설정
@Parameter(description = "조회 시작일자") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now().minusDays(14)}")
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@Parameter(description = "조회 종료일자") @RequestParam(required = false, defaultValue = "#{T(java.time.LocalDate).now()}")
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
try {
List<IpoSummaryDto> ipoData = ipoService.selectIpoScheduleList(startDate, endDate);
DatePeriod datePeriod = DatePeriod.from(startDate, endDate);
List<IpoSummaryDto> ipoData = ipoService.selectIpoScheduleList(datePeriod);
return new ResponseEntity<>(ipoData, HttpStatus.OK);
} catch (IllegalStateException e) {
throw new CustomException(IPO_SCHEDULE_NOT_FOUND_EXCEPTION);
} catch (IllegalArgumentException e) {
throw new CustomException(IPO_ILLEGAL_ARGUMENT_EXCEPTION);
}
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/dbsgapi/dbsgapi/api/ipo/domain/DatePeriod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.dbsgapi.dbsgapi.api.ipo.domain;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

public class DatePeriod {
private final LocalDate startDate;
private final LocalDate endDate;

public DatePeriod(LocalDate startDate, LocalDate endDate) {
this.startDate = startDate;
this.endDate = endDate;
validate();
}

private void validate() {
validateNotNull();
validateDateRange();
}

private void validateNotNull() {
if (startDate == null || endDate == null) {
throw new IllegalArgumentException("startDate and endDate must not be null");
}
}

private void validateDateRange() {
if (startDate.isAfter(endDate)) {
throw new IllegalArgumentException("Start date must be before end date");
}
}

public static DatePeriod from(LocalDate targetDate) {
return new DatePeriod(targetDate, targetDate);
}

public static DatePeriod from(LocalDate startDate, LocalDate endDate) {
return new DatePeriod(startDate, endDate);
}

/**
* tartget, start, end 중에 어떤 것을 런타임레벨에서 동적으로 결정하여 결과를 반환합니다.
* @param targetDate 특정일에 대한 명시 (startDate, endDate 값이 없는 경우에 사용됩니다.)
* @param startDate 시작 기준일
* @param endDate 종료 기준일
* @return DatePeriod
*/
public static DatePeriod from(LocalDate targetDate, LocalDate startDate, LocalDate endDate) {
if (startDate != null && endDate != null) {
return new DatePeriod(startDate, endDate);
}
if (startDate == null && endDate == null && targetDate != null) {
return new DatePeriod(targetDate, targetDate);
}
throw new IllegalArgumentException("잘못된 기간 값이 입력되었습니다.");
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("startDate", getStartDate());
map.put("endDate", getEndDate());

return map;
}

public LocalDate getStartDate() {
return startDate;
}

public LocalDate getEndDate() {
return endDate;
}

@Override
public String toString() {
return "기간: " + startDate.toString() + " ~ " + endDate.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dbsgapi.dbsgapi.api.ipo.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -10,6 +11,11 @@ public class IpoComment {
private String comment;
private String detail;

@JsonIgnore
public boolean isHaveTitle() {
return !comment.isEmpty();
}

public static IpoComment makeNew(KindOfComment kind, String detail) {
return IpoComment.builder()
.type(kind.getType())
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/dbsgapi/dbsgapi/api/ipo/domain/IpoPaging.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.dbsgapi.dbsgapi.api.ipo.domain;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

public class IpoPaging {
private final int pageNumber;
private final int numberOfArticles;
private final IpoSequence ipoSequence;
private final DatePeriod datePeriod;
private final boolean withCancelItem;
private final Sort sort;

public IpoPaging(int pageNumber, int numberOfArticles, IpoSequence ipoSequence, LocalDate startDate,
LocalDate endDate, boolean withCancelItem, Sort sort) {
this.pageNumber = pageNumber;
this.numberOfArticles = numberOfArticles;
this.ipoSequence = ipoSequence;
this.datePeriod = DatePeriod.from(startDate, endDate);
this.withCancelItem = withCancelItem;
this.sort = sort;
}

public IpoPaging(int pageNumber, int numberOfArticles, IpoSequence ipoSequence, LocalDate targetDate,
boolean withCancelItem, Sort sort) {
this.pageNumber = pageNumber;
this.numberOfArticles = numberOfArticles;
this.ipoSequence = ipoSequence;
this.datePeriod = DatePeriod.from(targetDate);
this.withCancelItem = withCancelItem;
this.sort = sort;
}

public IpoPaging(int pageNumber, int numberOfArticles, IpoSequence ipoSequence, LocalDate targetDate,
LocalDate startDate, LocalDate endDate, boolean withCancelItem, Sort sort) {
this.pageNumber = pageNumber;
this.numberOfArticles = numberOfArticles;
this.ipoSequence = ipoSequence;
this.datePeriod = DatePeriod.from(targetDate, startDate, endDate);
this.withCancelItem = withCancelItem;
this.sort = sort;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("limit", numberOfArticles);
map.put("offset", pageNumber * numberOfArticles - numberOfArticles);
map.put("startDate", datePeriod.getStartDate());
map.put("endDate", datePeriod.getEndDate());
map.put("ipoSequence", ipoSequence.getSequenceName());
map.put("withCancelItem", withCancelItem);
map.put("sort", sort.getName());

return map;
}

@Override
public String toString() {
return toMap().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ public static void validate(IpoSequence ipoSequence) {
validateSupported(ipoSequence);
}

public static void validateSupported(IpoSequence ipoSequence) {
private static void validateSupported(IpoSequence ipoSequence) {
notSupported.stream()
.filter(notSupportedSequence -> notSupportedSequence.equals(ipoSequence))
.findAny()
.ifPresent(e -> {
throw new CustomException(IPO_LIST_NOT_SUPPORTED_STATE);
});
}

public String getSequenceName() {
return this.name();
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/dbsgapi/dbsgapi/api/ipo/domain/Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dbsgapi.dbsgapi.api.ipo.domain;

import java.util.Arrays;
import java.util.List;

public enum Sort {
ASC(Arrays.asList("ASC", "asc")),
DESC(Arrays.asList("DESC", "desc"));

private final List<String> values;

Sort(List<String> values) {
this.values = values;
}

public static Sort from(String value) {
return Arrays.stream(Sort.values())
.filter(sort -> sort.getValues().contains(value))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 정렬값입니다. 변환 불가 " + value));
}

public List<String> getValues() {
return values;
}

public String getName() {
return this.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class IpoCommentDto {
private String logType;
private String changeLogJson;

public void setComment(String comment) {
title = comment;
}

public void setStockKinds(String stockKinds) {
this.stockKinds = StockKinds.from(stockKinds);
}
Expand Down Expand Up @@ -69,7 +73,6 @@ public String getChangeLogJson() {

@Override
public String toString() {
return String.format("[%d %s] %d - %s", ipoIndex, stockName, commentIndex,
title);
return String.format("[%d %s] %d - %s", ipoIndex, stockName, commentIndex, title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dbsgapi.dbsgapi.api.ipo.mapper;

import com.dbsgapi.dbsgapi.api.ipo.dto.IpoCommentDto;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface CommentMapper {
List<IpoCommentDto> selectIpoComment(long ipoIndex);

Optional<IpoCommentDto> selectIpoCommentIndex(long commentIndex);

List<IpoCommentDto> selectIpoCommentList(Map<String, Object> map);
}
Loading