Skip to content

Commit

Permalink
[FEAT] 투자자 성향별 종목 추천 서비스(#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghdeo committed Oct 19, 2023
1 parent d38458a commit 10aa2f0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bull4jo.kkanbustock.stock.controller;

import com.bull4jo.kkanbustock.member.domain.entity.InvestorType;
import com.bull4jo.kkanbustock.stock.domain.RecommendStockResponse;
import com.bull4jo.kkanbustock.stock.domain.Stock;
import com.bull4jo.kkanbustock.stock.service.StockService;
Expand Down Expand Up @@ -28,11 +29,22 @@ public ResponseEntity<Stock> searchStockByName(@RequestParam(value = "itmsNm") f
}

@GetMapping("/v1/recommends")
public ResponseEntity<Page<RecommendStockResponse>> getRecommendedStocks(@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "15") int size) {
public ResponseEntity<Page<RecommendStockResponse>> getRecommendedStocks(
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "15") int size) {
Pageable pageable = PageRequest.of(page, size);
return ResponseEntity.ok(stockService.getRecommendedStocks(pageable));
}

@GetMapping("/v1/recommends/{investorType}")
public ResponseEntity<Page<RecommendStockResponse>> getRecommendStocksByInvestorType(
@PathVariable InvestorType investorType,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "15") int size) {
Pageable pageable = PageRequest.of(page, size);
return ResponseEntity.ok(stockService.getRecommendStocksByInvestorType(investorType, pageable));
}

@GetMapping("/v1/init-stocks")
public void setInitStock() {
stockService.setStockRepository();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/bull4jo/kkanbustock/stock/domain/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public class Stock {
@Column
private long mrktTotAmt;

public Stock(RecommendStock recommendStock) {
this.srtnCd = recommendStock.getSrtnCd();
this.itmsNm = recommendStock.getItmsNm();
this.clpr = recommendStock.getClpr();
}

public void update(int clpr, int mkp) {
this.clpr = clpr;
this.mkp = mkp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.bull4jo.kkanbustock.stock.repository;

import com.bull4jo.kkanbustock.member.domain.entity.InvestorType;
import com.bull4jo.kkanbustock.stock.domain.RecommendStock;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface RecommendStockRepository extends JpaRepository<RecommendStock, String> {
@Override
Page<RecommendStock> findAll(Pageable pageable);

Optional<Page<RecommendStock>> findByInvestorType(InvestorType investorType, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

// 주식 넣고, 빼고, 업데이트, 가져오고, 있는지 확인하고
@Service
Expand Down Expand Up @@ -301,6 +302,18 @@ public Page<RecommendStockResponse> getRecommendedStocks(Pageable pageable) {
return new PageImpl<>(recommends, pageable, page.getTotalElements());
}

@Transactional
public Page<RecommendStockResponse> getRecommendStocksByInvestorType(InvestorType investorType, Pageable pageable) {
Page<RecommendStock> page = recommendStockRepository.findByInvestorType(investorType, pageable).orElseThrow(() -> new CustomException(ErrorCode.RECOMMENDED_STOCK_NOT_FOUND));
List<RecommendStockResponse> recommends = page
.getContent()
.stream()
.map(RecommendStockResponse::new)
.collect(Collectors.toList());

return new PageImpl<>(recommends, pageable, page.getTotalElements());
}

@Transactional
public String update(final String strnCd, final Stock req) {
Stock stock = stockRepository.findById(strnCd).orElseThrow(() -> new CustomException(ErrorCode.STOCK_NOT_FOUND));
Expand Down

0 comments on commit 10aa2f0

Please sign in to comment.