Skip to content

Commit 65dd198

Browse files
author
student
committed
updated to work with JDK25
1 parent 4a279dc commit 65dd198

File tree

10 files changed

+185
-185
lines changed

10 files changed

+185
-185
lines changed

stocks-api/pom.xml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.cloudacademy</groupId>
1313
<artifactId>stocks</artifactId>
14-
<version>1.0.1-SNAPSHOT</version>
14+
<version>1.0.8-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616

1717
<name>${project.artifactId}</name>
@@ -63,12 +63,6 @@
6363
<artifactId>commons-csv</artifactId>
6464
<version>1.10.0</version>
6565
</dependency>
66-
<dependency>
67-
<groupId>org.projectlombok</groupId>
68-
<artifactId>lombok</artifactId>
69-
<version>1.18.42</version>
70-
<scope>compile</scope>
71-
</dependency>
7266
<dependency>
7367
<groupId>org.springframework.boot</groupId>
7468
<artifactId>spring-boot-starter-test</artifactId>
@@ -89,20 +83,6 @@
8983
</execution>
9084
</executions>
9185
</plugin>
92-
<plugin>
93-
<groupId>org.apache.maven.plugins</groupId>
94-
<artifactId>maven-compiler-plugin</artifactId>
95-
<version>3.13.0</version>
96-
<configuration>
97-
<annotationProcessorPaths>
98-
<path>
99-
<groupId>org.projectlombok</groupId>
100-
<artifactId>lombok</artifactId>
101-
<version>1.18.42</version>
102-
</path>
103-
</annotationProcessorPaths>
104-
</configuration>
105-
</plugin>
10686
</plugins>
10787
</build>
10888

stocks-api/src/main/java/com/cloudacademy/stocks/controller/StockController.java

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@
33
import org.apache.commons.csv.CSVPrinter;
44
import org.apache.commons.csv.CSVFormat;
55

6-
import org.springframework.http.HttpStatus;
7-
import org.springframework.http.ResponseEntity;
6+
import org.springframework.http.*;
87
import org.springframework.core.io.Resource;
9-
import org.springframework.http.HttpHeaders;
108
import org.springframework.core.io.InputStreamResource;
11-
import org.springframework.web.bind.annotation.CrossOrigin;
12-
import org.springframework.web.bind.annotation.RestController;
13-
import org.springframework.web.bind.annotation.RequestMapping;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.GetMapping;
16-
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.*;
1710
import org.springframework.boot.info.BuildProperties;
1811

1912
import java.util.List;
@@ -26,8 +19,8 @@
2619
import java.text.SimpleDateFormat;
2720
import java.text.DecimalFormat;
2821

29-
import com.cloudacademy.stocks.dto.StockRecord;
3022
import com.cloudacademy.stocks.service.StockService;
23+
import com.cloudacademy.stocks.model.StockDTO;
3124

3225
@RestController
3326
@RequestMapping("api/stocks")
@@ -41,22 +34,12 @@ public StockController(StockService stockService, BuildProperties buildPropertie
4134
this.buildProperties = buildProperties;
4235
}
4336

