Skip to content

Commit

Permalink
fix: deleting an user generates an error
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed Jan 6, 2025
1 parent 15c866c commit 12892af
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/dao/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,10 @@ class BookRepository(
entity.delete()
}

fun deleteUserBookForUser(userId: UUID) {
UserBookTable.deleteWhere { user eq userId }
}

fun deleteBookById(bookId: UUID) {
val book = Book[bookId]
book.image?.let { fileManager.deleteImage(it) }
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/io/github/bayang/jelu/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.bayang.jelu.service

import io.github.bayang.jelu.dao.BookRepository
import io.github.bayang.jelu.dao.Provider
import io.github.bayang.jelu.dao.User
import io.github.bayang.jelu.dao.UserRepository
Expand Down Expand Up @@ -29,6 +30,7 @@ class UserService(
private val sessionRegistry: SessionRegistry,
private val sessionsRepo: FindByIndexNameSessionRepository<out Session>,
private val passwordEncoder: PasswordEncoder,
private val bookRepository: BookRepository,
) : UserDetailsService {

@Transactional
Expand Down Expand Up @@ -82,6 +84,7 @@ class UserService(

@Transactional
fun deleteUser(userId: UUID) {
bookRepository.deleteUserBookForUser(userId)
userRepository.deleteUser(userId)
}

Expand Down
34 changes: 34 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 @@ -1445,6 +1445,40 @@ class BookServiceTest(
Assertions.assertEquals(targetPageableSize, randomNumberOfBooks)
}

@Test
fun testDeleteUserWithUserbookWithNewBookImageAndEvent() {
val userDto = userService.save(CreateUserDto(login = "testdelete", password = "1234", isAdmin = true))
var entitiesIds = luceneHelper.searchEntitiesIds("title1", LuceneEntity.Book)
Assertions.assertEquals(0, entitiesIds?.size)
val createBook = bookDto()
val createUserBookDto = createUserBookDto(createBook, ReadingEventType.CURRENTLY_READING, 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, userDto, 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.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.CURRENTLY_READING, saved.lastReadingEvent)
Assertions.assertNotNull(saved.lastReadingEventDate)
Assertions.assertEquals(1, readingEventService.findAll(null, null, null, null, null, null, null, Pageable.ofSize(30)).totalElements)
Assertions.assertEquals(1, File(jeluProperties.files.images).listFiles().size)
entitiesIds = luceneHelper.searchEntitiesIds("title1", LuceneEntity.Book)
Assertions.assertEquals(1, entitiesIds?.size)
userService.deleteUser(userDto.id!!)
}

@Test
fun testInsertUserbookWithNewBookImageAndEvent() {
var entitiesIds = luceneHelper.searchEntitiesIds("title1", LuceneEntity.Book)
Expand Down

0 comments on commit 12892af

Please sign in to comment.