Skip to content

Commit

Permalink
fix: cannot remove cover
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Jun 2, 2022
1 parent a1df750 commit dd77866
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/jelu-ui/src/components/EditBookModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ const handleFileUpload = (event: any) => {
file.value = event.target.files[0];
};
const imageUrl = ref<string | null>(null);
const clearImageField = () => {
imageUrl.value = "";
};
const imageUrl = ref<string | null>(null);
const file = ref(null);
const uploadFromWeb = ref(true);
let uploadlabel = computed(() => {
Expand Down Expand Up @@ -80,9 +81,10 @@ const importBook = () => {
if (!props.canAddEvent || userbook.value.lastReadingEvent === ReadingEventType.NONE) {
userbook.value.lastReadingEvent = null
}
userbook.value.book.image = null
if (StringUtils.isNotBlank(imageUrl.value)) {
userbook.value.book.image = imageUrl.value
} else if (deleteImage.value) {
userbook.value.book.image = null
}
console.log(`push book ` + userbook.value);
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/service/BookService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BookService(
private val eventRepository: ReadingEventRepository,
private val properties: JeluProperties,
private val downloadService: DownloadService,
private val fileManager: FileManager
) {

@Transactional
Expand Down Expand Up @@ -94,8 +95,18 @@ class BookService(
var backup: File? = null
var skipSave = false
// no multipart image and url image field is empty in udate dto
// it means no new file upload and image has been explicitely set to null in update dto -> remove existing image
// otherwise it is impossible to remove an image from the UI without replacing it by a new one
if (file == null && book.book?.image.isNullOrBlank()) {
skipSave = true
// if only userbook is provided (eg if only userbook fields have to be updated)
// then don't touch the image
if (book.book != null && book.book.image.isNullOrBlank()) {
updated.book.image = null
if (previousImage != null) {
fileManager.deleteImage(previousImage)
}
}
} else if (file == null && !book.book?.image.isNullOrBlank() && ! previousImage.isNullOrBlank() && previousImage.equals(book.book?.image, false)) {
// no multipart file and image field in update dto is the same as in BDD -> no change
skipSave = true
Expand Down
62 changes: 62 additions & 0 deletions src/test/kotlin/io/github/bayang/jelu/service/BookServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,68 @@ class BookServiceTest(
Assertions.assertEquals(1, File(jeluProperties.files.images).listFiles().size)
}

@Test
fun testUpdateUserbookWithImageAndEventNewEventRequiredAndDeleteExistingImage() {
val createBook = bookDto()
val createUserBookDto = createUserBookDto(createBook, ReadingEventType.FINISHED, nowInstant())
val uploadFile = MockMultipartFile("test-cover.jpg", "test-cover.jpg", "image/jpeg", this::class.java.getResourceAsStream("test-cover.jpg"))
val saved: UserBookLightDto = bookService.save(createUserBookDto, user(), uploadFile)
Assertions.assertEquals(createBook.title, saved.book.title)
Assertions.assertEquals(createBook.isbn10, saved.book.isbn10)
Assertions.assertEquals(createBook.isbn13?.trim(), saved.book.isbn13)
Assertions.assertEquals("This is a test summary with a newline", saved.book.summary)
Assertions.assertEquals(createBook.publisher, saved.book.publisher)
Assertions.assertEquals(createBook.pageCount, saved.book.pageCount)
Assertions.assertEquals(createBook.goodreadsId, saved.book.goodreadsId)
Assertions.assertNull(saved.book.librarythingId)
Assertions.assertEquals(createUserBookDto.owned, saved.owned)
Assertions.assertEquals(createUserBookDto.toRead, saved.toRead)
Assertions.assertEquals(createUserBookDto.personalNotes, saved.personalNotes)
Assertions.assertNull(saved.percentRead)
Assertions.assertNotNull(saved.creationDate)
Assertions.assertNotNull(saved.modificationDate)
Assertions.assertNotNull(saved.book.creationDate)
Assertions.assertNotNull(saved.book.modificationDate)
Assertions.assertTrue(saved.book.image!!.contains(slugify(saved.book.title), true))
Assertions.assertEquals(ReadingEventType.FINISHED, saved.lastReadingEvent)
Assertions.assertNotNull(saved.lastReadingEventDate)
Assertions.assertEquals(1, readingEventService.findAll(null, null, null, null, null, Pageable.ofSize(30)).totalElements)
Assertions.assertEquals(1, File(jeluProperties.files.images).listFiles().size)

val updater = UserBookUpdateDto(
ReadingEventType.DROPPED,
personalNotes = "new notes",
owned = false,
book = createBook.copy(image = null),
toRead = null,
percentRead = 50
)
// val replacementFile = MockMultipartFile("test-replace-cover.jpg", "test-replace-cover.jpg", "image/jpeg", this::class.java.getResourceAsStream("test-cover.jpg"))
val updated = bookService.update(saved.id!!, updater, null)
Assertions.assertEquals(createBook.title, updated.book.title)
Assertions.assertEquals(createBook.isbn10, updated.book.isbn10)
Assertions.assertEquals(createBook.isbn13?.trim(), updated.book.isbn13)
Assertions.assertEquals("This is a test summary with a newline", updated.book.summary)
Assertions.assertEquals(createBook.publisher, updated.book.publisher)
Assertions.assertEquals(createBook.pageCount, updated.book.pageCount)
Assertions.assertEquals(createBook.goodreadsId, updated.book.goodreadsId)
Assertions.assertEquals("", updated.book.librarythingId)
Assertions.assertEquals(updater.owned, updated.owned)
Assertions.assertEquals(saved.toRead, updated.toRead)
Assertions.assertEquals(updater.percentRead, updated.percentRead)
Assertions.assertEquals(updater.personalNotes, updated.personalNotes)
Assertions.assertNotNull(updated.creationDate)
Assertions.assertNotNull(updated.modificationDate)
Assertions.assertNotNull(updated.book.creationDate)
Assertions.assertNotNull(updated.book.modificationDate)
Assertions.assertNull(updated.book.image)
Assertions.assertEquals(ReadingEventType.DROPPED, updated.lastReadingEvent)
Assertions.assertNotNull(updated.lastReadingEventDate)
Assertions.assertEquals(2, updated.readingEvents?.size)
Assertions.assertEquals(2, readingEventService.findAll(null, null, null, null, null, Pageable.ofSize(30)).totalElements)
Assertions.assertEquals(0, File(jeluProperties.files.images).listFiles().size)
}

@Test
fun testUpdateUserbookWithImageAndEventNewEventRequiredAndNewImage() {
val createBook = bookDto()
Expand Down

0 comments on commit dd77866

Please sign in to comment.