Skip to content

Commit

Permalink
feat: also export dropped dates and currently reading #24
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Aug 20, 2022
1 parent 2277c94 commit d81afbe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CsvExportService(
logger.debug { "target export file at ${destFile.absolutePath}" }
try {
CSVPrinter(BufferedWriter(FileWriter(destFile)), format).use { printer ->
printer.printRecord("Title", "Author", "ISBN", "Publisher", "Date Read", "Shelves", "Bookshelves", "read_dates", "tags", "authors", "isbn10", "isbn13", "owned")
printer.printRecord("Title", "Author", "ISBN", "Publisher", "Date Read", "Shelves", "Bookshelves", "read_dates", "tags", "authors", "isbn10", "isbn13", "owned", "dropped_dates", "currently_reading")
do {
books = bookService.findUserBookByCriteria(userId, null, null, null, null, PageRequest.of(currentPage, pageSize))
currentPage ++
Expand Down Expand Up @@ -122,20 +122,22 @@ class CsvExportService(
dateRead(it),
shelves(it),
bookShelves(it),
readDates(it, userId),
listOfDatesForEvent(it, userId, ReadingEventType.FINISHED),
tags(it),
authors(it),
if (it.book.isbn10.isNullOrBlank()) "" else it.book.isbn10,
if (it.book.isbn13.isNullOrBlank()) "" else it.book.isbn13,
if (it.owned == true) "true" else "",
listOfDatesForEvent(it, userId, ReadingEventType.DROPPED),
listOfDatesForEvent(it, userId, ReadingEventType.CURRENTLY_READING),
)
}
}

fun isbn(userbook: UserBookWithoutEventsAndUserDto): String {
return if (userbook.book.isbn13 != null && userbook.book.isbn13.isNotBlank()) {
return if (!userbook.book.isbn13.isNullOrBlank()) {
userbook.book.isbn13
} else if (userbook.book.isbn10 != null && userbook.book.isbn10.isNotBlank()) {
} else if (!userbook.book.isbn10.isNullOrBlank()) {
userbook.book.isbn10
} else {
""
Expand Down Expand Up @@ -178,8 +180,8 @@ class CsvExportService(
}
}

fun readDates(userbook: UserBookWithoutEventsAndUserDto, userId: UUID): String {
val reads = readingEventService.findAll(listOf(ReadingEventType.FINISHED), userId, userbook.book.id, null, null, Pageable.ofSize(100))
fun listOfDatesForEvent(userbook: UserBookWithoutEventsAndUserDto, userId: UUID, eventType: ReadingEventType): String {
val reads = readingEventService.findAll(listOf(eventType), userId, userbook.book.id, null, null, Pageable.ofSize(100))
if (! reads.isEmpty) {
return reads.content.stream().map { toDateString(it.modificationDate) }.collect(Collectors.joining(","))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class CsvExportServiceTest(
percentRead = null,
book = book1
)
bookService.save(createUserBookDto1, user(), null)
val saved1: UserBookLightDto = bookService.save(createUserBookDto1, user(), null)

val book2 = BookCreateDto(
id = null,
Expand Down Expand Up @@ -152,6 +152,31 @@ class CsvExportServiceTest(
),
user()
)

readingEventService.save(
CreateReadingEventDto(
ReadingEventType.DROPPED,
saved1.book.id,
date3.toInstant(),
),
user()
)
readingEventService.save(
CreateReadingEventDto(
ReadingEventType.DROPPED,
saved1.book.id,
date2.toInstant(),
),
user()
)
readingEventService.save(
CreateReadingEventDto(
ReadingEventType.CURRENTLY_READING,
saved1.book.id,
date1.toInstant(),
),
user()
)
csvExportService.export(user(), Locale.ENGLISH)
val csv = File(jeluProperties.files.imports).listFiles()[0]
var content = Files.contentOf(csv, Charsets.UTF_8)
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/csv-export/expected.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Title,Author,ISBN,Publisher,Date Read,Shelves,Bookshelves,read_dates,tags,authors,isbn10,isbn13,owned
book2,test author,9781566199094,test-publisher,2022/02/10,,,"2022/02/10,2021/02/10,2020/02/10",,test author,1566199093,9781566199094,true
book1,test author,9781566199094,test-publisher,,to-read,sciencefiction fantasy,,"science fiction,fantasy","test author,author2 name",1566199093,9781566199094,
Title,Author,ISBN,Publisher,Date Read,Shelves,Bookshelves,read_dates,tags,authors,isbn10,isbn13,owned,dropped_dates,currently_reading
book1,test author,9781566199094,test-publisher,2022/02/10,currently-reading,sciencefiction fantasy,,"science fiction,fantasy","test author,author2 name",1566199093,9781566199094,,"2021/02/10,2020/02/10",2022/02/10
book2,test author,9781566199094,test-publisher,2022/02/10,,,"2022/02/10,2021/02/10,2020/02/10",,test author,1566199093,9781566199094,true,,

0 comments on commit d81afbe

Please sign in to comment.