Skip to content

Commit 4e5dcd8

Browse files
committed
build: API endpoints for downloading sample files
1 parent 31b50dc commit 4e5dcd8

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

src/main/java/com/budlib/api/controller/DashboardController.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,140 @@ public ResponseEntity<?> exportTransactions() {
404404
.body(new ErrorBody(HttpStatus.GATEWAY_TIMEOUT, message));
405405
}
406406
}
407+
408+
/**
409+
* Download the sample file to import the books in the database
410+
*
411+
* @return file containing sample books in CSV format
412+
*/
413+
@GetMapping(path = "batch/samplebooks")
414+
public ResponseEntity<?> showImportSampleBooks() {
415+
String[] bookHeaders = { "title", "subtitle", "authors", "publisher", "edition", "year", "language", "isbn10",
416+
"isbn13", "librarySection", "totalQuantity", "availableQuantity", "notes", "imageLink", "retailPrice",
417+
"libraryPrice" };
418+
419+
try {
420+
Path tempPath = Files.createTempFile("sample_books_import", ".csv");
421+
File tempFile = tempPath.toFile();
422+
423+
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(tempFile, false)), true);
424+
CSVPrinter printer = new CSVPrinter(pw, CSVFormat.DEFAULT.withHeader(bookHeaders));
425+
426+
printer.printRecord(
427+
"How to Stop Losing Your Sh*t with Your Kids",
428+
"A Practical Guide to Becoming a Calmer, Happier Parent",
429+
"Carla Naumburg",
430+
"Workman Publishing Company",
431+
"",
432+
"2019-08-20",
433+
"en",
434+
"1523505427",
435+
"9781523505425",
436+
"PARENT_LIBRARY",
437+
5,
438+
5,
439+
"",
440+
"https://images-na.ssl-images-amazon.com/images/I/51TFx51B-FL._SX357_BO1,204,203,200_.jpg",
441+
21.73,
442+
19);
443+
printer.printRecord(
444+
"I Spy - Everything!",
445+
"A Fun Guessing Game for 2-4 Year Olds",
446+
"Books For Little Ones",
447+
"",
448+
"",
449+
"2018-03-19",
450+
"en",
451+
"1980596743",
452+
"9781980596745",
453+
"CHILDREN_LIBRARY",
454+
7,
455+
7,
456+
"",
457+
"https://m.media-amazon.com/images/P/1980596743.01._SCLZZZZZZZ_SX500_.jpg",
458+
10.8,
459+
5.99);
460+
printer.printRecord(
461+
"Student Engagement Techniques",
462+
"A Handbook for College Faculty",
463+
"Elizabeth F. Barkley,Claire H. Major",
464+
"John Wiley & Sons",
465+
"",
466+
"2020-05-05",
467+
"en",
468+
"1119686776",
469+
"9781119686774",
470+
"FACULTY_LIBRARY",
471+
2,
472+
2,
473+
"",
474+
"https://images-na.ssl-images-amazon.com/images/I/81G8F2OV9SL.jpg",
475+
53.45,
476+
50.5);
477+
printer.printRecord(
478+
"Classes Are Canceled!",
479+
"",
480+
"Jack Chabert",
481+
"Eerie Elementary",
482+
"",
483+
"2017",
484+
"en",
485+
"1338181807",
486+
"9781338181807",
487+
"CHILDREN_LIBRARY",
488+
15,
489+
15,
490+
"",
491+
"https://images-na.ssl-images-amazon.com/images/I/81vHq-mWzWL.jpg",
492+
7.91,
493+
2.99);
494+
495+
printer.close();
496+
pw.close();
497+
498+
return fileDownloader(tempFile.getAbsolutePath());
499+
}
500+
501+
catch (IOException e) {
502+
String message = "Error while processing export request";
503+
return ResponseEntity.status(HttpStatus.GATEWAY_TIMEOUT)
504+
.body(new ErrorBody(HttpStatus.GATEWAY_TIMEOUT, message));
505+
}
506+
}
507+
508+
/**
509+
* Download the sample file to import the loaners in the database
510+
*
511+
* @return file containing sample loaners in CSV format
512+
*/
513+
@GetMapping(path = "batch/sampleloaners")
514+
public ResponseEntity<?> showImportSampleLoaners() {
515+
String[] loanerHeaders = { "schoolId", "isStudent", "email", "salutation", "first_name",
516+
"middle_name", "last_name", "mother_name", "father_name" };
517+
518+
try {
519+
Path tempPath = Files.createTempFile("sample_loaners_import", ".csv");
520+
File tempFile = tempPath.toFile();
521+
522+
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(tempFile, false)), true);
523+
CSVPrinter printer = new CSVPrinter(pw, CSVFormat.DEFAULT.withHeader(loanerHeaders));
524+
525+
printer.printRecord("STU-1001", true, "", "Miss", "Josephine", "", "Murray", "Yazmin Murray", "Adonis Murray");
526+
printer.printRecord("STU-1002", true, "", "Mr", "Mildred", "", "Gonzales", "Micaela Gonzales", "Rhett Gonzales");
527+
printer.printRecord("STU-1003", true, "", "Miss", "Loretta", "", "Carrillo", "Esperanza Carrillo", "Roland Carrillo");
528+
printer.printRecord("FAC-1001", false, "", "Mr", "Dwight", "", "Schrute", "", "");
529+
printer.printRecord("FAC-1002", false, "", "Miss", "Angela", "", "Martin", "", "");
530+
531+
printer.close();
532+
pw.close();
533+
534+
return fileDownloader(tempFile.getAbsolutePath());
535+
}
536+
537+
catch (IOException e) {
538+
String message = "Error while processing export request";
539+
return ResponseEntity.status(HttpStatus.GATEWAY_TIMEOUT)
540+
.body(new ErrorBody(HttpStatus.GATEWAY_TIMEOUT, message));
541+
}
542+
}
407543
}

0 commit comments

Comments
 (0)