33import org .apache .commons .csv .CSVPrinter ;
44import org .apache .commons .csv .CSVFormat ;
55
6- import org .springframework .http .HttpStatus ;
7- import org .springframework .http .ResponseEntity ;
6+ import org .springframework .http .*;
87import org .springframework .core .io .Resource ;
9- import org .springframework .http .HttpHeaders ;
108import 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 .*;
1710import org .springframework .boot .info .BuildProperties ;
1811
1912import java .util .List ;
2619import java .text .SimpleDateFormat ;
2720import java .text .DecimalFormat ;
2821
29- import com .cloudacademy .stocks .dto .StockRecord ;
3022import 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+ }
0 commit comments