From 4bd964777a9ea65b0bc77c174672a8bbd256e06d Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Tue, 17 Dec 2024 16:09:02 +0900 Subject: [PATCH 01/10] feat: db connect with spring boot in local --- backend/build.gradle | 3 + .../backend/controller/DBController.java | 63 +++++++++++++++++++ .../com/example/backend/entity/Stock.java | 35 +++++++++++ .../backend/repository/StockRepository.java | 7 +++ .../src/main/resources/application.properties | 13 ++++ 5 files changed, 121 insertions(+) create mode 100644 backend/src/main/java/com/example/backend/controller/DBController.java create mode 100644 backend/src/main/java/com/example/backend/entity/Stock.java create mode 100644 backend/src/main/java/com/example/backend/repository/StockRepository.java diff --git a/backend/build.gradle b/backend/build.gradle index 39936c5..566ae1d 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -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' diff --git a/backend/src/main/java/com/example/backend/controller/DBController.java b/backend/src/main/java/com/example/backend/controller/DBController.java new file mode 100644 index 0000000..d465d7d --- /dev/null +++ b/backend/src/main/java/com/example/backend/controller/DBController.java @@ -0,0 +1,63 @@ +package com.example.backend.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +// MySQL +import com.example.backend.repository.StockRepository; +import com.example.backend.entity.Stock; +// Redis +import org.springframework.data.redis.core.StringRedisTemplate; +import java.util.Optional; +import java.util.List; + +@RestController +public class DBController { + + @Autowired + private StringRedisTemplate redisTemplate; + + @GetMapping("/redis-test") + public String testRedis() { + redisTemplate.opsForValue().set("testKey", "Hello from Redis!"); + return redisTemplate.opsForValue().get("testKey"); + } + + @Autowired + private StockRepository stockRepository; + + // 1. 데이터 저장 (Save) + @GetMapping("/save-stock") + public String saveStock() { + Stock stock = new Stock("005930", "72000"); + stockRepository.save(stock); + return "Stock data saved to RDS!"; + } + + // 2. ID로 데이터 조회 (FindById) + @GetMapping("/find-stock/{id}") + public String findStockById(@PathVariable Long id) { + Optional stock = stockRepository.findById(id); + return stock.map(s -> "Found: " + s.getStockName() + " - " + s.getPrice()) + .orElse("Stock not found with ID: " + id); + } + + // 3. 모든 데이터 조회 (FindAll) + @GetMapping("/find-all-stocks") + public List findAllStocks() { + return stockRepository.findAll(); + } + + // 4. ID로 데이터 삭제 (DeleteById) + @GetMapping("/delete-stock/{id}") + public String deleteStockById(@PathVariable Long id) { + if (stockRepository.existsById(id)) { + stockRepository.deleteById(id); + return "Stock with ID: " + id + " has been deleted."; + } else { + return "Stock not found with ID: " + id; + } + } +} diff --git a/backend/src/main/java/com/example/backend/entity/Stock.java b/backend/src/main/java/com/example/backend/entity/Stock.java new file mode 100644 index 0000000..c0876dd --- /dev/null +++ b/backend/src/main/java/com/example/backend/entity/Stock.java @@ -0,0 +1,35 @@ +package com.example.backend.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity +public class Stock { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String stockName; + private String price; + + public Stock() {} + + public Stock(String stockName, String price) { + this.stockName = stockName; + this.price = price; + } + + public Long getId() { + return id; + } + public String getStockName() { + return stockName; + } + public String getPrice() { + return price; + } + +} diff --git a/backend/src/main/java/com/example/backend/repository/StockRepository.java b/backend/src/main/java/com/example/backend/repository/StockRepository.java new file mode 100644 index 0000000..b16399a --- /dev/null +++ b/backend/src/main/java/com/example/backend/repository/StockRepository.java @@ -0,0 +1,7 @@ +package com.example.backend.repository; + +import com.example.backend.entity.Stock; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface StockRepository extends JpaRepository { +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 3ca17a4..ecd7578 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1 +1,14 @@ spring.application.name=backend +spring.redis.host=localhost +spring.redis.port=6379 + +# MySQL 설정 +spring.datasource.url=jdbc:mysql://localhost:3306/stockdb?serverTimezone=Asia/Seoul +spring.datasource.username=1team +spring.datasource.password=sorkwp1wkfskrk +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +# Hibernate 설정 (JPA) +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true From e9091f7e3ba424f7b1b6f7c877cbaf060f607c51 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Wed, 18 Dec 2024 14:18:55 +0900 Subject: [PATCH 02/10] feat: Prepare for merge by committing local changes --- .../backend/controller/TokenController.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backend/src/main/java/com/example/backend/controller/TokenController.java diff --git a/backend/src/main/java/com/example/backend/controller/TokenController.java b/backend/src/main/java/com/example/backend/controller/TokenController.java new file mode 100644 index 0000000..2418f53 --- /dev/null +++ b/backend/src/main/java/com/example/backend/controller/TokenController.java @@ -0,0 +1,54 @@ +package com.example.backend.controller; + +import com.example.backend.entity.Token; +import com.example.backend.repository.TokenRepository; +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 TokenController { + + @Autowired + private TokenRepository tokenRepository; + + // 1. 토큰 저장 + @PostMapping("/save") + public String saveToken(@RequestParam String userId, @RequestParam String tokenValue) { + Token token = new Token(); + token.setTokenValue(tokenValue); + + // 현재 시간 + 24시간 (만료 시간 설정) + token.setExpirationTime(LocalDateTime.now().plusHours(24)); + + tokenRepository.save(token); + return "Token saved for user: " + userId + ", expires at: " + token.getExpirationTime(); + } + + // 2. 특정 토큰 값으로 유효성 확인 + @GetMapping("/validate-token") + public String validateToken(@RequestParam String tokenValue) { + Optional 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.deleteExpiredTokens(); + return "Expired tokens deleted!"; + } +} \ No newline at end of file From 10d6de37204c8b31874421c0f42e5d7268ab783e Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Wed, 18 Dec 2024 14:27:22 +0900 Subject: [PATCH 03/10] feat: add entities and repositories in ERD --- .../com/example/backend/entity/APIToken.java | 44 +++++++++++++++++++ .../backend/entity/DailyStockPrice.java | 39 ++++++++++++++++ .../backend/entity/LiveDetermined.java | 38 ++++++++++++++++ .../com/example/backend/entity/Popular.java | 20 +++++++++ .../com/example/backend/entity/Stock.java | 18 +++----- .../repository/DailyStockPriceRepository.java | 7 +++ .../repository/LiveDeterminedRepository.java | 7 +++ .../backend/repository/PopularRepository.java | 7 +++ .../backend/repository/StockRepository.java | 2 +- .../backend/repository/TokenRepository.java | 17 +++++++ 10 files changed, 186 insertions(+), 13 deletions(-) create mode 100644 backend/src/main/java/com/example/backend/entity/APIToken.java create mode 100644 backend/src/main/java/com/example/backend/entity/DailyStockPrice.java create mode 100644 backend/src/main/java/com/example/backend/entity/LiveDetermined.java create mode 100644 backend/src/main/java/com/example/backend/entity/Popular.java create mode 100644 backend/src/main/java/com/example/backend/repository/DailyStockPriceRepository.java create mode 100644 backend/src/main/java/com/example/backend/repository/LiveDeterminedRepository.java create mode 100644 backend/src/main/java/com/example/backend/repository/PopularRepository.java create mode 100644 backend/src/main/java/com/example/backend/repository/TokenRepository.java diff --git a/backend/src/main/java/com/example/backend/entity/APIToken.java b/backend/src/main/java/com/example/backend/entity/APIToken.java new file mode 100644 index 0000000..bf3c565 --- /dev/null +++ b/backend/src/main/java/com/example/backend/entity/APIToken.java @@ -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; + } +} diff --git a/backend/src/main/java/com/example/backend/entity/DailyStockPrice.java b/backend/src/main/java/com/example/backend/entity/DailyStockPrice.java new file mode 100644 index 0000000..e5c866d --- /dev/null +++ b/backend/src/main/java/com/example/backend/entity/DailyStockPrice.java @@ -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 +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/entity/LiveDetermined.java b/backend/src/main/java/com/example/backend/entity/LiveDetermined.java new file mode 100644 index 0000000..29ce83d --- /dev/null +++ b/backend/src/main/java/com/example/backend/entity/LiveDetermined.java @@ -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 +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/entity/Popular.java b/backend/src/main/java/com/example/backend/entity/Popular.java new file mode 100644 index 0000000..bcb1e21 --- /dev/null +++ b/backend/src/main/java/com/example/backend/entity/Popular.java @@ -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 +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/entity/Stock.java b/backend/src/main/java/com/example/backend/entity/Stock.java index c0876dd..abf7c0b 100644 --- a/backend/src/main/java/com/example/backend/entity/Stock.java +++ b/backend/src/main/java/com/example/backend/entity/Stock.java @@ -1,35 +1,29 @@ package com.example.backend.entity; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; @Entity +@Table(name = "STOCK_TB") public class Stock { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private String id; + @Column(name = "stock_name") private String stockName; - private String price; public Stock() {} - public Stock(String stockName, String price) { + public Stock(String stockName) { this.stockName = stockName; - this.price = price; } - public Long getId() { + public String getId() { return id; } public String getStockName() { return stockName; } - public String getPrice() { - return price; - } } diff --git a/backend/src/main/java/com/example/backend/repository/DailyStockPriceRepository.java b/backend/src/main/java/com/example/backend/repository/DailyStockPriceRepository.java new file mode 100644 index 0000000..a8000fb --- /dev/null +++ b/backend/src/main/java/com/example/backend/repository/DailyStockPriceRepository.java @@ -0,0 +1,7 @@ +package com.example.backend.repository; + +import com.example.backend.entity.DailyStockPrice; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface DailyStockPriceRepository extends JpaRepository { +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/repository/LiveDeterminedRepository.java b/backend/src/main/java/com/example/backend/repository/LiveDeterminedRepository.java new file mode 100644 index 0000000..60ec704 --- /dev/null +++ b/backend/src/main/java/com/example/backend/repository/LiveDeterminedRepository.java @@ -0,0 +1,7 @@ +package com.example.backend.repository; + +import com.example.backend.entity.LiveDetermined; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface LiveDeterminedRepository extends JpaRepository { +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/repository/PopularRepository.java b/backend/src/main/java/com/example/backend/repository/PopularRepository.java new file mode 100644 index 0000000..0c96fe5 --- /dev/null +++ b/backend/src/main/java/com/example/backend/repository/PopularRepository.java @@ -0,0 +1,7 @@ +package com.example.backend.repository; + +import com.example.backend.entity.Popular; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PopularRepository extends JpaRepository { +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/repository/StockRepository.java b/backend/src/main/java/com/example/backend/repository/StockRepository.java index b16399a..e537385 100644 --- a/backend/src/main/java/com/example/backend/repository/StockRepository.java +++ b/backend/src/main/java/com/example/backend/repository/StockRepository.java @@ -3,5 +3,5 @@ import com.example.backend.entity.Stock; import org.springframework.data.jpa.repository.JpaRepository; -public interface StockRepository extends JpaRepository { +public interface StockRepository extends JpaRepository { } diff --git a/backend/src/main/java/com/example/backend/repository/TokenRepository.java b/backend/src/main/java/com/example/backend/repository/TokenRepository.java new file mode 100644 index 0000000..1cfeee0 --- /dev/null +++ b/backend/src/main/java/com/example/backend/repository/TokenRepository.java @@ -0,0 +1,17 @@ +package com.example.backend.repository; + +import com.example.backend.entity.APIToken; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.Optional; + +public interface TokenRepository extends JpaRepository { + + // 특정 토큰 값으로 조회 + Optional findByTokenValue(String tokenValue); + + // 만료된 토큰 삭제 + @Query("DELETE FROM TokenStore t WHERE t.expirationTime < CURRENT_TIMESTAMP") + void deleteExpiredTokens(); +} \ No newline at end of file From daef0d3338c8e44ad9d3100065d99c1449f157b2 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Wed, 18 Dec 2024 14:48:42 +0900 Subject: [PATCH 04/10] fix: add package above file --- .../java/com/example/backend/controller/APITokenController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/main/java/com/example/backend/controller/APITokenController.java b/backend/src/main/java/com/example/backend/controller/APITokenController.java index 4b5b465..24be531 100644 --- a/backend/src/main/java/com/example/backend/controller/APITokenController.java +++ b/backend/src/main/java/com/example/backend/controller/APITokenController.java @@ -1,3 +1,4 @@ +package com.example.backend.controller; import com.example.backend.entity.APIToken; import com.example.backend.repository.TokenRepository; import org.springframework.beans.factory.annotation.Autowired; From fae654c962e66e9cf5d5fa31e8076e3f641ee516 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Wed, 18 Dec 2024 14:49:56 +0900 Subject: [PATCH 05/10] add: add stock_id in Stock Entity & add findById in StockRepository --- .../main/java/com/example/backend/entity/Stock.java | 10 +++++++--- .../example/backend/repository/StockRepository.java | 7 ++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/com/example/backend/entity/Stock.java b/backend/src/main/java/com/example/backend/entity/Stock.java index abf7c0b..9bcf5ef 100644 --- a/backend/src/main/java/com/example/backend/entity/Stock.java +++ b/backend/src/main/java/com/example/backend/entity/Stock.java @@ -8,18 +8,22 @@ public class Stock { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private String id; + private int id; @Column(name = "stock_name") private String stockName; + @Column(name = "stock_id") + private String stockId; + public Stock() {} - public Stock(String stockName) { + public Stock(String stockName, String stockId) { this.stockName = stockName; + this.stockId = stockId; } - public String getId() { + public Integer getId() { return id; } public String getStockName() { diff --git a/backend/src/main/java/com/example/backend/repository/StockRepository.java b/backend/src/main/java/com/example/backend/repository/StockRepository.java index e537385..f22b7ea 100644 --- a/backend/src/main/java/com/example/backend/repository/StockRepository.java +++ b/backend/src/main/java/com/example/backend/repository/StockRepository.java @@ -1,7 +1,12 @@ package com.example.backend.repository; import com.example.backend.entity.Stock; + +import java.util.Optional; + import org.springframework.data.jpa.repository.JpaRepository; -public interface StockRepository extends JpaRepository { +public interface StockRepository extends JpaRepository { + + Optional findByStockId(String stockId); } From 6b4401a549890d07394742af65c54197df123c92 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Wed, 18 Dec 2024 15:02:12 +0900 Subject: [PATCH 06/10] fix: rename TokenRepository to APITokenRepository --- .../{TokenRepository.java => APITokenRepository.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename backend/src/main/java/com/example/backend/repository/{TokenRepository.java => APITokenRepository.java} (71%) diff --git a/backend/src/main/java/com/example/backend/repository/TokenRepository.java b/backend/src/main/java/com/example/backend/repository/APITokenRepository.java similarity index 71% rename from backend/src/main/java/com/example/backend/repository/TokenRepository.java rename to backend/src/main/java/com/example/backend/repository/APITokenRepository.java index 1cfeee0..0b9af39 100644 --- a/backend/src/main/java/com/example/backend/repository/TokenRepository.java +++ b/backend/src/main/java/com/example/backend/repository/APITokenRepository.java @@ -6,12 +6,12 @@ import java.util.Optional; -public interface TokenRepository extends JpaRepository { +public interface APITokenRepository extends JpaRepository { // 특정 토큰 값으로 조회 Optional findByTokenValue(String tokenValue); // 만료된 토큰 삭제 - @Query("DELETE FROM TokenStore t WHERE t.expirationTime < CURRENT_TIMESTAMP") + @Query("DELETE FROM Token t WHERE t.expirationTime < CURRENT_TIMESTAMP") void deleteExpiredTokens(); } \ No newline at end of file From 3c06009eaeeea728aea5446ed9584e34a184161e Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Wed, 18 Dec 2024 15:02:29 +0900 Subject: [PATCH 07/10] delete: delete userId --- .../com/example/backend/controller/APITokenController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/example/backend/controller/APITokenController.java b/backend/src/main/java/com/example/backend/controller/APITokenController.java index 24be531..e06421b 100644 --- a/backend/src/main/java/com/example/backend/controller/APITokenController.java +++ b/backend/src/main/java/com/example/backend/controller/APITokenController.java @@ -1,6 +1,6 @@ package com.example.backend.controller; import com.example.backend.entity.APIToken; -import com.example.backend.repository.TokenRepository; +import com.example.backend.repository.APITokenRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -12,7 +12,7 @@ public class APITokenController { @Autowired - private TokenRepository tokenRepository; + private APITokenRepository tokenRepository; // 1. 토큰 저장 @PostMapping("/save") @@ -24,7 +24,7 @@ public String saveToken(@RequestParam String userId, @RequestParam String tokenV token.setExpirationTime(LocalDateTime.now().plusHours(24)); tokenRepository.save(token); - return "Token saved for user: " + userId + ", expires at: " + token.getExpirationTime(); + return "expires at: " + token.getExpirationTime(); } // 2. 특정 토큰 값으로 유효성 확인 From 6d19c540332ee2a09b8cf96e9dd563d3bcbbaff8 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Thu, 19 Dec 2024 11:27:50 +0900 Subject: [PATCH 08/10] fix: fix Token to APIToken --- .../com/example/backend/controller/APITokenController.java | 2 +- .../com/example/backend/repository/APITokenRepository.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/example/backend/controller/APITokenController.java b/backend/src/main/java/com/example/backend/controller/APITokenController.java index e06421b..e51a360 100644 --- a/backend/src/main/java/com/example/backend/controller/APITokenController.java +++ b/backend/src/main/java/com/example/backend/controller/APITokenController.java @@ -47,7 +47,7 @@ public String validateToken(@RequestParam String tokenValue) { // 3. 만료된 토큰 삭제 @DeleteMapping("/delete-expired") public String deleteExpiredTokens() { - tokenRepository.deleteExpiredTokens(); + tokenRepository.deleteExpiredAPITokens(); return "Expired tokens deleted!"; } } \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/repository/APITokenRepository.java b/backend/src/main/java/com/example/backend/repository/APITokenRepository.java index 0b9af39..0980fbd 100644 --- a/backend/src/main/java/com/example/backend/repository/APITokenRepository.java +++ b/backend/src/main/java/com/example/backend/repository/APITokenRepository.java @@ -12,6 +12,6 @@ public interface APITokenRepository extends JpaRepository { Optional findByTokenValue(String tokenValue); // 만료된 토큰 삭제 - @Query("DELETE FROM Token t WHERE t.expirationTime < CURRENT_TIMESTAMP") - void deleteExpiredTokens(); + @Query("DELETE FROM APIToken t WHERE t.expirationTime < CURRENT_TIMESTAMP") + void deleteExpiredAPITokens(); } \ No newline at end of file From 115978678f8349a04657f598653c83a6e69400f8 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Thu, 19 Dec 2024 11:29:12 +0900 Subject: [PATCH 09/10] update: MySQL Entity & Controller --- .../backend/controller/DBController.java | 63 --------------- .../backend/controller/HelloController.java | 13 ---- .../backend/controller/RDSController.java | 76 +++++++++++++++++++ .../com/example/backend/entity/Stock.java | 26 ++++++- .../backend/repository/StockRepository.java | 15 ++-- 5 files changed, 108 insertions(+), 85 deletions(-) delete mode 100644 backend/src/main/java/com/example/backend/controller/DBController.java delete mode 100644 backend/src/main/java/com/example/backend/controller/HelloController.java create mode 100644 backend/src/main/java/com/example/backend/controller/RDSController.java diff --git a/backend/src/main/java/com/example/backend/controller/DBController.java b/backend/src/main/java/com/example/backend/controller/DBController.java deleted file mode 100644 index bba7cb7..0000000 --- a/backend/src/main/java/com/example/backend/controller/DBController.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.example.backend.controller; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -// MySQL -import com.example.backend.repository.StockRepository; -import com.example.backend.entity.Stock; -// Redis -import org.springframework.data.redis.core.StringRedisTemplate; -import java.util.Optional; -import java.util.List; - -@RestController -public class DBController { - - @Autowired - private StringRedisTemplate redisTemplate; - - @GetMapping("/redis-test") - public String testRedis() { - redisTemplate.opsForValue().set("testKey", "Hello from Redis!"); - return redisTemplate.opsForValue().get("testKey"); - } - - @Autowired - private StockRepository stockRepository; - - // 1. 데이터 저장 (Save) - @GetMapping("/save-stock") - public String saveStock() { - Stock stock = new Stock("005930"); - stockRepository.save(stock); - return "Stock data saved to RDS!"; - } - - // 2. ID로 데이터 조회 (FindById) - @GetMapping("/find-stock/{id}") - public String findStockById(@PathVariable Long id) { - Optional stock = stockRepository.findById(id); - return stock.map(s -> "Found: " + s.getStockName()) - .orElse("Stock not found with ID: " + id); - } - - // 3. 모든 데이터 조회 (FindAll) - @GetMapping("/find-all-stocks") - public List findAllStocks() { - return stockRepository.findAll(); - } - - // 4. ID로 데이터 삭제 (DeleteById) - @GetMapping("/delete-stock/{id}") - public String deleteStockById(@PathVariable Long id) { - if (stockRepository.existsById(id)) { - stockRepository.deleteById(id); - return "Stock with ID: " + id + " has been deleted."; - } else { - return "Stock not found with ID: " + id; - } - } -} diff --git a/backend/src/main/java/com/example/backend/controller/HelloController.java b/backend/src/main/java/com/example/backend/controller/HelloController.java deleted file mode 100644 index 159ea04..0000000 --- a/backend/src/main/java/com/example/backend/controller/HelloController.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.backend.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class HelloController { - - @GetMapping("/hello") - public String sayHello() { - return "Hello, Spring Boot!"; - } -} diff --git a/backend/src/main/java/com/example/backend/controller/RDSController.java b/backend/src/main/java/com/example/backend/controller/RDSController.java new file mode 100644 index 0000000..ac3f3b1 --- /dev/null +++ b/backend/src/main/java/com/example/backend/controller/RDSController.java @@ -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> getAllStocks() { + List stocks = stockRepository.findAll(); + return ResponseEntity.ok(stocks); + } + // 모든 주식 조회 + // curl -X GET http://localhost:8080/stocks/rds + + @GetMapping("/{id}") + public ResponseEntity 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 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 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 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 + +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/entity/Stock.java b/backend/src/main/java/com/example/backend/entity/Stock.java index 9bcf5ef..d7575fb 100644 --- a/backend/src/main/java/com/example/backend/entity/Stock.java +++ b/backend/src/main/java/com/example/backend/entity/Stock.java @@ -10,13 +10,19 @@ public class Stock { @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; - @Column(name = "stock_name") + @Column(name = "stock_name", nullable = false) private String stockName; - @Column(name = "stock_id") + @Column(name = "stock_id", nullable = false, unique = true) private String stockId; - public Stock() {} + protected Stock() { + // JPA에서 기본 생성자 필요 + } + + public Stock(String stockId) { + this.stockId = stockId; + } public Stock(String stockName, String stockId) { this.stockName = stockName; @@ -26,8 +32,20 @@ public Stock(String stockName, String 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; + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/example/backend/repository/StockRepository.java b/backend/src/main/java/com/example/backend/repository/StockRepository.java index f22b7ea..158c967 100644 --- a/backend/src/main/java/com/example/backend/repository/StockRepository.java +++ b/backend/src/main/java/com/example/backend/repository/StockRepository.java @@ -1,12 +1,17 @@ package com.example.backend.repository; import com.example.backend.entity.Stock; - -import java.util.Optional; - import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import java.util.List; + +@Repository public interface StockRepository extends JpaRepository { - Optional findByStockId(String stockId); -} + // 특정 주식 ID로 조회 + List findByStockId(String stockId); + + // 특정 주식 이름으로 조회 + List findByStockName(String stockName); +} \ No newline at end of file From 00b9981ba6c6560f05e908ae00e801dcd45b0880 Mon Sep 17 00:00:00 2001 From: kpryu6 Date: Thu, 19 Dec 2024 11:30:16 +0900 Subject: [PATCH 10/10] feat: seperate redis & mysql controller --- .../backend/controller/RedisController.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 backend/src/main/java/com/example/backend/controller/RedisController.java diff --git a/backend/src/main/java/com/example/backend/controller/RedisController.java b/backend/src/main/java/com/example/backend/controller/RedisController.java new file mode 100644 index 0000000..3471298 --- /dev/null +++ b/backend/src/main/java/com/example/backend/controller/RedisController.java @@ -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"); + } + +}