Skip to content

Commit

Permalink
refactor: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed May 1, 2022
1 parent 19e0a58 commit 8737192
Show file tree
Hide file tree
Showing 12 changed files with 1,414 additions and 119 deletions.
98 changes: 73 additions & 25 deletions src/main/kotlin/io/github/bayang/jelu/dao/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,46 @@ class BookRepository(
if (!book.title.isNullOrBlank()) {
updated.title = book.title.trim()
}
updated.isbn10 = book.isbn10?.trim()
updated.isbn13 = book.isbn13?.trim()
updated.pageCount = book.pageCount
updated.publisher = book.publisher?.trim()
book.isbn10?.let {
updated.isbn10 = book.isbn10.trim()
}
book.isbn13?.let {
updated.isbn13 = book.isbn13.trim()
}
book.pageCount?.let {
updated.pageCount = book.pageCount
}
book.publisher?.let {
updated.publisher = book.publisher.trim()
}
if (!book.summary.isNullOrBlank()) {
updated.summary = sanitizeHtml(book.summary)
} else {
updated.summary = book.summary
}
// image must be set when saving file succeeds
updated.publishedDate = book.publishedDate?.trim()
updated.series = book.series?.trim()
updated.numberInSeries = book.numberInSeries
updated.amazonId = book.amazonId?.trim()
updated.goodreadsId = book.goodreadsId?.trim()
updated.googleId = book.googleId?.trim()
updated.librarythingId = book.librarythingId?.trim()
updated.language = book.language?.trim()
book.publishedDate?.let {
updated.publishedDate = book.publishedDate.trim()
}
book.series?.let {
updated.series = book.series.trim()
}
book.numberInSeries?.let {
updated.numberInSeries = book.numberInSeries
}
book.amazonId?.let {
updated.amazonId = book.amazonId.trim()
}
book.goodreadsId?.let {
updated.goodreadsId = book.goodreadsId.trim()
}
book.googleId?.let {
updated.googleId = book.googleId.trim()
}
book.librarythingId?.let {
updated.librarythingId = book.librarythingId.trim()
}
book.language?.let {
updated.language = book.language.trim()
}
updated.modificationDate = nowInstant()
val authorsList = mutableListOf<Author>()
book.authors?.forEach {
Expand Down Expand Up @@ -333,7 +355,9 @@ class BookRepository(
if (book.owned != null) {
found.owned = book.owned
}
found.personalNotes = book.personalNotes?.trim()
book.personalNotes?.let {
found.personalNotes = book.personalNotes.trim()
}
if (book.toRead != null) {
found.toRead = book.toRead
}
Expand Down Expand Up @@ -361,16 +385,36 @@ class BookRepository(
if (!author.name.isNullOrBlank()) {
found.name = author.name.trim()
}
found.biography = author.biography?.trim()
found.dateOfDeath = author.dateOfDeath?.trim()
found.dateOfBirth = author.dateOfBirth?.trim()
found.notes = author.notes?.trim()
found.officialPage = author.officialPage?.trim()
found.wikipediaPage = author.wikipediaPage?.trim()
found.goodreadsPage = author.goodreadsPage?.trim()
found.twitterPage = author.twitterPage?.trim()
found.facebookPage = author.facebookPage?.trim()
found.instagramPage = author.instagramPage?.trim()
author.biography?.let {
found.biography = author.biography.trim()
}
author.dateOfDeath?.let {
found.dateOfDeath = author.dateOfDeath.trim()
}
author.dateOfBirth?.let {
found.dateOfBirth = author.dateOfBirth.trim()
}
author.notes?.let {
found.notes = author.notes.trim()
}
author.officialPage?.let {
found.officialPage = author.officialPage.trim()
}
author.wikipediaPage?.let {
found.wikipediaPage = author.wikipediaPage.trim()
}
author.goodreadsPage?.let {
found.goodreadsPage = author.goodreadsPage.trim()
}
author.twitterPage?.let {
found.twitterPage = author.twitterPage.trim()
}
author.facebookPage?.let {
found.facebookPage = author.facebookPage.trim()
}
author.instagramPage?.let {
found.instagramPage = author.instagramPage.trim()
}
// found.image = author.image?.trim()
found.modificationDate = nowInstant()
return found
Expand Down Expand Up @@ -537,6 +581,10 @@ class BookRepository(
Tag[tagId].delete()
}

/**
* Removes an author from a book without deleting the author from the database.
* Used to clean the composite table
*/
fun deleteAuthorFromBook(bookId: UUID, authorId: UUID) {
BookAuthors.deleteWhere {
BookAuthors.book eq bookId and(BookAuthors.author eq authorId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ class ReadingEventRepository {
val alreadyReadingEvent: ReadingEvent? =
userBook.readingEvents.find { it.eventType == ReadingEventType.CURRENTLY_READING }
val instant: Instant = nowInstant()
if (userBook.lastReadingEvent != null) {
if (createReadingEventDto.eventDate != null && createReadingEventDto.eventDate.isAfter(userBook.lastReadingEventDate)) {

// we have a previous event,
// only update lastReadingEvent if the new one has a date and is effectively after the one already existing
if (userBook.lastReadingEventDate != null && createReadingEventDto.eventDate != null) {
if (createReadingEventDto.eventDate.isAfter(userBook.lastReadingEventDate)) {
userBook.lastReadingEvent = createReadingEventDto.eventType
userBook.lastReadingEventDate = createReadingEventDto.eventDate
}
// no previous event, or the new one does not have a date set lastReadingEvent in any case
} else {
userBook.lastReadingEvent = createReadingEventDto.eventType
userBook.lastReadingEventDate = createReadingEventDto.eventDate ?: instant
Expand All @@ -90,7 +94,7 @@ class ReadingEventRepository {
alreadyReadingEvent.modificationDate = createReadingEventDto.eventDate ?: instant
return alreadyReadingEvent
}
// else : alreadyReadingEvent is not null and createReadingEventDto.eventDate is before an already existing CURRENTLY_READING EVENT
// FIXME else : alreadyReadingEvent is not null and createReadingEventDto.eventDate is before an already existing CURRENTLY_READING EVENT
// this could create CURRENTLY_READING events in the past
// should we allow this ? Let's wait and see for the moment, user can edit events in bookdetail page
}
Expand Down
34 changes: 32 additions & 2 deletions src/main/kotlin/io/github/bayang/jelu/service/BookService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ class BookService(
@Transactional
fun findAuthorsById(authorId: UUID): AuthorDto = bookRepository.findAuthorsById(authorId).toAuthorDto()

/**
* Image not updated, to add or update an image call the variant which accepts a MultiPartFile
*/
@Transactional
fun update(bookId: UUID, book: BookUpdateDto): BookDto = bookRepository.update(bookId, book).toBookDto()

// call saveImages in case image url is set
// call saveImages in case image url is set ?
/**
* Image not updated, to add or update an image call the variant which accepts a MultiPartFile
*/
@Transactional
fun update(userBookId: UUID, book: UserBookUpdateDto): UserBookLightDto = bookRepository.update(userBookId, book).toUserBookLightDto()

Expand Down Expand Up @@ -129,8 +135,29 @@ class BookService(
)
)
}
var backup: File? = null
var currentImage: File? = null
if (file != null || userBook.book.image != null) {
// existing book used on UserBook already had an image, backup it
if (!book.image.isNullOrBlank()) {
currentImage = File(properties.files.images, book.image)
backup = File(properties.files.images, "${book.image}.bak")
Files.move(currentImage.toPath(), backup.toPath())
}
book.image = saveImages(file, book.title, book.id.toString(), userBook.book.image, properties.files.images)
// we had a previous image and we saved a new one : delete the old one
if (backup != null && backup.exists()) {
// successfully saved new image, delete backup
if (book.image != null && book.image!!.isNotBlank()) {
Files.deleteIfExists(backup.toPath())
} else {
// saving new file failed ? Restore backup
if (currentImage != null) {
Files.move(backup.toPath(), currentImage.toPath())
book.image = currentImage.name
}
}
}
}
return created.toUserBookLightDto()
}
Expand All @@ -145,7 +172,6 @@ class BookService(
fun saveImages(file: MultipartFile?, title: String, id: String, dtoImage: String?, targetDir: String): String? {
var importedFile = false
var savedImage: String? = null
// FIXME resize image when saving (protect with a flag)
if (file != null) {
try {
val destFileName: String = imageName(slugify(title), id, FilenameUtils.getExtension(file.originalFilename))
Expand Down Expand Up @@ -273,6 +299,10 @@ class BookService(
bookRepository.deleteTagById(tagId)
}

/**
* Removes an author from a book without deleting the author from the database.
* The author is removed only from that book.
*/
@Transactional
fun deleteAuthorFromBook(bookId: UUID, authorId: UUID) {
bookRepository.deleteAuthorFromBook(bookId, authorId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.github.bayang.jelu.service.ImportService
import io.github.bayang.jelu.service.ReadingEventService
import io.github.bayang.jelu.service.UserService
import io.github.bayang.jelu.service.metadata.FetchMetadataService
import io.github.bayang.jelu.utils.toInstant
import mu.KotlinLogging
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
Expand All @@ -32,7 +33,6 @@ import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.io.File
import java.time.Instant
import java.time.LocalDate
import java.time.ZoneId
import java.time.format.DateTimeFormatter
Expand Down Expand Up @@ -269,8 +269,6 @@ class CsvImportService(
.count().toInt() > 0
}

private fun toInstant(date: LocalDate): Instant = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()

private fun readingStatus(readStatusFromShelves: String): ReadingEventType? {
return if (readStatusFromShelves.equals(CURRENTLY_READING, true)) {
ReadingEventType.CURRENTLY_READING
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/utils/DateUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.github.bayang.jelu.utils

import java.time.Instant
import java.time.LocalDate
import java.time.OffsetDateTime
import java.time.ZoneId

fun nowInstant(): Instant = OffsetDateTime.now(ZoneId.systemDefault()).toInstant()

fun toInstant(date: LocalDate): Instant = date.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()
58 changes: 58 additions & 0 deletions src/test/kotlin/io/github/bayang/jelu/TestHelpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.bayang.jelu

import io.github.bayang.jelu.dao.ReadingEventType
import io.github.bayang.jelu.dto.AuthorDto
import io.github.bayang.jelu.dto.BookCreateDto
import io.github.bayang.jelu.dto.CreateUserBookDto
import io.github.bayang.jelu.dto.TagDto
import java.time.Instant

fun createUserBookDto(bookDto: BookCreateDto, lastReadingEvent: ReadingEventType? = null, lastreadingEventDate: Instant? = null): CreateUserBookDto {
return CreateUserBookDto(
personalNotes = "test personal notes\nwith a newline",
lastReadingEvent = lastReadingEvent,
lastReadingEventDate = lastreadingEventDate,
owned = true,
toRead = false,
percentRead = null,
book = bookDto
)
}

fun bookDto(title: String = "title1", withTags: Boolean = false): BookCreateDto {
return BookCreateDto(
id = null,
title = title,
isbn10 = "1566199093",
isbn13 = "9781566199094 ",
summary = "This is a test summary\nwith a newline",
image = "",
publisher = "test-publisher",
pageCount = 50,
publishedDate = "",
series = "",
authors = mutableListOf(authorDto()),
numberInSeries = null,
tags = if (withTags) tags() else emptyList(),
goodreadsId = "4321abc",
googleId = "1234",
librarythingId = "",
language = "",
amazonId = ""
)
}

fun authorDto(name: String = "test author"): AuthorDto {
return AuthorDto(id = null, creationDate = null, modificationDate = null, name = name, image = "", dateOfBirth = "", dateOfDeath = "", biography = "author bio", facebookPage = null, goodreadsPage = null, instagramPage = null, notes = null, officialPage = null, twitterPage = null, wikipediaPage = "https://wikipedia.org")
}

fun tags(): List<TagDto> {
val tags = mutableListOf<TagDto>()
tags.add(tagDto())
tags.add(tagDto("fantasy"))
return tags
}

fun tagDto(name: String = "science fiction"): TagDto {
return TagDto(id = null, creationDate = null, modificationDate = null, name)
}
Loading

0 comments on commit 8737192

Please sign in to comment.