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: db connect with spring boot in local #3

Merged
merged 13 commits into from
Dec 19, 2024
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ 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-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.backend.controller;
import com.example.backend.entity.APIToken;
import com.example.backend.repository.APITokenRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Optional;

@RestController
@RequestMapping("/api/token")
public class APITokenController {

@Autowired
private APITokenRepository tokenRepository;

// 1. 토큰 저장
@PostMapping("/save")
public String saveToken(@RequestParam String userId, @RequestParam String tokenValue) {
APIToken token = new APIToken();
token.setTokenValue(tokenValue);

// 현재 시간 + 24시간 (만료 시간 설정)
token.setExpirationTime(LocalDateTime.now().plusHours(24));

tokenRepository.save(token);
return "expires at: " + token.getExpirationTime();
}

// 2. 특정 토큰 값으로 유효성 확인
@GetMapping("/validate-token")
public String validateToken(@RequestParam String tokenValue) {
Optional<APIToken> token = tokenRepository.findByTokenValue(tokenValue);

if (token.isPresent()) {
// 만료 시간 확인
if (token.get().getExpirationTime().isAfter(LocalDateTime.now())) {
return "Token is valid.";
} else {
return "Token is expired.";
}
} else {
return "Token does not exist.";
}
}

// 3. 만료된 토큰 삭제
@DeleteMapping("/delete-expired")
public String deleteExpiredTokens() {
tokenRepository.deleteExpiredAPITokens();
return "Expired tokens deleted!";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.backend.controller;

import com.example.backend.entity.Stock;
import com.example.backend.repository.StockRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stocks/rds")
public class RDSController {

private final StockRepository stockRepository;

public RDSController(StockRepository stockRepository) {
this.stockRepository = stockRepository;
}

@GetMapping
public ResponseEntity<List<Stock>> getAllStocks() {
List<Stock> stocks = stockRepository.findAll();
return ResponseEntity.ok(stocks);
}
// 모든 주식 조회
// curl -X GET http://localhost:8080/stocks/rds

@GetMapping("/{id}")
public ResponseEntity<Stock> getStockById(@PathVariable int id) {
return stockRepository.findById(id)
.map(stock -> ResponseEntity.ok(stock))
.orElse(ResponseEntity.notFound().build());
}
// 특정 ID 주식 조회
// curl -X GET http://localhost:8080/stocks/rds/1

@PostMapping
public ResponseEntity<Stock> createStock(@RequestBody Stock stock) {
Stock saved = stockRepository.save(stock);
return ResponseEntity.status(HttpStatus.CREATED).body(saved);
}
// 새로운 주식 생성
// curl -X POST http://localhost:8080/stocks/rds \
// -H "Content-Type: application/json" \
// -d '{"stockName":"TestStock","stockId":"TST001"}'

@PutMapping("/{id}")
public ResponseEntity<Stock> updateStock(@PathVariable int id, @RequestBody Stock updatedStock) {
return stockRepository.findById(id)
.map(existing -> {
existing.setStockName(updatedStock.getStockName());
existing.setStockId(updatedStock.getStockId());
Stock saved = stockRepository.save(existing);
return ResponseEntity.ok(saved);
})
.orElse(ResponseEntity.notFound().build());
}
// 주식 업데이트
// curl -X PUT http://localhost:8080/stocks/rds/1 \
// -H "Content-Type: application/json" \
// -d '{"stockName":"UpdatedName","stockId":"UPD123"}'

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteStock(@PathVariable int id) {
if (stockRepository.existsById(id)) {
stockRepository.deleteById(id);
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
}
// 특정 ID 주식 삭제
// curl -X DELETE http://localhost:8080/stocks/rds/1

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.backend.controller;
import org.springframework.beans.factory.annotation.Autowired;
// Redis
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;

@GetMapping("/redis-test")
public String testRedis() {
redisTemplate.opsForValue().set("testKey", "Hello from Redis!");
return redisTemplate.opsForValue().get("testKey");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.example.backend.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,4 +17,3 @@ public String getToken() throws Exception {
return kisTokenService.getAccessToken();
}
}

44 changes: 44 additions & 0 deletions backend/src/main/java/com/example/backend/entity/APIToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.backend.entity;
import java.time.LocalDateTime;

import jakarta.persistence.*;

@Entity
@Table(name = "API_TOKEN_TB")
public class APIToken {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int tokenId;

@Column(name = "token_value", nullable = false, columnDefinition = "TEXT")
private String tokenValue;

@Column(name = "expiration_time")
private LocalDateTime expirationTime;

// Getters and Setters
public int getTokenId() {
return tokenId;
}

public void setTokenId(int tokenId) {
this.tokenId = tokenId;
}

public String getTokenValue() {
return tokenValue;
}

public void setTokenValue(String tokenValue) {
this.tokenValue = tokenValue;
}

public LocalDateTime getExpirationTime() {
return expirationTime;
}

public void setExpirationTime(LocalDateTime expirationTime) {
this.expirationTime = expirationTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.backend.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "DAILY_STOCK_TB")
public class DailyStockPrice {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int dailyId;

@Column(name = "stock_id", nullable = false)
private String stockId;

@Column(name = "date")
private Integer date;

@Column(name = "closing_price")
private Integer closingPrice;

@Column(name = "fluctuation_rate_daily")
private Integer fluctuationRateDaily;

@Column(name = "cntg_vol")
private Integer cntgVol;

@Column(name = "opening_price")
private Integer openingPrice;

@Column(name = "high_price")
private Integer highPrice;

@Column(name = "low_price")
private Integer lowPrice;


// Getters and Setters
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.backend.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "LIVE_DETERMINED_TB")
public class LiveDetermined {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "stock_id", nullable = false)
private String stockId;

@Column(name = "stock_name")
private Integer stockName;

@Column(name = "current_price")
private Integer currentPrice;

@Column(name = "fluctuation")
private Integer fluctuation;

@Column(name = "fluctuation_rate")
private Integer fluctuationRate;

@Column(name = "sign")
private Integer sign;

@Column(name = "execution_time")
private Integer executionTime;

@Column(name = "exectuion_amout")
private Integer executionAmount;

// Getters and Setters
}
20 changes: 20 additions & 0 deletions backend/src/main/java/com/example/backend/entity/Popular.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.backend.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "POPULAR_TB")
public class Popular {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int rankingId;

@Column(name = "stock_id", nullable = false)
private String stockId;

@Column(name = "ranking")
private Integer ranking;

// Getters and Setters
}
51 changes: 51 additions & 0 deletions backend/src/main/java/com/example/backend/entity/Stock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.backend.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "STOCK_TB")
public class Stock {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "stock_name", nullable = false)
private String stockName;

@Column(name = "stock_id", nullable = false, unique = true)
private String stockId;

protected Stock() {
// JPA에서 기본 생성자 필요
}

public Stock(String stockId) {
this.stockId = stockId;
}

public Stock(String stockName, String stockId) {
this.stockName = stockName;
this.stockId = stockId;
}

public Integer getId() {
return id;
}

public String getStockName() {
return stockName;
}

public void setStockName(String stockName) {
this.stockName = stockName;
}

public String getStockId() {
return stockId;
}

public void setStockId(String stockId) {
this.stockId = stockId;
}
}
Loading