44-
// build create Stock REST API
45-
@PostMapping
46-
public ResponseEntity<StockRecord> createStock(@RequestBody StockRecord stockRecord) {
47-
var savedStock = stockService.createStock(stockRecord.toEntity());
48-
return new ResponseEntity<>(StockRecord.fromEntity(savedStock), HttpStatus.CREATED);
49-
}
50-
51-
// http://localhost:8080/api/stocks
52-
@CrossOrigin(origins = "*")
53-
@GetMapping
54-
public ResponseEntity<List<StockRecord>> getAllStocks() {
55-
var stocks = stockService.getAllStocks()
56-
.stream()
57-
.map(StockRecord::fromEntity)
58-
.toList();
59-
return new ResponseEntity<>(stocks, HttpStatus.OK);
37+
// http://localhost:8080/api/stocks/version
38+
@GetMapping(value = "/version", produces = "text/plain")
39+
public ResponseEntity<String> getVersion() {
40+
return ResponseEntity
41+
.status(HttpStatus.OK)
42+
.body(buildProperties.getVersion());
6043
}
6144

6245
// http://localhost:8080/api/stocks/ok
@@ -66,74 +49,75 @@ public ResponseEntity<String> getOk() {
6649
return new ResponseEntity<>("OK", HttpStatus.OK);
6750
}
6851

69-
// http://localhost:8080/api/version
70-
@GetMapping(value = "/version", produces = "text/plain")
71-
public ResponseEntity<String> getVersion() {
72-
return ResponseEntity
73-
.status(HttpStatus.OK)
74-
.body(buildProperties.getVersion());
52+
// http://localhost:8080/api/stocks
53+
@CrossOrigin(origins = "*")
54+
@GetMapping
55+
public List<StockDTO> getAllStocks() {
56+
return stockService.getAllStocks();
57+
}
58+
59+
// http://localhost:8080/api/stocks
60+
@CrossOrigin(origins = "*")
61+
@PostMapping
62+
public StockDTO createEmployee(@RequestBody StockDTO stockDTO) {
63+
return stockService.saveStock(stockDTO);
7564
}
7665

7766
// http://localhost:8080/api/stocks/csv
7867
@CrossOrigin(origins = "*")
7968
@GetMapping(value = "/csv", produces = "text/csv")
8069
public ResponseEntity<Resource> exportCSV() {
8170
// replace this with your header (if required)
82-
String[] csvHeader = {
71+
var csvHeader = new String[] {
8372
"date", "open", "high", "low", "close", "volume"
8473
};
8574

8675
// replace this with your data retrieving logic
87-
List<List<String>> csvBody = new ArrayList<>();
88-
var stocks = stockService.getAllStocks()
89-
.stream()
90-
.map(StockRecord::fromEntity)
91-
.toList();
76+
var csvBody = new ArrayList<List<String>>();
77+
var stocks = stockService.getAllStocks();
9278

9379
var dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
9480
var numFormatter = new DecimalFormat("##.000000");
9581

96-
for (var stock : stocks) {
82+
stocks.forEach(stock -> {
9783
csvBody.add(Arrays.asList(
9884
dateFormatter.format(stock.date()),
9985
numFormatter.format(stock.open()),
10086
numFormatter.format(stock.high()),
10187
numFormatter.format(stock.low()),
10288
numFormatter.format(stock.close()),
10389
stock.volume().toString()));
104-
}
90+
});
10591

106-
ByteArrayInputStream byteArrayOutputStream;
92+
var csvFormat = CSVFormat.DEFAULT.builder()
93+
.setHeader(csvHeader)
94+
.build();
10795

10896
try (
10997
var out = new ByteArrayOutputStream();
110-
111-
var csvPrinter = new CSVPrinter(
112-
new PrintWriter(out),
113-
CSVFormat.DEFAULT.withHeader(csvHeader));) {
114-
for (var record : csvBody) {
115-
csvPrinter.printRecord(record);
116-
}
117-
118-
// writing the underlying stream
119-
csvPrinter.flush();
120-
121-
byteArrayOutputStream = new ByteArrayInputStream(out.toByteArray());
98+
var printer = new CSVPrinter(new PrintWriter(out), csvFormat)) {
99+
csvBody.forEach(row -> {
100+
try {
101+
printer.printRecord(row);
102+
} catch (IOException e) {
103+
e.printStackTrace();
104+
}
105+
});
106+
107+
printer.flush();
108+
109+
var stream = new ByteArrayInputStream(out.toByteArray());
110+
111+
var headers = new HttpHeaders();
112+
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=stocks.csv");
113+
headers.set(HttpHeaders.CONTENT_TYPE, "text/csv");
114+
115+
return new ResponseEntity<>(
116+
new InputStreamResource(stream),
117+
headers,
118+
HttpStatus.OK);
122119
} catch (IOException e) {
123-
throw new RuntimeException(e.getMessage());
120+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
124121
}
125-
126-
var fileInputStream = new InputStreamResource(byteArrayOutputStream);
127-
128-
var csvFileName = "stocks.csv";
129-
130-
HttpHeaders headers = new HttpHeaders();
131-
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + csvFileName);
132-
headers.set(HttpHeaders.CONTENT_TYPE, "text/csv");
133-
134-
return new ResponseEntity<>(
135-
fileInputStream,
136-
headers,
137-
HttpStatus.OK);
138122
}
139-
}
123+
}

stocks-api/src/main/java/com/cloudacademy/stocks/dto/StockRecord.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

stocks-api/src/main/java/com/cloudacademy/stocks/entity/Stock.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.cloudacademy.stocks.model;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Table;
5+
import jakarta.persistence.Id;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.GenerationType;
8+
import jakarta.persistence.Column;
9+
10+
import java.util.Date;
11+
12+
@Entity
13+
@Table(name = "stocks")
14+
public class Stock {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private Long id;
19+
@Column(nullable = false, unique = true)
20+
private Date date;
21+
@Column(nullable = false)
22+
private Double open;
23+
@Column(nullable = false)
24+
private Double high;
25+
@Column(nullable = false)
26+
private Double low;
27+
@Column(nullable = false)
28+
private Double close;
29+
@Column(nullable = false)
30+
private Integer volume;
31+
32+
public Long getId() { return id; }
33+
public void setId(Long id) { this.id = id; }
34+
35+
public Date getDate() { return date; }
36+
public void setDate(Date date) { this.date = date; }
37+
38+
public Double getOpen() { return open; }
39+
public void setOpen(Double open) { this.open = open; }
40+
41+
public Double getHigh() { return high; }
42+
public void setHigh(Double high) { this.high = high; }
43+
44+
public Double getLow() { return low; }
45+
public void setLow(Double low) { this.low = low; }
46+
47+
public Double getClose() { return close; }
48+
public void setClose(Double close) { this.close = close; }
49+
50+
public Integer getVolume() { return volume; }
51+
public void setVolume(Integer volume) { this.volume = volume; }
52+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.cloudacademy.stocks.model;
2+
3+
import java.util.Date;
4+
5+
public record StockDTO(Long id, Date date, Double open, Double high, Double low, Double close, Integer volume) {}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.cloudacademy.stocks.repo;
22

33
import org.springframework.data.jpa.repository.JpaRepository;
4-
import com.cloudacademy.stocks.entity.Stock;
4+
import org.springframework.stereotype.Repository;
55

6+
import com.cloudacademy.stocks.model.Stock;
7+
8+
@Repository
69
public interface StockRepository extends JpaRepository<Stock, Long> {
710
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.cloudacademy.stocks.service;
22

3+
import com.cloudacademy.stocks.model.StockDTO;
34
import java.util.List;
4-
5-
import com.cloudacademy.stocks.entity.Stock;
5+
import java.util.Optional;
66

77
public interface StockService {
8-
Stock createStock(Stock stock);
9-
10-
List<Stock> getAllStocks();
8+
List<StockDTO> getAllStocks();
9+
Optional<StockDTO> getStockById(Long id);
10+
StockDTO saveStock(StockDTO employeeDTO);
11+
StockDTO updateStock(Long id, StockDTO employeeDTO);
12+
void deleteStock(Long id);
1113
}

0 commit comments

Comments
 (0)